Sub topic 7
Creating one-to-many relationships
This chapter assumes you know how to open the Model view in Power BI Desktop. It also assumes you know what primary and foreign keys are, and that you have already split your data into a fact table and dimension tables.
What a one-to-many relationship means
A relationship links a column in one table to a column in another. In a star schema, every relationship has the same shape. The one side is a dimension table, where the key column holds unique values. The many side is the fact table, where the matching foreign key repeats across rows.
For example, DimProduct[ProductKey] is unique, and FactSales[ProductKey] repeats once per order line that sold that product.
Creating the relationship
You can create a relationship in either of two ways:
- In Model view, drag
ProductKeyfromDimProductontoProductKeyinFactSales. - Choose Manage relationships, then New relationship. Pick both tables and select the key column in each.
Then check the dialog settings:
- Cardinality should be One to many (1:*), with the dimension on the one side.
- Cross filter direction should be Single, so filters travel from the dimension to the fact table.
- Make this relationship active should be checked, unless you are deliberately adding a second path between the same two tables.
Power BI often detects relationships automatically when column names match. Always confirm the result in Model view, because an auto-detected relationship can pick the wrong cardinality or direction.
Check the keys before you relate
A relationship fails or misbehaves if the one-side column contains duplicates or blanks. The many side can hold values that are missing from the dimension. Power BI groups those rows under a blank member in visuals.
Check for these orphan rows at the source before loading. This query assumes the DimProduct and FactSales tables from earlier in the tutorial:
SELECT COUNT(*) AS OrphanRows
FROM FactSales AS f
LEFT JOIN DimProduct AS p
ON f.ProductKey = p.ProductKey
WHERE p.ProductKey IS NULL;A result of 0 means every fact row finds its product.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Power BI offers many-to-many or one-to-one | The key on the dimension is not unique | Remove duplicates in Power Query or fix the key |
| A blank row appears in a slicer | Fact rows have keys that are missing from the dimension | Add the missing members or correct the source |
| Slicers do nothing | The relationship is inactive, or the data types differ | Activate it and make both columns the same type |
| Totals look wrong after a filter | The relationship was reversed, or the filter is bidirectional | Put the one side on the dimension and use single direction |
Verify the result
Create a card visual with a measure such as Total Sales = SUM ( Sales[SalesAmount] ). Then add a slicer on DimProduct[Category]. If the card changes when you select a category, the filter is flowing from the dimension through the relationship to the fact table. The next chapter covers that flow in detail.
Exercise
A fact table's ProductKey column has the value 99, but DimProduct has no row with that key. What happens to those rows in a visual that groups by product category, and where should you fix it?
Show answer
The rows are grouped under a blank category, so totals are still correct but a blank member appears. Fix it at the source or in Power Query by adding the missing product to the dimension, or by correcting the key in the fact data.