Sub topic 15
Many-to-many relationships and bridge tables
This chapter assumes you can create one-to-many relationships in the Model view. It also assumes you know how a filter moves from a dimension to a fact table, and that you can write simple DAX measures.
When a dimension does not match one-to-one
A star schema expects each fact row to point to exactly one dimension row. Some data breaks that rule. A joint bank account belongs to two customers, and a customer can hold several accounts. A product can be in several promotions, and a promotion covers several products.
Neither table has unique values in the other's key, so a direct one-to-many relationship is not possible.
The bridge table pattern
A bridge table resolves a many-to-many relationship into two one-to-many relationships. It has one row for each valid pair of keys. Its grain is one customer and one account.
CREATE TABLE BridgeCustomerAccount (
CustomerKey INT NOT NULL REFERENCES DimCustomer (CustomerKey),
AccountKey INT NOT NULL REFERENCES DimAccount (AccountKey),
PRIMARY KEY (CustomerKey, AccountKey)
);In the model, DimCustomer filters the bridge, and DimAccount also filters the bridge. The fact table, FactTransactions, relates to DimAccount as usual.
The filter direction problem
A filter on DimCustomer reaches the bridge. It cannot reach DimAccount, because that relationship points the other way, from DimAccount to the bridge. The fact table therefore stays unfiltered.
You have two options:
- Set the bridge-to-account relationship to bidirectional. This is simple, but it changes filtering for every measure and visual in the model.
- Turn on bidirectional filtering only inside a measure. This keeps the model's default behavior predictable.
The second option uses CROSSFILTER:
Transaction Amount by Customer =
CALCULATE (
SUM ( FactTransactions[Amount] ),
CROSSFILTER ( BridgeCustomerAccount[AccountKey], DimAccount[AccountKey], BOTH )
)With this measure, a visual that slices by customer name shows the transactions of every account that customer holds.
Double counting
A joint account's transactions appear under both owners. If you add up the customer rows, the sum is larger than the real total. The total row of a table visual is computed without a customer filter, so it shows the correct amount, and it will not match the sum of the rows above it.
Choose how to handle this:
- Accept it, and label the visual so readers know the rows overlap.
- Add a weighting factor column to the bridge, for example 0.5 for each of two owners. Multiply by it inside an iterator such as
SUMX. The rows then add up to the total.
Native many-to-many relationships
Power BI can also create a many-to-many relationship directly between two columns. Prefer a bridge table in most cases. A bridge makes the pairs visible and lets you control them. It also lets you add attributes, such as the weighting factor above.
Exercise
In the customer and account model, a slicer on DimCustomer does not change a card showing SUM(FactTransactions[Amount]). Why, and how can you fix it without changing the model's relationships?
Show answer
The filter from DimCustomer stops at the bridge table, because the bridge-to-DimAccount relationship points from DimAccount to the bridge. It never reaches FactTransactions. Wrap the aggregation in CALCULATE with CROSSFILTER on BridgeCustomerAccount[AccountKey] and DimAccount[AccountKey], set to BOTH.