Sub topic 11
Switching relationships with USERELATIONSHIP
This chapter assumes you can write basic CALCULATE expressions and know how filters flow from a dimension table to a fact table across a one-to-many relationship. Here you will learn how to use a second, inactive relationship for a single calculation.
Why a model has inactive relationships
A fact table often has several date columns. A Sales table might record when an order was placed and when it shipped. Both columns can relate to the Date table, but DAX allows only one active relationship between two tables. The others are inactive. Filters do not flow through an inactive relationship.
| Relationship | Columns | State |
|---|---|---|
| Order date | `Sales[OrderDate]` to `'Date'[Date]` | Active |
| Ship date | `Sales[ShipDate]` to `'Date'[Date]` | Inactive |
With this model, a matrix that shows [Total Sales] by 'Date'[Year] groups sales by order date. The ship date has no effect on it.
Activating a relationship for one calculation
USERELATIONSHIP is a CALCULATE modifier. It takes the two columns that define the inactive relationship and activates it while that CALCULATE runs.
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)While [Total Sales] is evaluated, filters on Date reach Sales through ShipDate. The order-date relationship is deactivated for the same duration. Outside this CALCULATE, nothing changes.
Place [Total Sales] and [Sales by Ship Date] in the same matrix, grouped by 'Date'[Year]. Each year then shows sales ordered in that year next to sales shipped in that year. The two columns differ when orders ship in a later year.
The diagram below shows which relationship carries the filter in each measure.
How it fits the evaluation order
CALCULATE evaluates its filter arguments in the original context. It then applies the modifiers, which include USERELATIONSHIP, and applies the filters after that. The filters in the same CALCULATE therefore use the relationship you activated. This matters when you combine the modifier with a filter on Date:
Sales Shipped YTD =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)DATESYTD returns a table of dates from the start of the year. Those dates filter Sales through ShipDate, so the result is sales shipped year to date.
Rules and gotchas
- The two columns must match an existing relationship in the model, and the order of the arguments does not matter. If no such relationship exists, the measure returns an error.
- The relationship must exist but be inactive, or already be active. Naming an active relationship does nothing useful.
- The modifier affects everything evaluated inside that CALCULATE, including measures that you reference.
[Total Sales]itself needs no changes to use the ship date. - A variable evaluated before the CALCULATE keeps its original value. The next example looks like it switches the relationship, but it does not:
Wrong Sales by Ship Date =
VAR t = [Total Sales]
RETURN
CALCULATE ( t, USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) )The variable t is computed once, in the original context, using the order date. This is the same mistake as Wrong Red Sales earlier in the tutorial. Put the measure reference inside CALCULATE, as in Sales by Ship Date.
- If another CALCULATE is nested inside, its own modifiers can override the relationship choice for the inner expression.
When to use a different approach
USERELATIONSHIP suits a few measures that need an alternate date. If many measures need the ship date, you could create a separate Ship Date table with its own active relationship. That costs more model size and needs more setup. In return, users can slice by ship date directly, and your measures need no modifier.
Exercise
A model has an active relationship from Sales[OrderDate] to 'Date'[Date] and an inactive one from Sales[ShipDate] to 'Date'[Date]. A report slices by 'Date'[Year] = 2025. What does CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) ) return, and what does it return if you write VAR t = [Total Sales] RETURN CALCULATE ( t, USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) )?
Show answer
The first expression returns sales whose ShipDate falls in 2025, because the 2025 filter on Date flows through the activated ship-date relationship. The second returns sales whose OrderDate falls in 2025. The variable t was evaluated before CALCULATE ran, so the modifier cannot change it.