Sub topic 5
Keys that connect facts to dimensions
This chapter assumes you know what a fact table and a dimension table are, and that you have used the Model view in Power BI Desktop. It covers the three kinds of keys a star schema depends on and the mistakes that break relationships.
Why keys matter
A fact table stores events and numbers. A dimension table stores descriptions. Keys are the columns that link the two, so a row in the fact table can find its product, customer, or date. Power BI relationships are built on a key column on each side. If the keys are wrong, filters do not flow and totals are wrong.
Three kinds of keys
- Primary key: a column whose value is unique in every row of a dimension table.
ProductKeyinDimProductis an example. - Foreign key: a column in the fact table that holds a primary key value from a dimension.
FactSales[ProductKey]points toDimProduct[ProductKey]. - Surrogate key: a key created for the model, usually an integer with no business meaning. It replaces a natural key such as a SKU or an email address.
The fact table often has its own identifier too. In the example below, the pair OrderNumber and OrderLineNumber identifies one row, which matches the order-line grain.
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)
);DimProduct.ProductKey is the primary key. FactSales.ProductKey is the foreign key that references it.
Why use surrogate keys
Natural keys come from source systems, and they change. A product may be renumbered, or two source systems may reuse the same code. A surrogate key isolates the model from those changes. Integer keys also compress well in the VertiPaq engine, the in-memory storage engine behind Power BI, and they make relationships cheaper to evaluate than long text keys.
A date key is a common surrogate. The date table in this series builds DateKey as an integer such as 20250131, so the fact table stores a small whole number instead of a date-time value.
Rules for a key that works
- The dimension key must be unique and must not be blank. The "one" side of a one-to-many relationship requires this.
- Both columns in a relationship must have the same data type. Join an integer to an integer, not to text.
- Every foreign key value in the fact table should exist in the dimension. A value with no match shows up under a blank member in visuals.
- Keep keys out of visuals. Hide key columns from Report view so report authors use descriptive attributes instead.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Many-to-many" appears when you create a relationship | Duplicate values in the dimension key | Remove duplicates in Power Query or choose a truly unique column |
| A blank row appears in a slicer | Fact rows with no matching dimension key | Add the missing member to the dimension, or fix the fact data |
| The relationship cannot be created | Mismatched data types, such as text and whole number | Change the type in Power Query so both match |
| Totals are correct but a dimension attribute shows odd groupings | Key chosen at the wrong level of detail | Re-check the grain and use the true key |
Exercise
A FactSales table has a ProductKey column with the whole-number type. DimProduct has a ProductKey column, also a whole number, but 3 of its 500 rows share the same ProductKey. What happens when you create the relationship, and how do you fix it?
Show answer
Power BI cannot treat DimProduct as the "one" side because the key is not unique, so it proposes a many-to-many relationship or refuses the one-to-many setting. Fix the dimension by removing the duplicate rows in Power Query (for example with Remove Duplicates on ProductKey), or by correcting the source so each product has one key. Then recreate the relationship as one-to-many from DimProduct to FactSales.