Sub topic 11
Switching relationships with USERELATIONSHIP
This chapter assumes you can write basic DAX measures and know how filters flow from the "one" side to the "many" side of a one-to-many relationship. It also assumes you know the basic CALCULATE behavior from earlier chapters: its filter arguments modify the filter context before the expression is evaluated.
Why a model needs more than one relationship
A fact table often has several date columns. A Sales table might hold OrderDate, ShipDate and DueDate. Each of these can relate to the same 'Date' table. This is called a role-playing dimension: one dimension table plays several roles.
Power BI and Analysis Services allow only one active relationship between two tables. Other relationships between the same two tables can exist, but they must be inactive. An inactive relationship is defined in the model but does not propagate filters unless a measure asks for it.
Suppose the model has these relationships:
| From (many side) | To (one side) | State |
|---|---|---|
| `Sales[OrderDate]` | `'Date'[Date]` | Active |
| `Sales[ShipDate]` | `'Date'[Date]` | Inactive |
A date slicer or a year on a matrix row filters Sales through OrderDate. Any measure that sums Sales[Amount] therefore reports sales by order date. The ship-date relationship exists, but nothing uses it.
What USERELATIONSHIP does
USERELATIONSHIP is a filter modifier. It can only appear as an argument of CALCULATE (or CALCULATETABLE). It takes two columns that define an existing relationship and activates that relationship for the duration of that CALCULATE call.
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)When this measure is evaluated in a cell where 'Date'[Year] is 2025, the filter on year still reaches the 'Date' table. Inside the CALCULATE, the filter then flows from 'Date' to Sales through ShipDate. The relationship on OrderDate is ignored for that evaluation. The result is the total of sales whose ship date falls in 2025.
Outside this measure, nothing changes. The model still uses OrderDate by default, and other measures are unaffected.
How the swap works
USERELATIONSHIP does not add a filter. It changes which relationship the engine follows when it propagates filters to Sales. The engine evaluates it before it applies the other filter arguments, so the filter arguments in the same CALCULATE use the relationship you activated.
When you activate the inactive relationship, the active one between the same two tables is turned off for that call. There is never more than one active relationship between two tables at a time.
Combining with other filter arguments
You can mix USERELATIONSHIP with other arguments. Common combinations use a time-intelligence function or a Boolean filter on the date table.
Sales Shipped in 2025 =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
'Date'[Year] = 2025
)The Boolean filter on 'Date'[Year] replaces any existing filter on that column, as you saw earlier in this tutorial. The filter is then propagated to Sales through ShipDate, because USERELATIONSHIP is in force. The order in which you list the arguments does not matter.
You can also compare two roles in one report by creating one measure per relationship.
Sales by Order Date =
[Total Sales]
Shipped Sales =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)
Shipping Lag Impact =
[Sales by Order Date] - [Shipped Sales]Put 'Date'[Year] and 'Date'[MonthName] on the rows of a matrix. Each cell shows the sales ordered in the period, the sales shipped in the period, and the difference. Sales ordered in December and shipped in January show up in both periods, which is exactly what the comparison should reveal.
Time intelligence with an inactive relationship
Time-intelligence functions such as TOTALYTD and DATESYTD work on the date table. They produce a table of dates, which then filters Sales through whichever relationship is active in that evaluation. To get a year-to-date total by ship date, add USERELATIONSHIP to the same CALCULATE.
Shipped Sales YTD =
CALCULATE (
[Total Sales],
DATESYTD ( 'Date'[Date] ),
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)DATESYTD returns dates from the 'Date' table, so it does not depend on which relationship is active. Those dates then filter Sales through ShipDate.
The shorthand TOTALYTD can do the same thing, but it is less readable when you need a relationship modifier, so the explicit CALCULATE form is a good default.
Rules and limits
- Both columns must define an existing relationship. If the model has no relationship between
Sales[ShipDate]and'Date'[Date], the measure returns an error.USERELATIONSHIPcannot create a relationship. - The relationship can be active or inactive. Naming the active relationship is allowed, and it simply has no effect. This is useful when you want a measure to state its intent explicitly.
- Only use it in
CALCULATEorCALCULATETABLE. It is not a standalone function that returns a value. - Argument order does not matter.
USERELATIONSHIP ( 'Date'[Date], Sales[ShipDate] )works the same as the version shown above. - Several modifiers may appear in one
CALCULATE. Each one activates a different relationship. You cannot activate two relationships between the same pair of tables in the same call, because only one can be active. - Row-level security can limit it. If row-level security (RLS) is defined on a table, the engine does not allow
USERELATIONSHIPto use a relationship that would let the role's filters flow in unexpected ways. Test measures with the role applied.
Interaction with other filter modifiers
USERELATIONSHIP works alongside the modifiers covered earlier in this chapter sequence.
- With
REMOVEFILTERS ( 'Date' ), the date filters are cleared. The chosen relationship then has no filter to propagate, so the measure returns the unfiltered total. - With
KEEPFILTERS, the new filter is intersected with the existing one on the same column, and then propagated through the activated relationship. - Inside a measure called from an iterator, context transition happens before the
CALCULATEfilter arguments. The row context on a table becomes a filter first, and thenUSERELATIONSHIPdetermines how filters from the'Date'table reachSales.
Common mistakes
- Putting the modifier in the wrong place. Writing
USERELATIONSHIPin aFILTERcall, or outsideCALCULATE, produces an error. It must be a direct argument ofCALCULATE. - Expecting a variable to follow the switch. A variable is evaluated once, in the context where it is defined.
VAR t = [Total Sales]followed byCALCULATE ( t, USERELATIONSHIP (...) )returns the order-date total. Put the measure reference inside theCALCULATEinstead. - Forgetting the relationship is inactive in the model. Developers sometimes try to use
USERELATIONSHIPfor a relationship that does not exist. Check the model view first. - Using it where a second date table is cleaner. If users need to slice by order date and ship date in the same visual, one date table cannot do both. Two copies of the date table, each with its own active relationship, let users filter both at once. The
USERELATIONSHIPapproach switches the role for a measure but cannot show two roles on the same axis. - Ignoring blank dates. If some
ShipDatevalues are blank, those rows do not match any date. They are excluded from every date-filtered result, but they still count in a grand total with no date filter.
Choosing between the approaches
| Need | Better choice |
|---|---|
| One report page needs a "by ship date" version of a few measures | `USERELATIONSHIP` in measures |
| Users must filter by order date and ship date together | Separate date tables, one per role |
| Many measures all need the ship-date role | Separate date table, or a calculation group that applies the modifier |
| A one-off analysis in a single measure | `USERELATIONSHIP` |
The measure approach keeps the model small, but each measure that needs the other role must repeat the modifier. The duplicate-table approach has a larger model, but report authors can use ordinary measures without any special handling.
Exercise
A model has an active relationship from Sales[OrderDate] to 'Date'[Date] and an inactive one from Sales[ShipDate] to 'Date'[Date]. A matrix shows 'Date'[Year] on rows. Write a measure that returns the total of Sales[Amount] by ship date, and explain why the measure Wrong Ship = VAR t = [Total Sales] RETURN CALCULATE ( t, USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) ) returns the same numbers as [Total Sales].
Show answer
Use CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) ). The Wrong Ship measure computes t once, before CALCULATE runs, in the original filter context where the active OrderDate relationship is in force. CALCULATE then evaluates only the stored value t, which cannot react to the relationship change, so the result equals [Total Sales].
Exercise
You need a single chart with order dates on the axis and two lines: sales by order date and sales by ship date, both plotted against the same axis. Does USERELATIONSHIP alone solve this? What does it do on each axis point?
Show answer
Yes for this layout. The axis shows 'Date'[Date] filtered by the active OrderDate relationship for the first line, and the ship-date measure uses USERELATIONSHIP to send that same date filter through ShipDate for the second line. At each axis point, the second line shows sales shipped on that date. It does not solve the case where users want to slice by order date and ship date as independent filters, which needs two date tables.