Sub topic 7
Building the Bridge Table
This chapter shows how to build a bridge table of unique key pairs from a source table that may contain duplicates. It assumes you can open Power BI Desktop model view, know the difference between a fact table and a dimension table, understand one-to-many relationships, and can write basic DAX with SUM and CALCULATE.
What a bridge table must contain
A bridge table sits between two dimensions that have a many-to-many link. In the running example, one customer can hold several accounts, and one account can be shared by several customers. The bridge holds one row for each valid pair of AccountID and CustomerID.
A usable bridge has three properties:
- It has one column for each key you want to connect, and nothing else, unless you add a weight column later.
- Each pair appears exactly once. The pair of columns acts as a composite key.
- Every value in each key column exists in the dimension it connects to.
The reason for the second property is that Power BI needs the "one" side of a one-to-many relationship to have unique values. The bridge is on the "many" side of both relationships. Each dimension is on the "one" side. A duplicate pair in the bridge does not break the relationships, but it makes row counts wrong and slows scans.
Where the pairs come from
The pairs usually come from an existing table. Common sources are:
- An account-to-customer ownership table exported from a core system.
- A join table in the source database, such as a student-to-course enrolment table.
- A fact table that records both keys on each row, such as a sales table with a customer and a promotion.
These sources often hold duplicates. An ownership table may have one row per period or per change in role, so the same pair repeats. This chapter's example is a source table named AccountCustomer with columns AccountID and CustomerID, and it may contain the same pair more than once.
Build the bridge with a calculated table in DAX
A calculated table is a table whose rows come from a DAX expression. Power BI recalculates it when the model refreshes. To create one, go to the Modeling tab and choose New table.
AccountBridge =
DISTINCT (
SELECTCOLUMNS (
AccountCustomer,
"AccountID", AccountCustomer[AccountID],
"CustomerID", AccountCustomer[CustomerID]
)
)The expression works in two steps:
- SELECTCOLUMNS returns a table with only the two named columns, one row for each row of AccountCustomer. Any other columns in the source are dropped.
- DISTINCT removes duplicate rows from that table, so each pair appears once.
The new column names are the quoted strings. Keep them identical to the dimension key names. This keeps the model readable and makes the relationships easy to spot.
Why SELECTCOLUMNS comes first
DISTINCT applied directly to a table returns unique rows across all columns of that table. If AccountCustomer has an extra column, such as a LoadDate, two rows with the same pair but different dates count as different rows. Selecting only the key columns first makes the uniqueness test apply to the pair alone.
Rows that point to nothing
A bridge can contain a key that is missing from a dimension. For example, AccountCustomer might list an AccountID that is not in the Accounts table. In the model, such a row relates to the blank member of the dimension. Reports then show a blank row, and totals appear that belong to no visible account. Check for this after you build the bridge. A DAX query in DAX query view can list the orphan keys:
EVALUATE
FILTER (
AccountBridge,
ISBLANK ( LOOKUPVALUE ( Accounts[AccountID], Accounts[AccountID], AccountBridge[AccountID] ) )
)An empty result means every bridge account exists in Accounts. Run the same check for CustomerID against Customers.
Build the bridge in Power Query instead
You can also build the bridge before the data loads, in Power Query. The steps are:
- Reference or duplicate the source query. Use Reference if you want the bridge to follow changes in the source query.
- Choose only the AccountID and CustomerID columns (Choose Columns, or Remove Other Columns).
- Select both columns and choose Remove Rows, then Remove Duplicates.
- Rename the query AccountBridge and load it.
Remove Duplicates, with both columns selected, compares the pair, so it matches what DISTINCT does in DAX.
Choosing between the two methods
| Question | Calculated table (DAX) | Power Query |
|---|---|---|
| Where does it run? | In the model, after data loads | During refresh, before the model is built |
| Does it use model memory? | Yes, stored like any other table | Yes, stored like any other table |
| Can it fold to the source? | No | Often, for a database source, so the database removes duplicates |
| Is it easy to inspect? | Visible in Data view | Visible in Power Query steps |
| Can it depend on model measures or relationships? | Yes | No |
For a large source from a database, Power Query is usually the better place because folding pushes the work to the server. For a small model, or when you already have the source loaded and want a quick result, a calculated table is simpler. In either case the result in the model is a normal table.
Check the data types
The key columns in the bridge must have the same data type as the matching columns in the dimensions. A text AccountID in one table and a whole-number AccountID in another produces relationship errors or an empty match. Whitespace and case differences also cause trouble. Text matching in relationships ignores case, but it does not ignore leading or trailing spaces. If you see keys that look equal but do not match, trim the text in Power Query before you build the bridge.
Hide the bridge from report authors
Report authors should filter by dimension columns, not by bridge columns. Selecting a key from the bridge directly, instead of from a dimension, can give confusing results. After you create the relationships in the next chapter, hide the bridge table, or at least its key columns, from report view. Right-click the table and choose Hide in report view. Hidden objects still work in measures and relationships.
Add a weight column when shares differ
Some bridges need more than the pair. If two customers share an account and one owns 70 percent, you can store that share in the bridge. The table then has three columns: AccountID, CustomerID, and a numeric column such as OwnershipShare. In that case, uniqueness applies to the pair, and the source must have one weight for each pair. A later chapter covers how to use the weight. For now, this is the design difference to note: the DISTINCT approach above removes duplicate rows only when the rows are identical, so if the same pair carries two different weights, both rows stay. You must resolve that in the source or aggregate it first.
Verify that the pairs are unique
Do not assume the bridge is clean. Add a quick check measure or run a query. This DAX query compares the number of rows to the number of distinct pairs:
EVALUATE
ROW (
"Rows", COUNTROWS ( AccountBridge ),
"Distinct pairs", COUNTROWS ( DISTINCT ( AccountBridge ) )
)The two numbers must be equal. If they are not, the bridge still has duplicates, often because of a hidden extra column or a weight column with different values.
A second check is the shape of the data. Count how many accounts each customer has, and how many customers each account has. If every account has exactly one customer, the relationship is really one-to-many, and you do not need a bridge at all. A bridge is justified only when both counts exceed one for at least some keys.
EVALUATE
ROW (
"Accounts with more than one customer",
COUNTROWS (
FILTER (
VALUES ( AccountBridge[AccountID] ),
CALCULATE ( COUNTROWS ( AccountBridge ) ) > 1
)
),
"Customers with more than one account",
COUNTROWS (
FILTER (
VALUES ( AccountBridge[CustomerID] ),
CALCULATE ( COUNTROWS ( AccountBridge ) ) > 1
)
)
)If the first number is zero, each account belongs to one customer, and a plain one-to-many relationship from Customers to Accounts is enough.
Common errors
- Duplicates survive. An extra column, such as a date or a status, makes rows differ. Select only the key columns before removing duplicates.
- The bridge has no matching dimension rows. Orphan keys create a blank member in reports. Run the orphan check.
- Types do not match. A text key against a number key prevents the relationship from working as intended. Convert the types first.
- The bridge is built from the fact table when it should not be. If you take pairs from fact rows, the bridge lists only pairs that have transactions. Accounts with no activity vanish from it. Build from an ownership table whenever one exists.
- A bridge is built where none is needed. Check the shape of the data first.
Exercise
Your source table AccountCustomer has columns AccountID, CustomerID and LoadDate. Rows (A1, C1, 2024-01-01) and (A1, C1, 2024-02-01) both exist. You write AccountBridge = DISTINCT ( AccountCustomer ). How many rows does the bridge hold for the pair A1 and C1, and how do you fix it?
Show answer
It holds two rows, because DISTINCT compares all columns and the LoadDate values differ. The bridge also keeps LoadDate as a column. Fix it by selecting only the key columns first: DISTINCT ( SELECTCOLUMNS ( AccountCustomer, "AccountID", AccountCustomer[AccountID], "CustomerID", AccountCustomer[CustomerID] ) ). The pair then appears once.
Exercise
You run the shape check and find that no account has more than one customer, while some customers have several accounts. Do you need a bridge table?
Show answer
No. The relationship is one-to-many from Customers to Accounts, which Power BI handles directly. Add a CustomerID column to the Accounts table and relate it to Customers. A bridge is needed only when both sides can have more than one match.