Sub topic 15
Many-to-many relationships
This chapter assumes you know how to create a one-to-many relationship in the Model view. It also assumes you know what a fact table and a dimension table are, and how a filter travels from the "one" side to the "many" side. The next chapter covers the bridge table, which is the usual fix for the problems described here.
What a many-to-many relationship is
In a many-to-many relationship, neither column in the pair holds unique values. A value in the first table can match several rows in the second table. A value in the second table can also match several rows in the first.
Real data produces this shape in many places:
- Students and courses. A student takes several courses, and a course has several students.
- Products and promotions. A promotion covers several products, and a product can sit in several promotions.
- Budget and sales. A budget table has one row per month and region. A sales table has many rows for each month and region. Neither side is unique on the shared columns.
Power BI Desktop lets you create this relationship directly. In the Manage relationships dialog, or in the Edit relationship pane, set Cardinality to Many to many.
Why it is not the default
A one-to-many relationship works because the "one" side is a list of unique keys. Each fact row has exactly one parent row, so a filter on the dimension selects a clear set of fact rows.
When both sides repeat values, that guarantee is gone. Power BI cannot use a unique key to decide which rows belong together. It matches rows on the column value instead, and it cannot treat either table as a clean lookup.
Three effects follow:
- No blank row. A one-to-many relationship adds a blank row to the "one" side when fact rows have no matching key. A many-to-many relationship does not do this. Unmatched values simply disappear from visuals that use columns from the other table.
- No RELATED.
RELATEDfollows a relationship toward the "one" side. With both sides non-unique, it has no single row to return, so it is not valid across a many-to-many relationship. - Slower, less predictable results. The engine has less information to optimize with, and totals can look wrong when you do not expect them to.
How filters behave
A many-to-many relationship still has a filter direction. In the common case, the direction is single, from one table to the other. A filter on a column of the source table selects the distinct values of the matching column. Power BI then uses those values to filter the target table.
Take a small example. Budget has one row per Region and Month, and Sales has many rows per region and month.
| Table | Columns |
|---|---|
| Budget | Region, Month, BudgetAmount |
| Sales | Region, Month, SalesAmount |
Suppose you relate Budget[Region] to Sales[Region] as many-to-many. A slicer on Budget[Region] filters Sales correctly. A visual with Sales[Region] on its axis and [Total Budget] as a value shows the same budget total on every row, because a filter on Sales[Region] does not reach Budget unless the direction allows it.
Even when the direction is right, the relationship uses only Region. If you want the budget by month, the month must also filter correctly, and a relationship on one column cannot do that. This is the main weakness of the direct approach: a many-to-many relationship works on a single column pair.
A worked example of wrong totals
Consider two tables with a many-to-many relationship on Region.
Total Sales = SUM ( Sales[SalesAmount] )
Total Budget = SUM ( Budget[BudgetAmount] )Add a table visual with Budget[Month] as the row field, and both measures as values. The budget total is correct for each month, because Budget[Month] filters the Budget table directly. The sales total repeats the same figure on every row. Budget[Month] does not filter Sales, because the relationship is on Region, not Month.
This is a common pattern. The visual looks reasonable, and the numbers are not an error message. They are just wrong. The fix is to filter both tables through a shared dimension, which is the subject of the next chapter.
Sales Share of Budget =
DIVIDE ( [Total Sales], [Total Budget] )If either measure repeats a value across rows, this ratio is wrong for every row except the one where the repeated value happens to be correct.
Cardinality and filter direction combinations
The following table summarizes the combinations you can choose when you create a many-to-many relationship.
| Setting | Behavior |
|---|---|
| Many-to-many, single direction | A filter on the source table filters the target table through the shared column values. |
| Many-to-many, both directions | Each table filters the other. Use it with care, because it increases the chance of ambiguous paths and slower queries. |
The two options do not give the same answer in the same visual. If a visual mixes columns from both tables, the single-direction setting can leave one side unfiltered. The both-direction setting filters each side, but it can reduce totals you wanted to keep.
Referential integrity
In a one-to-many relationship, the engine knows that every fact row either matches one dimension row or belongs to the blank row. In a many-to-many relationship, there is no such guarantee. Rows with no match on the other side are not shown as blank. They are left out of the visual, and the total can be lower than the sum of the rows.
Check this with a card that shows [Total Sales] with no filters, and compare it with a table that lists [Total Sales] by Budget[Region]. If the table's grand total is lower, some sales rows have regions that do not exist in Budget.
When a direct many-to-many relationship is acceptable
Direct many-to-many relationships are acceptable in a narrow set of cases:
- Both tables are already summarized at the same grain, such as one fact table at month level and another at region level, and you only need to compare them through a single column.
- You are relating a fact table to a lookup table that you cannot change, and the lookup table has duplicate keys that you cannot remove.
- You need a quick prototype, and you accept that the totals may need checking.
In other cases, a different design is more reliable. The standard approach is a dimension table with unique keys that both fact tables relate to by one-to-many relationships. This is the "bridge" or "shared dimension" pattern from the next chapter.
Spotting a many-to-many relationship in the Model view
In the Model view, a many-to-many relationship appears as a line with an asterisk (*) at both ends. A one-to-many relationship has 1 at one end and * at the other. If you see * at both ends and did not choose that on purpose, Power BI may have detected duplicate values in a column you expected to be unique. Open the relationship and check the column values.
Duplicate Check =
COUNTROWS ( Customer ) - DISTINCTCOUNT ( Customer[CustomerKey] )Put this measure in a card. A result of 0 means the key is unique. Any other value is the number of extra rows that share a key.
Decision flow
Summary of risks
- A many-to-many relationship works on one column pair, so it cannot filter on a second column such as month.
- Unmatched rows are dropped instead of shown under a blank row.
RELATEDdoes not work across it.- Both-direction filtering on it increases the chance of ambiguous paths.
- Grand totals can differ from the sum of the visible rows.
Exercise
A Budget table (one row per Region and Month) and a Sales table (many rows per Region and Month) are joined by a many-to-many relationship on Region only. A table visual shows Budget[Month] in rows, with [Total Budget] and [Total Sales]. Total Budget differs by month, but Total Sales is the same on every row. Why, and what is the right fix?
Show answer
Budget[Month] filters only the Budget table. The relationship on Region does not carry the month filter to Sales, so Sales is not filtered by month and shows the same figure on every row. The fix is to relate both tables by one-to-many relationships to a shared dimension, such as a Date table and a Region table, and use the dimension columns in the visual.
Exercise
How can you tell in the Model view that a relationship is many-to-many, and how can you check whether a column you expected to be unique contains duplicates?
Show answer
A many-to-many relationship shows an asterisk at both ends of the line. To check for duplicates, compare COUNTROWS of the table with DISTINCTCOUNT of the key column. A difference of 0 means the key is unique.