Sub topic 16
Resolving many-to-many with a bridge table
This chapter assumes you can create relationships in the Model view, that you know one-to-many cardinality and single versus both cross-filter direction, and that you have seen the native many-to-many cardinality option. Here you build the classic alternative, a bridge table, and learn when it beats the native option.
The problem a bridge solves
Two tables have a many-to-many link when each row on either side can match several rows on the other. In this chapter, a product can appear in several promotions, and a promotion covers several products.
Neither table has unique values for the other's key. Power BI can't create a normal one-to-many relationship between them, because a one-to-many relationship needs a column with unique values on the "one" side.
A bridge table (also called a junction or linking table) splits the many-to-many link into two one-to-many links. It has one row for each valid pairing of the two keys.
The sample model
The model has four tables.
| Table | Role | Key columns |
|---|---|---|
| Sales | Fact table | ProductKey, SalesAmount |
| Product | Dimension | ProductKey (unique), Name |
| Promotion | Dimension | PromotionKey (unique), PromotionName |
| ProductPromotion | Bridge | ProductKey, PromotionKey |
The bridge holds one row per pairing:
| ProductKey | PromotionKey |
|---|---|
| P1 | Spring |
| P1 | Clearance |
| P2 | Spring |
Sales has P1 at 100 and P2 at 50.
Building the bridge
- Load or create a table with only the two key columns.
- In Power Query, remove duplicate rows so each pairing appears once. Duplicates are harmless to the totals here, but they waste memory and hide data problems.
- Confirm that the key columns have the same data type as the dimension keys.
- In the Model view, create Promotion[PromotionKey] to ProductPromotion[PromotionKey] and Product[ProductKey] to ProductPromotion[ProductKey]. Both are one-to-many, with the dimension on the "one" side.
Check that the arrows point from each dimension toward the bridge. The bridge is always on the many side, so it never needs a unique key.
Why a bridge alone doesn't filter correctly
A filter on Promotion flows to the bridge, because that relationship filters from the one side to the many side. The next step is from the bridge to Product. That is a many-to-one relationship, and a single-direction relationship doesn't filter that way. The filter stops at the bridge.
If you put PromotionName on a table visual next to a Sales measure, every promotion shows the same total. The Sales table never receives the promotion filter.
You have two ways to push the filter through.
- Set the cross-filter direction of the Product-to-bridge relationship to Both. This is simple, but it applies to every visual and measure, and it can create ambiguous paths or slow queries.
- Leave the model single-direction and turn on both-direction filtering inside one measure with CROSSFILTER. The effect stays limited to that measure.
A measure with CROSSFILTER
Total Sales = SUM ( Sales[SalesAmount] )
Sales by Promotion =
CALCULATE (
[Total Sales],
CROSSFILTER ( ProductPromotion[ProductKey], Product[ProductKey], Both )
)Inside CALCULATE, the relationship between the bridge and Product filters in both directions. A promotion filter now reaches Product, and from Product it reaches Sales through the normal one-to-many relationship.
You can also use TREATAS to avoid changing the model's behavior:
Sales by Promotion (TREATAS) =
CALCULATE (
[Total Sales],
TREATAS ( VALUES ( ProductPromotion[ProductKey] ), Product[ProductKey] )
)Both measures read the bridge rows left visible by the current Promotion filter, then apply their product keys to Product. They give the same result here.
Reading the totals
Put PromotionName and the measure in a table visual.
| PromotionName | Sales by Promotion |
|---|---|
| Clearance | 100 |
| Spring | 150 |
| Total | 150 |
The rows add up to 250, but the total row shows 150. This is correct. P1 is in two promotions, so its 100 counts under each one. The total row has no promotion filter, so it counts each sale once.
Users often read this as a bug. A callout in the report, or a note in the measure description, saves support questions.
Bridge or native many-to-many?
Power BI can also relate two tables directly with a many-to-many cardinality. Compare the two approaches.
| Question | Bridge table | Native many-to-many |
|---|---|---|
| Where the pairings live | In a visible table you control | Implied by matching values in the key columns |
| Model clarity | Each relationship is one-to-many | Both sides can contain duplicates |
| Filter direction | You choose per relationship or per measure | Defaults to both directions, which you can restrict |
| Unmatched keys | Visible by inspecting the bridge | Blank rows appear without a clear source |
| Setup effort | Needs an extra table | Needs no extra table |
Choose a bridge when the pairing is real data, such as promotion eligibility, or when you want one-to-many relationships everywhere. Native many-to-many fits cases where both tables are already at a granularity you can't change and a quick answer is enough.
Edge cases
- Missing keys. If the bridge has a ProductKey that doesn't exist in Product, the relationship adds a blank member to Product. Sales for those keys group under (Blank). Fix the data, or filter the bridge in Power Query.
- Products with no promotion. They have no bridge rows. They don't appear under any promotion, but they are included in the total row.
- Both-direction relationships. If you set Both on a relationship in the model, check for ambiguous paths. A second route from Promotion to Sales, such as through a date table, makes Power BI refuse or deactivate one of the relationships.
- Bridge with attributes. If the pairing has its own values, such as a discount percentage, keep them as columns in the bridge. Don't sum them across the bridge without thinking about which rows are visible.
- Row-level security. A both-direction relationship can let a filter on one table leak into another. Test roles after enabling it.
Checklist for diagnosing a broken bridge
- Does each dimension key have unique values? Test with
COUNTROWSagainstDISTINCTCOUNT. - Do the arrows point from the dimensions toward the bridge?
- Does the measure send the filter from the bridge to the fact table's dimension, either through the relationship setting or CROSSFILTER?
- Does a blank row appear in a dimension visual? If so, find the bridge keys that have no match.
Exercise
Your model has Student and Course dimensions and an Enrollment bridge. A card measure, Total Credits = SUM(Course[Credits]), is sliced by Student[Name]. Each student shows the total credits for all courses. What is wrong, and how do you fix it without setting the whole model to Both?
Show answer
The filter from Student reaches the Enrollment bridge, but it can't travel from the bridge to Course because that relationship is many-to-one and set to single direction. Wrap the measure in CALCULATE with CROSSFILTER ( Enrollment[CourseKey], Course[CourseKey], Both ), or use TREATAS with VALUES ( Enrollment[CourseKey] ) applied to Course[CourseKey]. Either approach applies the bridge filter to Course for this measure only.