Sub topic 2
Deciding the grain before anything else
This chapter assumes you know how to load data into Power BI Desktop, read the Model view, and recognize primary and foreign keys. Fact and dimension tables get their own chapters later. Here you only need to decide what one row of a fact table means.
What grain means
The grain of a table is the answer to the question "what does one row represent?" You state it in a single sentence, for example "one row per order line" or "one row per product per day".
Decide the grain first because every other choice depends on it. The grain determines which columns the fact table can hold, which dimensions can connect to it, and which questions the model can answer.
A worked example
Suppose your source holds retail orders. An order has several lines, and each line is one product with a quantity and an amount. You have three candidate grains:
| Grain | One row is | Can answer | Cannot answer |
|---|---|---|---|
| Order | One whole order | Sales per order, per customer, per date | Sales by product |
| Order line | One product within one order | Sales by product, category, customer, date | Anything below line level, such as individual units |
| Product per day | Daily totals per product | Daily product trends | Which order a sale belonged to |
The order-line grain is the most detailed of the three. Detail is usually what you want, because you can always add up detailed rows but you cannot split summed rows apart again. The FactSales table in this tutorial uses this grain.
CREATE TABLE FactSales (
OrderNumber VARCHAR(20) NOT NULL,
OrderLineNumber INT NOT NULL,
DateKey INT NOT NULL,
ProductKey INT NOT NULL,
Quantity INT NOT NULL,
SalesAmount DECIMAL(18, 2) NOT NULL,
PRIMARY KEY (OrderNumber, OrderLineNumber)
);The primary key shows the grain. OrderNumber and OrderLineNumber together identify exactly one row, which matches the sentence "one row per order line".
Test the grain
Check that your sentence holds in the data. This query returns every combination that appears more than once. It should return no rows.
SELECT OrderNumber, OrderLineNumber, COUNT(*) AS RowCount
FROM FactSales
GROUP BY OrderNumber, OrderLineNumber
HAVING COUNT(*) > 1;If it returns rows, either the data has duplicates or your stated grain is wrong. Fix that before you build relationships.
Common errors
- Mixed grains in one table. A table that holds order-level rows and order-line rows together double counts amounts when you sum them. Use one fact table per grain.
- Repeating an order-level value on every line. If a shipping fee belongs to the order but is stored on each line,
SUMmultiplies it by the number of lines. Keep the fee in an order-grain table or allocate it across lines. - Choosing the grain after building visuals. You end up bending the model to fit one report, and the next report breaks it.
- Going coarser than needed to save space. Pre-aggregating discards detail you cannot recover. Start at the finest grain the business needs, then consider aggregation if performance requires it.
Exercise
A fact table has one row per product per store per day. A report needs the order number that each sale belonged to. Can this table answer that, and what should you do?
Show answer
No. The grain sums sales across orders, so the order number is not stored at that level. Either build a fact table at order-line grain that includes the order number, or accept that this question cannot be answered from the daily table.
Exercise
Your FactSales table has the stated grain "one row per order line". Which query checks that the grain holds?
Show answer
Group by OrderNumber and OrderLineNumber, and use HAVING COUNT(*) > 1. Any rows returned are duplicates that break the stated grain.