Sub topic 5
The Native Many-to-Many Relationship
This chapter assumes you can work in Power BI Desktop's model view, know fact and dimension tables, understand one-to-many relationships and cross-filter direction, and can write basic SUM and CALCULATE measures. It covers how to create a native many-to-many relationship, how filters move across it, and where it causes surprises.
What "native" means
Power BI Desktop lets you set a relationship's cardinality to Many-to-many (\:\). You can then relate two tables on columns that both contain duplicate values. No table needs a unique key column, and no helper table is needed.
A regular one-to-many relationship has a "one" side. That side holds each key once, and Power BI relies on this. A native many-to-many relationship has no such side. Power BI filters one table by the distinct values that are visible in the other.
The examples use this model. Customers has one row per customer. Transactions has one row per posting. AccountCustomer lists which customers own which accounts, so one account can appear for several customers.
| Table | Columns | Rows |
|---|---|---|
| Customers | CustomerID, CustomerName | 1 Ann, 2 Bob |
| AccountCustomer | AccountID, CustomerID | A1/1, A1/2, A2/1 |
| Transactions | TransactionID, AccountID, Amount | T1 A1 100, T2 A1 50, T3 A2 200 |
Account A1 is shared by Ann and Bob. Account A2 belongs to Ann alone.
Creating the relationship
- In model view, drag
Transactions[AccountID]ontoAccountCustomer[AccountID]. - In the Edit relationship dialog, set Cardinality to Many to many.
- Set Cross-filter direction to Single, with
AccountCustomerfilteringTransactions. - Create a regular one-to-many relationship from
Customers[CustomerID]toAccountCustomer[CustomerID].
Desktop detects cardinality when you create a relationship. It picks many-to-many when neither column is unique. Check the result, because duplicates in a column you expected to be unique silently produce a many-to-many relationship. This is a common cause of confusing totals.
How filters travel
The path from a customer to an amount has two hops.
Suppose a slicer selects Ann. The engine takes these steps:
Customersis filtered to Ann.- The one-to-many relationship keeps the
AccountCustomerrows for customer 1, which are A1 and A2. - The many-to-many relationship collects the distinct
AccountIDvalues visible inAccountCustomerand keeps everyTransactionsrow whoseAccountIDis in that set.
Step 3 works on a set of values, not on row pairs. The engine does not match each AccountCustomer row to specific transactions. It only asks whether a transaction's AccountID appears in the visible set. This is why the result is correct for a slicer but can be wrong for totals, as shown below.
Cross-filter direction on a many-to-many relationship
Both directions are allowed.
| Direction | Effect | Typical use |
|---|---|---|
| Single | The table you pick filters the other. The reverse is not filtered. | Default choice. Easier to predict and faster. |
| Both | Each table filters the other. | Needed only when a slicer on the fact side must narrow the lookup table. |
Bidirectional filtering across a many-to-many relationship adds ambiguity. A second path between two tables can make Power BI reject the relationship or choose a path you did not expect. Keep the direction single unless a specific report requirement demands both.
Seeing the behavior with a measure
Define two measures on the model:
Total Amount = SUM ( Transactions[Amount] )
Customer Count = DISTINCTCOUNT ( Customers[CustomerID] )Run this in DAX query view to see the result with a grand total row:
EVALUATE
SUMMARIZECOLUMNS (
ROLLUPADDISSUBTOTAL ( Customers[CustomerName], "IsTotal" ),
"Amount", [Total Amount]
)
ORDER BY [IsTotal], Customers[CustomerName]The result is:
| CustomerName | Amount |
|---|---|
| Ann | 350 |
| Bob | 150 |
| Total | 350 |
Ann owns A1 (150) and A2 (200), which is 350. Bob owns A1 (150). The customer rows add up to 500, but the total is 350. The total row has no customer filter, so no AccountCustomer filtering happens, and the engine sums every transaction once. The 150 on A1 appears under both customers, so the visible rows overcount the true total.
Neither number is wrong. Each answers a different question. "Amount for Ann" and "Amount for Bob" are both correct, but they overlap, so you cannot add them. A measure that is correct per row but does not sum to its total is called non-additive. Later chapters cover how to detect and handle this.
Limitations and gotchas
- No
RELATEDacross the relationship.RELATEDandRELATEDTABLEneed a one side to follow. They do not work across a many-to-many relationship. UseLOOKUPVALUEor a measure withCALCULATEinstead, and keep in mind thatLOOKUPVALUEreturns blank when several rows match with different values. - No blank row for unmatched values. A regular one-to-many relationship adds a blank row on the one side for fact rows with no match, so they appear as
(Blank). A many-to-many relationship does not do this. A transaction whoseAccountIDis missing fromAccountCustomershows up in the unfiltered total but disappears as soon as a customer is selected. Nothing flags it. Check for unmatched keys yourself. - It is a weak relationship. Power BI treats a many-to-many relationship as weaker than a one-to-many one. The engine cannot assume uniqueness, so some optimizations are unavailable. It can be slower on large tables than a one-to-many relationship.
- Direction and ambiguity. Two filter paths between the same pair of tables are not allowed to be active together. Mark extra paths inactive, then activate them in a measure with
USERELATIONSHIPwhen needed. - Row-level security. Security filters follow relationship direction and can behave differently across a many-to-many relationship. Test any role against real user accounts before you rely on it.
When the native option fits
Use it when all of these hold:
- The two tables truly relate many-to-many on a shared column, such as a budget table at region level and a sales table at region level.
- Neither table needs a unique key, and adding one would be artificial.
- The report only needs filtering in one direction, and overlapping totals are understood by report users.
- The tables are moderate in size, or the report has been tested for speed.
Choose a bridge table instead when you need a clear dimension for each side, filtering from either side, allocation weights, or tight control of which filters flow where. The following chapters cover that pattern.
Quick checks before you publish
- Open the relationship and confirm the cardinality says what you intended.
- Confirm the direction is single unless you can name the report need for both.
- Compare a grand total against the sum of its rows. If they differ, find the overlap.
- Count fact rows whose key is missing from the other table, for example with
COUNTROWS ( EXCEPT ( VALUES ( Transactions[AccountID] ), VALUES ( AccountCustomer[AccountID] ) ) ).
The last check returns the number of distinct fact keys with no partner row. A non-zero result means some transactions will vanish when a customer filter is applied.
Exercise
Using the sample data in this chapter, Ann is selected in a slicer and the visual shows Total Amount. What value appears, and why does it differ from a visual that lists Ann and Bob on rows with a total at the bottom?
Show answer
With only Ann selected, the visual shows 350. Ann's accounts are A1 and A2, and the many-to-many relationship keeps every transaction whose AccountID is in that set, which is 100 + 50 + 200. In the table with Ann, Bob, and a total, the rows are 350 and 150, which sum to 500, but the total row shows 350. The total row has no customer filter, so each transaction counts once. The 150 on account A1 is shared by Ann and Bob, so it appears in both rows. The measure is non-additive.
Exercise
A transaction has AccountID A9, which does not exist in AccountCustomer. Where does its amount appear in a report that slices Total Amount by customer name, and where does it not?
Show answer
It is included in the grand total, because the total has no customer filter. It does not appear under any customer, and it does not appear under a (Blank) row, because a many-to-many relationship does not create a blank row for unmatched values. The amount is silently missing from the customer breakdown. You can find such keys by comparing the distinct AccountID values in Transactions against those in AccountCustomer.