Sub topic 2
Deciding the grain before anything else
This chapter assumes you can load data into Power BI Desktop, open the Model view, and read simple SQL and DAX. You should also know what primary and foreign keys are. The chapter covers what grain is, how to state it, and which problems appear when it is wrong or mixed.
What grain means
The grain of a table is the answer to the question "what does one row represent?" Grain is a statement about meaning, not about columns. "One row per product" is a grain. "One row per order line" is a grain. A list of column names is not.
You set the grain of a fact table before you choose any columns. Every later decision depends on it:
- Which dimensions can attach to the table.
- Which numeric columns can be stored on it.
- Which measures will give correct totals.
If you skip this step, you tend to add columns until the table "feels right". The result is rows that mean different things in different columns, and totals that look plausible but are wrong.
Stating the grain in one sentence
Write the grain as a plain sentence before you build anything. A good sentence names the business event and the unit that makes each row unique.
| Business process | Grain statement |
|---|---|
| Retail sales | One row per product on each order line |
| Web analytics | One row per page view per session |
| Inventory | One row per product per warehouse per day |
| Support tickets | One row per ticket status change |
Notice that each statement contains the word "per" and lists every part of the row's identity. If you cannot list those parts, the grain is not yet decided.
Grain decides the dimensions
Once the grain is fixed, the dimensions follow. Ask which descriptive contexts are true for a single row at that grain.
For the grain "one row per product on each order line", these contexts are single-valued for each row:
- The product that was sold.
- The date of the order.
- The customer who placed it.
- The store or channel.
Each context becomes a dimension, and each gets a key column in the fact table. A context that can have many values for one row, such as "all promotions that applied to the order", does not fit this grain cleanly. It needs a different treatment, which a later chapter on many-to-many relationships and bridge tables covers.
Grain decides the facts
The grain also decides which numbers you can store. A numeric column belongs on the fact table only if it has one correct value for each row at that grain.
At order-line grain:
Quantityis correct, because each line has its own quantity.SalesAmountis correct, because each line has its own amount.OrderShippingCostis a problem. Shipping is charged once for the whole order, not once per line.
If you copy an order-level shipping cost onto every line, summing the column repeats it. An order with three lines and a shipping cost of 10 would report 30.
A worked example
Suppose a source system gives you this flat extract of orders.
| OrderNumber | OrderLine | OrderDate | Product | Quantity | LineAmount | ShippingCost |
|---|---|---|---|---|---|---|
| SO1001 | 1 | 2025-03-02 | Helmet | 1 | 50.00 | 10.00 |
| SO1001 | 2 | 2025-03-02 | Gloves | 2 | 40.00 | 10.00 |
| SO1001 | 3 | 2025-03-02 | Lock | 1 | 30.00 | 10.00 |
| SO1002 | 1 | 2025-03-03 | Helmet | 2 | 100.00 | 8.00 |
The grain of this extract is one row per order line. Quantity and LineAmount agree with that grain. ShippingCost does not, because it is the same on every line of an order.
A sum of ShippingCost over the four rows gives 38.00. The real shipping total is 18.00 (10.00 for SO1001 and 8.00 for SO1002). The sum is wrong by 20.00 because of repetition across lines.
You have three honest options:
- Allocate shipping across the lines in the source or in Power Query, so each line carries its own share. The allocated values then sum correctly.
- Move shipping to a separate fact table at order grain, with one row per order.
- Leave shipping out of the model if the report does not need it.
Option 2 gives you two fact tables with different grains. That is normal and correct, as long as each table has one grain and each is related to the shared dimensions.
The model from the spine
The chapter's reference tables use order-line grain. FactSales declares its grain through its primary key, which is made of the order number and the line number.
CREATE TABLE DimProduct (
ProductKey INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
Subcategory VARCHAR(50),
Category VARCHAR(50)
);
CREATE TABLE FactSales (
OrderNumber VARCHAR(20) NOT NULL,
OrderLineNumber INT NOT NULL,
DateKey INT NOT NULL,
ProductKey INT NOT NULL REFERENCES DimProduct (ProductKey),
Quantity INT NOT NULL,
SalesAmount DECIMAL(18, 2) NOT NULL,
PRIMARY KEY (OrderNumber, OrderLineNumber)
);The composite primary key (OrderNumber, OrderLineNumber) is the grain written as a constraint. The database will reject a second row for the same order line. If you can write the key that makes a row unique, you have stated the grain.
Power BI does not enforce primary keys on fact tables. Power BI relationships only require uniqueness on the "one" side, which is the dimension. You must check the fact grain yourself.
Checking the grain with a query
You can test a grain claim by looking for duplicates on the columns that should be unique. Run this against the source or the loaded table.
SELECT OrderNumber, OrderLineNumber, COUNT(*) AS RowCount
FROM FactSales
GROUP BY OrderNumber, OrderLineNumber
HAVING COUNT(*) > 1;An empty result means the grain holds. Any rows returned are duplicates, and they will inflate every sum that touches them.
In DAX you can compare the number of rows with the number of distinct grain combinations.
Grain Check =
VAR RowsInTable = COUNTROWS ( Sales )
VAR DistinctLines =
COUNTROWS (
SUMMARIZE ( Sales, Sales[OrderNumber], Sales[OrderLineNumber] )
)
RETURN
RowsInTable - DistinctLinesA result of 0 means every row is a unique order line. Any other value is the count of surplus rows. This sketch assumes the fact table is loaded into the model as Sales, as in the measures later in this tutorial.
Common grain mistakes
Mixed grain in one table
This is the most damaging mistake. A table holds some rows at one level and some at another, for example order-line rows plus an "order total" row for each order. Any sum then counts the same money twice.
The fix is to split the table by grain. Keep one fact table per grain.
Grain that is too coarse
If you load monthly totals only, you cannot answer questions about a single day or a single order. You can always roll detail up, but you cannot break a summary back down. Choose the lowest grain that the source reliably provides and that the business will ask about.
Grain that is too fine
Storing every click when reports only need daily counts makes the model large. The VertiPaq engine, which Power BI uses to store imported data, compresses repeated values well. Still, millions of unneeded rows cost memory and refresh time. The trade-off is to pick the most detailed grain you will actually use, then aggregate only if size becomes a problem.
Grain changed by a join
A join can silently change grain. If you merge an order-line table with a payments table where one order has two payments, each line is duplicated and the grain is no longer one row per line. Check the row count before and after every merge.
How grain affects measures
A measure such as Total Sales = SUM ( Sales[SalesAmount] ) is correct only because each row's SalesAmount is the amount for that row's grain. The measure does not know the grain. It adds whatever is in the column, filtered by the current context.
This is why you fix the grain first. If the column holds repeated or mixed values, no measure written on top of it can be right in all cases.
A short procedure
Follow these steps for each new fact table.
- Name the business process, such as sales, inventory, or tickets.
- Write the grain as one sentence with "one row per".
- List the columns that together make a row unique.
- List the dimensions that have a single value for each row.
- Keep only numeric columns that have one correct value per row.
- Test uniqueness with a duplicate query or a DAX check.
- Record the grain in the table description so other modelers can see it.
Exercise
A source table has one row per order line, with columns OrderNumber, OrderLine, ProductKey, Quantity, LineAmount, and OrderDiscount. OrderDiscount is a single discount applied to the whole order and is repeated on every line. State the grain, say which numeric columns fit it, and explain what goes wrong if you sum OrderDiscount.
Show answer
The grain is one row per order line, identified by OrderNumber and OrderLine. Quantity and LineAmount fit, because each line has its own value. OrderDiscount does not fit, because it belongs to the order. Summing it counts the discount once for every line, so an order with three lines and a discount of 5 reports 15. Fix it by allocating the discount across lines, or by moving it to an order-level fact table with one row per order.
Exercise
Your Power Query merge of an order-line table with a payments table raises the row count from 1,000 to 1,150. What most likely happened, and how do you confirm it?
Show answer
Some orders have more than one payment, so the merge duplicated their order lines and the grain is no longer one row per line. Confirm it by grouping on OrderNumber and OrderLine and looking for counts above 1. Fix it by aggregating payments to order grain before merging, or by keeping payments in their own fact table.