Sub topic 4
Where Many-to-Many Comes From
This chapter assumes you can read Power BI Desktop's model view, know the difference between fact and dimension tables, and understand one-to-many relationships. It also assumes you can write basic DAX with SUM and CALCULATE. Here you will learn what makes a relationship many-to-many, the business situations that produce one, and how to tell a real one from a data-quality problem. The later chapters cover the fixes.
The rule that breaks
A one-to-many relationship depends on one guarantee. The "one" side column holds each value at most once. A dimension such as Customers has one row per CustomerID, so a filter on a customer selects exactly one row. That filter then travels to the "many" side, where every fact row with that key is selected.
A relationship is many-to-many when neither column can make that guarantee. A value in table A can match several rows in table B, and a value in table B can match several rows in table A.
| Cardinality | Duplicates in left key | Duplicates in right key | Typical use |
|---|---|---|---|
| One-to-many | No | Yes | Dimension to fact |
| One-to-one | No | No | Splitting one wide table in two |
| Many-to-many | Yes | Yes | Two tables that share a non-unique attribute |
The filter engine does not break when the guarantee fails. It still propagates filters, but one selected row can reach several rows on the other side. Aggregations then count the same facts more than once.
A running example
The tutorial uses a banking model with four tables:
Customers: one row per customer.Accounts: one row per account.Transactions: a fact table with one row per posted amount, keyed byAccountID.AccountCustomer: the source list that says which customer holds which account.
Joint accounts are the reason this model needs special handling. A couple can share one account, and each of them can also hold other accounts alone. Here is the AccountCustomer data:
| AccountID | CustomerID |
|---|---|
| A-100 | C-1 |
| A-100 | C-2 |
| A-200 | C-1 |
| A-300 | C-3 |
And here are the transactions:
| AccountID | Amount |
|---|---|
| A-100 | 500 |
| A-200 | 300 |
| A-300 | 200 |
The grand total of Transactions[Amount] is 1000. Customer C-1 holds A-100 and A-200, so C-1's amount is 800. C-2 shares A-100, so C-2's amount is 500. C-3's amount is 200. The three customer amounts add up to 1500, which is more than the 1000 in the fact table. Nothing is wrong with the data. Account A-100 genuinely belongs to two customers, so its 500 appears under both.
That is the many-to-many problem. A customer can hold many accounts, and an account can have many customers.
Common sources of many-to-many
Many-to-many relationships come from the business, not from modeling mistakes. These patterns appear in most industries:
- Shared ownership. Joint accounts, co-signed loans, and policies with several holders.
- Membership and enrollment. Students and courses, employees and projects, patients and care teams.
- Classification with multiple values. A product in several categories, an article with several tags, a film with several genres.
- Coverage and assignment. Sales territories shared by several reps, or a region served by several warehouses.
- Grain mismatch. Two fact tables recorded at different levels of detail. For example, sales are recorded by day and product, while a budget is recorded by month and category. Both share
Category, but neither side is unique.
The first four patterns are about entities. The grain mismatch is about the level of detail in the data. You solve it by making a dimension that sits at the shared level. For example, you could build a Category table with one row per category and relate both facts to it with one-to-many relationships. Treat this as the first thing to try. A many-to-many relationship that exists only because a dimension is missing should become a dimension.
Why a many-to-many link cannot sit in the middle of a star
In a star schema, filters flow from dimension to fact. Each fact row has one customer, one product, and one date, so a selection never selects the same fact row twice.
The Transactions table holds an AccountID and no CustomerID, because an account can have several customers. A transaction cannot belong to one customer. You can model the ownership with a table such as AccountCustomer, but that table is itself a many-to-many source. Its AccountID repeats for joint accounts, and its CustomerID repeats for customers with several accounts. Neither column works as the "one" side of a relationship.
If you try to create a one-to-many relationship from AccountCustomer[AccountID] to Accounts[AccountID], the direction is backwards. AccountCustomer is the "many" side there. If you try to relate Customers straight to Accounts on a shared key, Power BI cannot find a unique column on either end.
A duplicate key that is not a business rule
Not every duplicate is a business reality. Many "many-to-many" warnings come from dirty dimension data. When you create a relationship, Power BI checks the column values. If a column you expected to be unique contains duplicates, it proposes many-to-many or refuses a one-to-many.
Typical causes:
- A dimension was loaded from a history table, so a key appears once per version of the row.
- Two source systems were appended, and both contain the same key.
- Keys differ only by case or trailing spaces, which creates near-duplicates after a transformation.
- A blank key appears more than once.
Check these before choosing a modeling pattern. The following measure counts how many rows in Accounts exceed the number of distinct keys. A healthy dimension returns 0.
Duplicate Account Rows =
COUNTROWS ( Accounts ) - DISTINCTCOUNT ( Accounts[AccountID] )DISTINCTCOUNT counts a blank as one value, so a column with repeated blanks still shows excess rows. To find which keys are shared by several customers in the source list, use a calculated table:
Shared Accounts =
FILTER (
VALUES ( AccountCustomer[AccountID] ),
CALCULATE ( DISTINCTCOUNT ( AccountCustomer[CustomerID] ) ) > 1
)VALUES returns each distinct AccountID once. For each of those rows, CALCULATE converts the row into a filter (a context transition) and counts the customers. With the sample data above, the result has one row, A-100.
A test for a true many-to-many
Ask these questions in order:
- Is each key supposed to be unique on at least one side? If yes, and it is not, clean the data.
- Could a dimension at the shared level resolve it? If both tables share an attribute such as
Category, build the dimension, as the grain-mismatch pattern above describes. - Does the business really allow several matches in both directions? If yes, you have a true many-to-many relationship.
Question 3 is the one to verify with the data owner. A relationship that holds "in practice" today can change. If an account could only have one owner until last year, a one-to-many model may be correct for old data and wrong for new data.
What a true many-to-many costs you
Once you accept the relationship, three things follow, and later chapters address each of them:
- Filters reach too many rows. A customer selection now reaches every account of that customer, including joint accounts that other customers also hold.
- Totals stop adding up. The sum of customer amounts can exceed the fact total, as the 1500 against 1000 example shows. The amount is correct for each customer and still misleading in total.
- You must choose an implementation. Power BI offers a native many-to-many cardinality setting, and the standard alternative is a bridge table. They behave differently with filter direction and with totals.
The next chapters cover the native relationship and the bridge table in turn.
Exercise
A model has a Products table and a Sales fact table. A refresh adds a second row for product P-10 in Products, because the product was renamed and both versions were loaded. Power BI now suggests a many-to-many relationship on ProductID. Should you accept it? Which measure from this chapter would confirm the cause, adapted to this table?
Show answer
No. The business does not allow a product to have two identities, so this is a data-quality problem, not a true many-to-many relationship. Fix the duplicate row (keep one row per ProductID, for example the current name) and keep the one-to-many relationship. A check such as COUNTROWS ( Products ) - DISTINCTCOUNT ( Products[ProductID] ) returns 1 here. After the fix it returns 0.
Exercise
Using the sample data in this chapter, customer C-2 holds only the joint account A-100. If a report filters to customers C-1 and C-2 together and shows the sum of their amounts per customer, what are the per-customer values and their sum? What is the true total for those two customers' accounts?
Show answer
C-1 shows 800 and C-2 shows 500, so the sum of the rows is 1300. The accounts held by C-1 and C-2 together are A-100 and A-200, whose real total is 500 + 300 = 800. The 500 on joint account A-100 is counted once for C-1 and once for C-2, so the 1300 overstates the total by 500.