Sub topic 5
The native many-to-many relationship
This chapter assumes you can use model view in Power BI Desktop. It also assumes you know fact and dimension tables, one-to-many relationships, cross-filter direction, and basic DAX such as SUM and CALCULATE.
What the option does
Since composite models arrived, Power BI has let you set a relationship's cardinality to Many-to-many. Neither column has to hold unique values. You can relate two tables on a column that repeats in both, and the engine matches rows by value.
Before this option, a one-to-many relationship required unique values on the "one" side. When both columns repeated, you had to build a separate table with unique keys first. That table is the bridge pattern, covered in the next chapter.
A worked example
Suppose a model has these tables:
Customershas one row per customer.AccountCustomerlists which customers own which accounts. An account can have several owners, and a customer can have several accounts. BothAccountIDandCustomerIDrepeat.Transactionshas one row per transaction, with anAccountIDcolumn that repeats.
Customers[CustomerID] to AccountCustomer[CustomerID] is a normal one-to-many relationship. The link between AccountCustomer[AccountID] and Transactions[AccountID] is the problem, because neither side is unique.
To create the link:
- In model view, drag
AccountCustomer[AccountID]ontoTransactions[AccountID]. - In the Edit relationship dialog, set Cardinality to Many-to-many.
- Set Cross-filter direction to Single, so
AccountCustomerfiltersTransactions. - Select Save.
A plain measure now responds to a customer slicer:
Total Amount = SUM ( Transactions[Amount] )Selecting a customer filters AccountCustomer to that customer's accounts. Those AccountID values then filter Transactions through the many-to-many relationship.
How it behaves internally
Power BI treats this relationship as weak. It does not assume one side has unique keys, so it cannot guarantee that each row matches exactly one row on the other side. Three things follow from that:
- Unmatched values do not create a blank row. A regular relationship adds a blank row to the "one" side when the many side has unmatched keys. A weak relationship does not, so unmatched transactions can drop out of visuals without any sign.
RELATEDhas no single row to fetch across the relationship, so it does not work there.- The engine does extra work to match values at query time. This can be slower on large tables than a one-to-many relationship on unique keys.
Common errors
- Using it by default. Many-to-many is easy to create, but it hides the duplicate keys that cause surprising totals. Check whether the repeated column is a real business many-to-many or a data-quality problem such as accidental duplicates in a dimension.
- Setting bidirectional filtering to make a visual work. This can create ambiguous paths with other relationships. Change direction only when you know which filter must travel.
- Expecting totals to add up. If an account has two owners, its amount appears under both customers. The sum of the rows then exceeds the grand total. Later chapters cover this double counting and how to detect it.
Exercise
Transactions[AccountID] and AccountCustomer[AccountID] both contain repeated values. Which cardinality setting do you use for a native relationship between them, and what happens to a transaction whose AccountID does not appear in AccountCustomer?
Show answer
Use Many-to-many. The relationship is weak, so no blank row is added for the unmatched transaction. That transaction is not attributed to any customer, so customer-level visuals leave it out and their sum can fall below the true total.