Sub topic 7
Creating one-to-many relationships
This chapter assumes you can load data into Power BI Desktop, open the Model view, and read simple DAX. It also assumes you know what primary and foreign keys are. You should already have a fact table and dimension tables with the keys described in earlier chapters. Here you connect them.
What a relationship does
A relationship tells the model that a column in one table matches a column in another. Power BI uses it to carry filters between tables. When a user picks "Bikes" in a slicer on the product table, the relationship narrows the sales table to rows whose ProductKey belongs to a bike.
In a star schema, almost every relationship has the same shape:
- The one side is a dimension table. The key column has unique values.
- The many side is the fact table. The foreign key column repeats, because many fact rows share one dimension row.
One product can appear in thousands of order lines. That is one-to-many (written 1:*).
Cardinality options
Power BI offers these cardinality settings when you create a relationship.
| Cardinality | Meaning | Typical use in a star schema |
|---|---|---|
| One-to-many (1:*) | Unique key on the one side, repeating key on the many side | Dimension to fact. This is the default choice. |
| Many-to-one (*:1) | The same relationship, with the tables listed in the opposite order | Same as above. Only the order differs. |
| One-to-one (1:1) | Unique keys on both sides | Rare. Usually a sign the two tables should be merged. |
| Many-to-many (*:*) | Repeating values on both sides | Only for special cases, covered in a later chapter. |
Many-to-one and one-to-many describe the same thing. Power BI shows the "1" and "*" markers on the line in Model view, so read those to see which table is which.
The two conditions that must hold
A one-to-many relationship works only if the one side really is unique. If DimProduct[ProductKey] contains the same value twice, Power BI cannot create a valid one-to-many relationship. Depending on the data, it either rejects the relationship or offers many-to-many instead. Do not accept many-to-many as a way around the error. Fix the duplicate.
The second condition is that the data types match. Join integer to integer, text to text. A text key like "101" will not match the integer 101. Trailing spaces or different capitalization in text keys also break matches.
Creating the relationship
You can create a relationship in three ways.
- Drag and drop. In Model view, drag
ProductKeyfromFactSalesontoProductKeyinDimProduct. - Manage relationships. On the Home ribbon, choose Manage relationships, then New relationship. Pick the two tables, click the key column in each, and set cardinality and cross filter direction.
- Auto-detect. Power BI may create relationships when you load data, based on matching column names. Treat these as suggestions and check each one.
A good dialog setup for the product relationship looks like this:
- Table 1:
FactSales, columnProductKey - Table 2:
DimProduct, columnProductKey - Cardinality: Many to one (*:1)
- Cross filter direction: Single
- Make this relationship active: checked
Single direction filtering
Cross filter direction decides which way filters travel. With Single, filters travel from the one side to the many side: from dimension to fact. This is the setting you want for nearly every relationship in a star schema.
Both lets filters also travel from fact to dimension. It looks convenient, but it can create ambiguous filter paths and slow queries. A later chapter on anti-patterns covers it. For now, leave the direction on Single.
The relationship in the model
Here is the model from the earlier chapters, drawn as a star with the relationship direction shown.
The arrows show the direction in which filters travel. Both go into the fact table.
Active and inactive relationships
Between any two tables, only one relationship can be active at a time. The active one is used by default. Additional relationships between the same two tables are created as inactive, shown as dashed lines in Model view. A measure can switch one on for its own calculation with USERELATIONSHIP, which a later chapter on role-playing dimensions covers.
Power BI also refuses to make a relationship active if doing so would create two different filter paths between the same pair of tables. In a clean star schema this does not come up, because every path from a dimension to the fact table is direct.
Verifying the relationship
Do not assume the relationship works. Test it with a small table visual or a quick query.
Build a table visual with DimProduct[Category] and a measure:
Total Sales = SUM ( FactSales[SalesAmount] )If the relationship works, each category shows a different total, and the grand total equals the sum of the rows. Two symptoms signal trouble:
- The same number on every row. The measure ignores the category, which means there is no active relationship between the tables, or the column on the visual comes from an unrelated table.
- A row labeled blank. Fact rows exist whose key has no match in the dimension.
You can check for the second problem in DAX:
Orphan Rows =
COUNTROWS (
FILTER (
FactSales,
ISBLANK ( RELATED ( DimProduct[ProductName] ) )
)
)RELATED follows the relationship from the many side to the one side. If it returns blank for a row, that row has no matching dimension member. A nonzero result means you need to add the missing products to the dimension or correct the keys in the fact table.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "The cardinality you selected isn't valid for this relationship" | Duplicate values in the column you picked as the one side | Remove duplicates in Power Query, or choose the correct key column |
| Every row of a visual shows the same total | No active relationship, or the visual uses a column from an unrelated table | Check Model view and use columns from related dimensions |
| A blank row appears in a slicer or visual | Fact keys with no matching dimension row | Add the missing dimension rows or fix the keys. |
| Relationship cannot be activated | Another active path exists between the tables | Remove the extra path or keep it inactive |
| Keys fail to match | Different data types or whitespace | Convert both columns to the same type and trim text |
Hide the foreign keys
Once the relationship exists, users should filter with dimension attributes, not with the foreign key in the fact table. Hide FactSales[ProductKey] and FactSales[DateKey] from Report view. Slicing by a raw key bypasses the descriptive columns and leads to confusing reports. Hiding does not delete the column, and the relationship keeps working.
Exercise
A FactSales table has a CustomerKey column. You try to create a one-to-many relationship to DimCustomer[CustomerKey], but Power BI only offers many-to-many. What is the most likely cause, and how do you confirm and fix it?
Show answer
DimCustomer[CustomerKey] probably contains duplicate values, so it is not a valid one side. Confirm it by comparing the row count with the distinct count of the column, for example with COUNTROWS ( DimCustomer ) and DISTINCTCOUNT ( DimCustomer[CustomerKey] ). If they differ, remove the duplicates in Power Query (for example with Table.Distinct on the key, or by fixing the source), then create the relationship again. It should now be available as one-to-many with Single filter direction.
Exercise
You build a table visual with DimProduct[Category] and [Total Sales], and every category shows the same value, equal to the grand total. List two things to check.
Show answer
First, check in Model view that an active relationship connects DimProduct and FactSales. Second, check that the key columns have matching data types and values, and that the filter direction is Single from DimProduct to FactSales. Also confirm the visual uses DimProduct[Category] and not a column from a table with no relationship to the fact table.