Sub topic 15
Many-to-many relationships
This chapter assumes you can create a relationship in the Model view and that you know one-to-many cardinality and single cross-filter direction.
What makes a relationship many-to-many
A many-to-many relationship joins two columns that both contain repeated values. Neither column is a unique key. A one-to-many relationship always has a "one" side whose key is unique.
A common case is a Budget table with one row per Category and month, and a Product table with many products per Category. Category repeats in both tables, so neither table can be the "one" side.
How a filter behaves
A filter on one side selects its Category values. Power BI then keeps every row on the other side that has a matching value. Nothing is lost, but the result can surprise you.
Suppose a visual shows Product[ProductName] next to SUM ( Budget[Amount] ). Each product shows the budget for its entire category. The budget is not split by product. Adding the rows together counts each category budget once per product, so the grand total looks too large.
The relationship is behaving correctly. The Budget table simply has no product detail to filter.
Creating one
- Open Manage relationships and choose New.
- Select the two tables and the matching column in each.
- Set Cardinality to Many to many (:). Power BI may choose this for you when it detects duplicates on both sides.
- Check the cross-filter direction, then select Save.
Differences from one-to-many
- Power BI cannot guarantee a single matching row, so
RELATEDdoes not work across a many-to-many relationship.RELATEDTABLEis still possible but returns every matching row. - Totals can repeat or overstate, as in the budget example.
- The relationship does not follow the usual rules for a star schema, so it is harder to reason about when several such relationships exist.
When to use it
Use a direct many-to-many relationship when two tables are at different levels of detail. Typical cases are budgets by category and actuals by product, or when you must relate two fact tables. Keep the model simple and use it only when no better design is available.
The usual alternative is to add a small table of unique values, such as a Category dimension, and relate both tables to it with one-to-many relationships. The next chapter builds that bridge table.
Exercise
A Budget table and a Product table are related on Category with many-to-many cardinality. A table visual shows each Product name beside SUM(Budget[Amount]), and every product in the same category shows the same large number. Why does this happen, and what is the usual fix?
Show answer
Budget has no product-level detail, so the filter from Product only selects the category. Each product therefore returns the full budget of its category, and the grand total counts that budget once per product. The usual fix is to relate both tables to a separate Category dimension with one-to-many relationships. Then report budgets by category, not by product.