Sub topic 5
Keys that connect facts to dimensions
This chapter assumes you can load data in Power BI Desktop, open the Model view, and read simple Power Query and DAX. It also assumes you know what a table, a row, and a column are. Earlier chapters defined fact tables, dimension tables, and grain. Here the focus is the columns that tie them together: keys.
Why keys matter in a Power BI model
A relationship in Power BI joins one column in one table to one column in another. Those columns are keys. If a key is wrong, the relationship is wrong, and every visual that crosses it returns wrong numbers or blanks. Most "my slicer does nothing" problems come from key design, not from DAX.
Three kinds of key appear in a star schema:
| Key | Where it lives | Role |
|---|---|---|
| Primary key | Dimension table | Uniquely identifies each dimension row |
| Foreign key | Fact table | Stores the primary key of the related dimension row |
| Surrogate key | Usually a dimension table, copied into facts | A meaningless integer created by the model builder to replace a messy source key |
Primary keys in dimension tables
A primary key is a column whose value is unique in every row and never blank. In DimProduct, that column is ProductKey. The one side of a relationship must be unique, so the primary key is what makes the dimension a valid "one" side.
Power BI does not enforce primary keys the way a relational database does. You can import a dimension with duplicate keys and the load will succeed. The problem appears when you create the relationship. Power BI detects the duplicates and either refuses to create a one-to-many relationship or falls back to many-to-many. Neither is what you want for a star schema.
The fact table also has a key, but it is a different kind. In the chapter's example, FactSales is identified by the combination of OrderNumber and OrderLineNumber. This composite key defines the grain: one row per order line. Nothing in the model needs it as a relationship column, because no other table points at the fact table.
Foreign keys in fact tables
A foreign key is a column in the fact table that holds a value from a dimension's primary key. FactSales[ProductKey] is a foreign key to DimProduct[ProductKey]. Each fact row stores one foreign key per dimension that describes it.
The following SQL, from the plan for this tutorial, shows both sides.
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 REFERENCES clause is a database-level guarantee that every ProductKey in FactSales exists in DimProduct. Power BI has no such guarantee. If the fact table contains a ProductKey that the dimension lacks, Power BI loads both tables without complaint. It then groups those fact rows under a blank member in visuals. This is called a referential integrity violation, and it is the most common key defect in real models.
Surrogate keys versus natural keys
A natural key (also called a business key) comes from the source system, such as a SKU like BK-R93R-62 or a customer email address. A surrogate key is an integer you generate, with no business meaning, such as ProductKey = 1042.
Natural keys are tempting because they already exist. They cause trouble in these situations:
- Text keys are larger. VertiPaq, the engine that stores imported tables, compresses columns by value. A text key with many distinct values compresses poorly. An integer key compresses better and joins faster.
- Composite natural keys. Power BI relationships use exactly one column on each side. If the source identifies a row by two columns, such as
StoreIDplusRegisterID, you must combine them into one column first. - Source keys change or collide. Two source systems may both use
1001for different products. A merged dimension needs its own key. - History. If you keep multiple versions of a customer row when an address changes, the natural key repeats. A surrogate key gives each version its own identity.
Surrogate keys are not always required. If you have one clean source with a stable integer ID, using it directly is fine. Create a surrogate when the natural key is text, composite, duplicated across sources, or needs history.
The date key convention
The date dimension is the usual place where a smart key is acceptable. The spine's date table uses YEAR * 10000 + MONTH * 100 + DAY, so 27 November 2026 becomes 20261127. This integer is readable and sorts correctly. The fact table carries the same format in DateKey.
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2025, 12, 31 ) ),
"DateKey", YEAR ( [Date] ) * 10000 + MONTH ( [Date] ) * 100 + DAY ( [Date] ),
"Year", YEAR ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "MMM" )
)Note that the calendar ends on 31 December 2025. Any fact row with a later date would have no match in this table. Make the range cover every date in the fact table.
Building a key in Power Query
When a flat table has no dimension key, you can create one in Power Query. This query follows the chapter plan and builds DimProduct from a flat SalesFlat table using the source ProductID as the key.
let
Source = SalesFlat,
ProductColumns = Table.SelectColumns(Source, {"ProductID", "ProductName", "Subcategory", "Category"}),
DimProduct = Table.Distinct(ProductColumns, {"ProductID"})
in
DimProductTable.Distinct with {"ProductID"} keeps one row per ProductID. This only works well if each ProductID has a single name and category. If the same ID appears with two different names, Table.Distinct keeps the first one it meets and silently drops the other, which hides a data quality problem. Check for it before relying on the result, for example by grouping on ProductID and counting distinct names.
To add a surrogate key to a dimension, use an index column:
- In the dimension query, choose Add Column, then Index Column, then From 1.
- Rename the new column
ProductKey. - In the fact query, merge with the dimension on the natural key (
ProductID), expandProductKey, and removeProductIDfrom the fact table.
Removing the natural key from the fact table matters. It reduces the size of the model and prevents report authors from using the wrong column.
An index-based surrogate key is regenerated each refresh. It stays consistent between the fact and the dimension only because both are built in the same refresh from the same source. Do not store it in another system or use it in a report bookmark that expects stable values.
Data types and relationship columns
Both ends of a relationship should have the same data type. If one side is whole number and the other is text, Power BI may refuse the relationship or it may match fewer rows than expected. Text keys also have hidden problems: trailing spaces, mixed case, and leading zeros. "A100" and "A100 " are different values. Trim and standardize text keys in Power Query before you load them.
Whole-number keys avoid most of these issues. That is one more reason to prefer integers.
Checking key quality
Before you build relationships, run these checks. Each takes a minute and prevents wrong totals later.
- Dimension key is unique. In Power Query, turn on Column quality and Column distribution under View. The key column should show 100 percent unique with no errors or empty values.
- No blanks in fact foreign keys. A blank foreign key cannot match any dimension row.
- Every fact key exists in the dimension. In a table visual, put a dimension attribute next to
Total Sales. A row labelled "(Blank)" with a nonzero total signals orphan keys.
If orphan keys exist, you have two sound options. Fix the source, or add a row to the dimension with a key such as -1 and the name "Unknown", then replace unmatched fact keys with -1 in Power Query. The second option keeps the numbers visible under a clear label instead of an anonymous blank.
How keys lead into relationships
The next chapter creates the one-to-many relationships themselves. The key design above decides how easy that step is. When the dimension has a unique integer primary key and the fact table holds a matching integer foreign key of the same type, Power BI detects the relationship, sets cardinality to one-to-many, and sets the filter direction to single, from dimension to fact.
Summary of decisions
| Situation | Recommended key |
|---|---|
| Single clean source with a stable integer ID | Use the source ID directly |
| Text, composite, or duplicated natural keys | Create an integer surrogate key |
| Date dimension | Integer in `YYYYMMDD` form |
| Fact table with unmatched dimension values | Add an "Unknown" row with key `-1` |
Exercise
A fact table has a ProductKey column, and a table visual that shows Total Sales by Category displays a "(Blank)" category with a nonzero total. The dimension's ProductKey is unique. What is the most likely cause, and what are two ways to fix it?
Show answer
Some ProductKey values in the fact table do not exist in the dimension (orphan keys), so Power BI groups those rows under a blank member. Fix it by correcting the source data or the dimension load so the missing products are present, or by adding an "Unknown" dimension row with key -1 and replacing the unmatched fact keys with -1 in Power Query.
Exercise
A source table identifies a store register by two columns, StoreID and RegisterID. Why can't you relate it directly to a fact table, and what would you do?
Show answer
A Power BI relationship uses one column on each side. Create a single-column key, either a surrogate integer produced by an index column in the dimension and merged into the fact, or a combined column such as StoreID and RegisterID joined with a separator in both tables. The surrogate integer is usually better because it compresses well.