Sub topic 15
Many-to-many relationships and bridge tables
This chapter assumes you can build a star schema in Power BI Desktop, create one-to-many relationships in Model view, and read simple DAX. It covers the case where a dimension and a fact table do not fit the one-to-many pattern, and how a bridge table resolves it.
Why one-to-many is not always enough
A star schema relies on each fact row pointing to exactly one row in each dimension. Many real situations break that rule:
- A bank account has several owners, and a customer has several accounts.
- A sales order is tagged with several promotions, and a promotion applies to many orders.
- A course has several instructors, and an instructor teaches several courses.
- A fact table is recorded at a coarser grain than a dimension, such as monthly budgets compared with daily sales.
In each case, one side of the relationship has repeated key values on both tables. Power BI cannot use a many-to-one filter path, because the key is not unique on either side.
Two ways to model many-to-many
Power BI offers two approaches. They produce similar visuals but behave differently.
| Approach | How it works | Strength | Weakness |
|---|---|---|---|
| Direct many-to-many relationship | Relates two tables on a column that has duplicates in both | Quick to create, no extra table | Weaker integrity, subtle totals, harder to reason about |
| Bridge table | A table of unique key pairs, with one-to-many relationships to each side | Explicit, predictable filtering, easy to inspect | One more table to build and maintain |
The bridge approach keeps every relationship one-to-many, so the filter rules you already know still apply. Prefer it unless you have a specific reason not to.
The bridge table pattern
A bridge table (also called a factless fact table or junction table) holds one row for each valid pair of keys. It stores no measures. Its only job is to record which members of one table relate to which members of another.
Take the account and customer example. FactTransactions has one row per transaction and an AccountKey. Each account can have several customers.
The diagram below shows the structure.
The bridge has a many-to-one relationship to each dimension. Together, the two relationships express a many-to-many link between customers and accounts.
Building the bridge
Suppose your source has one row per account and customer pair. This SQL creates the three tables involved.
CREATE TABLE DimCustomer (
CustomerKey INT PRIMARY KEY,
CustomerName VARCHAR(100) NOT NULL
);
CREATE TABLE DimAccount (
AccountKey INT PRIMARY KEY,
AccountName VARCHAR(100) NOT NULL
);
CREATE TABLE BridgeAccountCustomer (
AccountKey INT NOT NULL REFERENCES DimAccount (AccountKey),
CustomerKey INT NOT NULL REFERENCES DimCustomer (CustomerKey),
PRIMARY KEY (AccountKey, CustomerKey)
);If the pairs only exist inside a flat table, build the bridge in Power Query the same way you built a dimension: select the two key columns and remove duplicate rows.
let
Source = AccountOwnersFlat,
KeyColumns = Table.SelectColumns(Source, {"AccountKey", "CustomerKey"}),
BridgeAccountCustomer = Table.Distinct(KeyColumns)
in
BridgeAccountCustomerTable.Distinct without a column list removes rows that are duplicates across all selected columns. That makes each pair unique, which keeps the bridge at a clean grain of one row per account and customer.
Relationship direction is the catch
In a normal star, filters flow from the one side to the many side. Look at the bridge model:
- Filtering
DimCustomerflows to the bridge, because the bridge is on the many side. - But
DimAccountis on the one side of the bridge relationship, and the bridge sits between it and the customer. A customer filter reaches the bridge, and it must continue from the bridge up toDimAccount, then down toFactTransactions.
The step from the bridge to DimAccount goes against the one-to-many direction. By default, that filter does not pass. You have two options to fix it.
- Set the bridge-to-
DimAccountrelationship to filter in both directions. - Leave the relationship as it is, and write a measure that carries the filter with
CROSSFILTER.
The second option limits the bidirectional behavior to the measures that need it.
Total Amount = SUM ( FactTransactions[Amount] )
Amount by Customer =
CALCULATE (
[Total Amount],
CROSSFILTER ( BridgeAccountCustomer[AccountKey], DimAccount[AccountKey], BOTH )
)Use Amount by Customer in visuals that slice by customer. Visuals that slice by account only can keep using Total Amount, with no extra cost.
Totals do not add up, by design
A bridge model can double count. If an account has two owners, then a visual that lists customers shows the full account amount against each of them. The grand total counts the account once, but the sum of the visible rows counts it twice.
| Customer | Account | Amount |
|---|---|---|
| Ana | Joint account | 1,000 |
| Ben | Joint account | 1,000 |
| Total | 1,000 |
This is correct behavior. Each customer is associated with the full account, and the total does not repeat it. The problem arises when a reader adds the rows by hand and expects the total to match.
You can handle this in three ways:
- Explain it in the report, for example with a note on the visual that says amounts are not split between owners.
- Add a weighting factor column to the bridge, such as
AllocationPct, and use a weighted measure so that the rows add up. - Hide the customer breakdown when the total is the main goal.
A weighted measure uses SUMX to apply the allocation for each bridge row:
Allocated Amount =
SUMX (
BridgeAccountCustomer,
BridgeAccountCustomer[AllocationPct]
* CALCULATE ( [Total Amount], ALL ( BridgeAccountCustomer ) )
)This version is only correct when the visual filters to one account at a time. For arbitrary mixes of accounts and customers, compute the weighting at the fact table instead. That means adding a customer key and an allocated amount to the fact table at load time, which changes the grain to one row per transaction and customer.
Direct many-to-many relationships
Power BI lets you relate two tables on a column that has duplicates on both sides. Use this when the granularity is different but compatible, for example a budget table at the category level and a product table with many products in each category.
Consider the following guidelines for the direct approach:
- Check that the shared column is a real shared attribute, not an accident of matching names.
- Remember that blank values on either side can match many rows, so clean them first.
- Prefer a small shared dimension, such as a category table, which relates to both sides with one-to-many relationships. This is a bridge in disguise, and it is usually the better design.
The shared dimension option looks like this:
A slicer on DimCategory filters both the budget and the sales, so a single visual can compare them with no many-to-many relationship at all.
Common mistakes
- Making the bridge a dimension with extra attributes. Keep it to keys and, if needed, an allocation weight.
- Leaving duplicate pairs in the bridge. Duplicates inflate every amount. Verify with a row count against a distinct count of the key pair.
- Turning on bidirectional filtering across the whole model. This slows queries and can make results ambiguous.
- Using many-to-many when a cleaner grain change would work. If one fact row can be split by owner, split it and keep a pure star.
Choosing an approach
- If the many-to-many link is between a fact table and a dimension, and the amounts must be allocated, change the fact grain to include the second key.
- If the link is an unavoidable membership list, such as owners or tags, build a bridge and limit bidirectional filtering to one relationship or one measure.
- If two tables differ only in granularity, add a shared dimension at the coarser level.
- Use a direct many-to-many relationship only for quick analysis or prototypes where exact totals matter less.
Exercise
A FactSales table has one row per order line. Each order line can carry several promotions, and each promotion applies to many order lines. A report slices total sales by promotion, and the sum of the promotion rows is larger than the grand total. Is this a bug? Describe the model you would build, which relationships you would create, and where the bidirectional filter would be needed.
Show answer
It is not a bug. An order line with two promotions is counted once under each promotion, while the grand total counts it once. To model it, build a DimPromotion with unique promotion keys and a BridgeOrderLinePromotion with one row per order line and promotion pair. Because the fact grain is the order line, the bridge needs both the order number and the line number, or you add a single OrderLineKey column to the fact and the bridge. Create a one-to-many relationship from DimPromotion to the bridge and a one-to-many relationship from FactSales (through the order line key, which must be unique in a table on the one side, so a small order line dimension may be needed) to the bridge. The filter from a promotion flows to the bridge, and it then has to travel from the bridge back up to the order line table, which goes against the one-to-many direction. Set that single relationship to filter in both directions, or use CROSSFILTER inside a measure.