Sub topic 10
Using CROSSFILTER in measures
This chapter assumes you can build a basic Power BI model with a fact table and dimension tables. It also assumes you know one-to-many relationships and can write measures with SUM and CALCULATE. The model here has a bridge table, AccountBridge, between Customers and Accounts. Accounts filters Transactions.
The problem: the filter stops at the bridge
A bridge table holds unique pairs of keys. Each pair links one customer to one account. The relationships run from Customers to AccountBridge (one-to-many) and from Accounts to AccountBridge (one-to-many). Because the Accounts side is the "one" side, a filter normally flows from Accounts to the bridge. It does not flow from the bridge back to Accounts.
That means a selection on Customers reaches AccountBridge and stops. Transactions never sees it.
Start with a base measure:
Total Amount = SUM ( Transactions[Amount] )Put Customers[CustomerName] in a table visual next to [Total Amount]. Every row shows the same grand total, because the customer filter never reaches Transactions.
What CROSSFILTER does
CROSSFILTER changes the cross-filter direction of one relationship, but only while a CALCULATE evaluates. It takes three arguments:
- The column on one side of the relationship.
- The column on the other side.
- The direction:
BOTH,ONEWAY, orNONE.
The model setting stays as it is. Other measures and visuals are not affected.
Amount via Bridge =
CALCULATE (
SUM ( Transactions[Amount] ),
CROSSFILTER ( AccountBridge[AccountID], Accounts[AccountID], BOTH )
)The filter now takes this path:
A selection on a customer filters the bridge to that customer's accounts. The bidirectional step passes those accounts to Accounts. From there the filter reaches Transactions in the usual way.
Why use it instead of changing the model
You could set the relationship to bidirectional in the model. That affects every visual and measure. It can create ambiguous filter paths and slow down queries.
CROSSFILTER limits the change to the measures that need it. The trade-offs:
| Option | Scope | Risk |
|---|---|---|
| Bidirectional in the model | Every report item | Ambiguity, slower queries |
| `CROSSFILTER` in a measure | Only that measure | You must remember to use the right measure |
A common mistake: totals
Suppose two customers share one account. That account's transactions appear under both customers. The total row, with no customer filter, counts them once. The rows add up to more than the total.
This is not a bug in CROSSFILTER. The amounts really belong to both customers. Be sure your report readers know the rows are not additive. A later chapter covers detecting and handling this.
Other errors to watch for
- Wrong direction argument.
ONEWAYorNONEwill not push the filter back toAccounts, so the result looks like[Total Amount]again. - Naming the wrong columns. The columns must be the relationship's own key columns.
- Applying it to all measures. Use it only where the many-to-many filter is wanted.
Exercise
A report shows [Total Amount] and [Amount via Bridge] next to each customer. [Total Amount] repeats the same figure on every row, and [Amount via Bridge] differs by customer. Why? And why can the sum of the rows exceed the total?
Show answer
[Total Amount] has no path from Customers to Transactions, because the filter stops at the bridge. [Amount via Bridge] uses CROSSFILTER ... BOTH so the filter continues from the bridge to Accounts and Transactions. The rows can exceed the total because accounts shared by several customers are counted under each of them.