Sub topic 10
Using CROSSFILTER in measures
This chapter assumes you can read a Power BI model view, know the difference between fact and dimension tables, understand one-to-many relationships, and can write basic DAX with SUM and CALCULATE. It also assumes you have already built the bridge table from the previous chapters. Here you learn how CROSSFILTER changes filter flow for a single measure, and when that is better than changing the relationship itself.
The problem CROSSFILTER solves
In the sample model, Customers and Accounts are linked through a bridge table named AccountBridge. The bridge holds one row for each unique pair of AccountID and CustomerID. Transactions is the fact table and is related to Accounts on AccountID.
The relationships look like this:
Customers[CustomerID]toAccountBridge[CustomerID]: one-to-many, filters flow fromCustomersto the bridge.AccountBridge[AccountID]toAccounts[AccountID]: many-to-one, so by default filters flow fromAccountstoward the bridge, not the other way.Accounts[AccountID]toTransactions[AccountID]: one-to-many, filters flow fromAccountstoTransactions.
A slicer on Customers filters the bridge. The filter then stops, because the bridge-to-Accounts relationship has a single cross-filter direction that points the wrong way. A measure such as [Total Amount] ignores the customer selection and returns the same number for every customer.
Total Amount = SUM ( Transactions[Amount] )You can fix this by setting the bridge-to-Accounts relationship to filter in both directions. That change applies to the whole model and affects every visual and every measure. CROSSFILTER gives you the same effect inside one measure.
Syntax and behavior
CROSSFILTER takes two columns and a direction. It can only be used as a filter argument of CALCULATE (or CALCULATETABLE), and it changes the cross-filter direction of an existing relationship between those two columns for the duration of that calculation.
CROSSFILTER ( <columnName1>, <columnName2>, <direction> )The three parts:
- The two columns are the ends of a relationship that already exists in the model. If no relationship connects them, DAX returns an error.
- It does not matter which column you name first.
- The direction is one of
BOTH,ONEWAY, orNONE.
| Direction | Effect during the calculation |
|---|---|
| `BOTH` | Filters flow from either table to the other. |
| `ONEWAY` | Filters flow only in the relationship's normal one-to-many direction, from the "one" side to the "many" side. |
| `NONE` | The relationship is disabled, so no filter passes through it. |
The model setting is unchanged afterward. Other measures and visuals still use the direction saved in the model.
A bridge measure with CROSSFILTER
This measure turns on bidirectional filtering between the bridge and Accounts for one calculation. A selection on Customers now travels through the bridge to Accounts, and from there to Transactions.
Amount via Bridge =
CALCULATE (
SUM ( Transactions[Amount] ),
CROSSFILTER ( AccountBridge[AccountID], Accounts[AccountID], BOTH )
)Walk through how the filter moves when a report page shows a Customers[CustomerName] column next to this measure:
- Each row in the visual sets a filter on
Customers, for example one customer. - The relationship from
CustomerstoAccountBridgepasses that filter to the bridge. Only that customer's account pairs remain visible. CROSSFILTER ... BOTHlets the filter continue from the bridge toAccounts. Only the accounts linked to that customer remain.- The normal one-to-many relationship passes the filter from
AccountstoTransactions. SUMadds the amounts of the remaining transactions.
Why a measure and not the model setting
Setting the relationship to both directions in the model is simpler to write, but it has costs. You choose between them on these points:
| Consideration | Model setting: Both | `CROSSFILTER` in a measure |
|---|---|---|
| Scope | Every visual and measure uses it. | Only measures that include it. |
| Ambiguity risk | Can create several filter paths between tables, which Power BI may reject or resolve unpredictably. | Contained inside one calculation. |
| Performance | Filter propagation runs for all queries touching the path. | Cost is paid only where the measure is used. |
| Maintenance | One place to change. | Every measure that needs the behavior must repeat it. |
| Visibility | Visible in model view. | Hidden in the DAX, so reviewers must read measures. |
A reasonable default is to keep the model relationship single-direction and put CROSSFILTER in the few measures that need the bridge behavior. Move to the model setting only when many measures need it and you have confirmed there are no ambiguous paths.
Common errors
The measure returns the same value for every customer. The CROSSFILTER call names the wrong relationship, or it is missing. Check that the two columns are the ends of the bridge-to-Accounts relationship, not the Customers-to-bridge relationship, which already filters from Customers toward the bridge.
DAX error about a missing relationship. The two columns you named must be directly related. Naming Customers[CustomerID] and Accounts[AccountID] fails, because those tables are connected only through the bridge. Name the columns of one real relationship.
An inactive relationship is ignored. CROSSFILTER changes the direction of an active relationship. If the relationship is inactive, activate it with USERELATIONSHIP in the same CALCULATE instead. Do not assume CROSSFILTER will activate it for you.
A measure behaves differently under row-level security. CROSSFILTER does not work as expected on tables that have row-level security rules applied, and the call may be blocked or ignored there. Test measures with the roles your users have, not only as the model owner.
Results look too large in the Total row. This is the double counting covered in the previous chapter. CROSSFILTER makes the filter reach Transactions, but it cannot decide how to split an account shared by several customers. Each customer row shows the full account amount, so the total can exceed the true grand total. Validating this is covered later in the tutorial.
CROSSFILTER with other filter arguments
You can combine CROSSFILTER with ordinary filters in the same CALCULATE. All filter arguments are applied together before the expression is evaluated.
Amount via Bridge, Current Year =
CALCULATE (
SUM ( Transactions[Amount] ),
CROSSFILTER ( AccountBridge[AccountID], Accounts[AccountID], BOTH ),
YEAR ( Transactions[TransactionDate] ) = YEAR ( TODAY () )
)You can also use NONE to switch a relationship off. This is useful when a measure should ignore a customer selection and show an overall figure for comparison.
Amount Ignoring Customer =
CALCULATE (
SUM ( Transactions[Amount] ),
CROSSFILTER ( AccountBridge[AccountID], Accounts[AccountID], NONE )
)With the bridge-to-Accounts relationship set to NONE, the customer filter cannot pass beyond the bridge, so the result does not depend on which customers are selected.
How CROSSFILTER compares with TREATAS
The next chapter covers TREATAS, which reaches the same goal differently. The distinction matters when you decide how to write a measure:
CROSSFILTERreuses a relationship that exists in the model and changes how it filters.TREATAScreates a filter without any relationship, by applying a set of values to a column.
Use CROSSFILTER when the relationship is already in the model and you only need to change its direction for a measure.
Exercise
A report shows Customers[CustomerName] and [Total Amount]. Every customer shows the same number. The bridge-to-Accounts relationship is single-direction. Write a measure that shows each customer's own transactions without changing the model, and name which two columns you pass to CROSSFILTER and why.
Show answer
Use CALCULATE ( SUM ( Transactions[Amount] ), CROSSFILTER ( AccountBridge[AccountID], Accounts[AccountID], BOTH ) ). The two columns are the ends of the bridge-to-Accounts relationship, because that is the step where the customer filter stops. The Customers-to-bridge step already passes the filter, and Accounts-to-Transactions already filters in the right direction. Naming Customers[CustomerID] and Accounts[AccountID] would fail, since those tables have no direct relationship.
Exercise
What happens to the model's saved cross-filter direction after the measure Amount via Bridge runs, and which other measures are affected?
Show answer
Nothing changes in the model. CROSSFILTER applies only inside the CALCULATE that contains it, so other measures and visuals keep using the saved direction.