Sub topic 3
Fact tables: events and numeric measurements
This chapter assumes you know what a primary key and a foreign key are, and that you can open the Model view in Power BI Desktop. It covers what a fact table stores, how its grain shapes its columns, and the mistakes people make most often when building one.
What a fact table records
A fact table records events that happened in the business. Each row is one event at the level of detail you chose as the grain. A sale, a shipment, a website click, and a support ticket closing are all events.
A fact table has two kinds of columns:
- Foreign keys that point to dimension tables, such as
DateKeyandProductKey. These say which date, product, or customer the event involved. - Numeric columns that measure the event, such as
QuantityandSalesAmount. You aggregate these with sums, counts, and averages.
Descriptive text, such as a product name or a category, belongs in a dimension table. Keeping it out of the fact table is what keeps the fact table narrow.
A fact table at order-line grain
The FactSales table below has one row per order line. The pair OrderNumber and OrderLineNumber identifies each row. That pair is the declared grain written as a key.
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)
);FactSales holds no product names. To report sales by category, Power BI follows ProductKey to DimProduct and reads Category from there.
How the grain decides the columns
Every column you add must be true at the grain. Quantity and sales amount are true for one order line. A column such as "total order amount" is true for the whole order, so it does not fit.
If you repeat an order-level value on every line, a simple SUM counts it once per line. The total comes out too high. Put order-level values in a separate fact table at order grain, or leave them out.
Types of numeric columns
Not every number can be added in every direction.
| Type | Example | How to aggregate |
|---|---|---|
| Additive | `SalesAmount`, `Quantity` | Sum across every dimension |
| Semi-additive | Account balance, stock level | Sum across products, but not across time |
| Non-additive | Unit price, margin percentage | Never sum. Recalculate from additive parts |
For a ratio such as margin percentage, store the additive parts (profit and sales) in the fact table. Divide them in a measure.
Common errors
- Mixed grain. Two kinds of rows share one table, for example order lines and order totals. Totals double count. Split them into two fact tables.
- Text in the fact table. Product names and categories repeated on millions of rows inflate the model. Move them to a dimension.
- A missing or duplicated key. If a fact row's
ProductKeyhas no match in the dimension, the row shows up under a blank member in visuals. Check for blanks after loading. - Summing a non-additive column. A summed unit price gives a meaningless number.
Exercise
A fact table has one row per order line. Someone adds a column OrderShippingCost and repeats the same value on every line of an order. Why is this a problem, and what are two ways to fix it?
Show answer
The value is true for the whole order, not for one line. Summing it counts the cost once per line, so the total is too high. Fix it by moving shipping cost into a separate fact table at order grain, or by allocating the cost across the lines so the line values add up to the order's shipping cost.