Sub topic 6
Activating a relationship with USERELATIONSHIP
This chapter assumes you know star schemas, one-to-many relationships, SUM, and the basics of CALCULATE and filter context. It also assumes your model already has an inactive relationship from Sales[ShipDate] to 'Date'[Date], as the previous chapter set up.
The problem to solve
The Sales table has an OrderDate and a ShipDate. Both relate to the Date table, but only one relationship can be active. Here the OrderDate relationship is active, so a report that slices by 'Date'[Year] always shows sales by order date. The ShipDate relationship is inactive, which means it exists but filters nothing.
A base measure shows this behavior:
Sales Amount = SUM ( Sales[SalesAmount] )This measure follows the active relationship. Placed in a visual next to 'Date'[Year], it reports sales by order date.
What USERELATIONSHIP does
USERELATIONSHIP takes two column references: the column on the many side and the column on the one side of an existing relationship. It tells the engine to use that relationship instead of the active one, for the duration of one CALCULATE.
Sales Amount by Ship Date =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)Inside this CALCULATE, the ShipDate relationship is active and the OrderDate relationship between the same two tables is switched off. When the calculation ends, the model returns to its normal state. No relationship in the model is changed.
Rules for the arguments
- Both arguments must be plain columns, not expressions.
- The two columns must be the endpoints of an existing relationship in the model. If no such relationship exists, the measure returns an error.
- The order is not important in recent versions of DAX, but the convention is many side first, then one side.
- The relationship can be active or inactive. Naming an already active relationship is allowed and has no effect.
- You can list several
USERELATIONSHIPcalls in oneCALCULATE, each for a different pair of tables.
Where the filter comes from
USERELATIONSHIP does not filter anything by itself. It only chooses the path that filters take. Suppose a report has 'Date'[Year] on rows. For each year, the engine puts that year in the filter context of the Date table. The new relationship then carries that filter to Sales[ShipDate], so each row of the visual shows sales that shipped in that year.
This also means the measure depends on the filter reaching the Date table. A slicer on Sales[OrderDate] still filters by order date directly, because it filters a column in Sales and not through a relationship.
Using it with other filters
You can combine USERELATIONSHIP with other filter arguments. The relationship switch applies to the whole CALCULATE, including the other filter arguments, which is what makes time intelligence by ship date work. The next chapter covers this in detail. A preview:
Shipped Sales YTD =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)DATESYTD returns dates in the Date table. Because ShipDate is now the active path, those dates select rows by ship date.
Common errors
| Symptom | Likely cause |
|---|---|
| Error that a relationship cannot be found | The two columns are not the endpoints of any relationship. Check the table and column names, or create the relationship. |
| Same numbers as the base measure | The model's active relationship is already the one you named, or the visual uses a column from `Sales` instead of the Date table. |
| Blank rows or a total that looks too high | Some ship dates are blank or fall outside the Date table's range. Orders not yet shipped have no ship date, so they never appear. |
| Wrong result in a row-level security model | A `USERELATIONSHIP` measure may behave differently when security filters apply to the tables involved. Test it with the role applied. |
Quick check
Exercise
A model has an active relationship from Sales[OrderDate] to 'Date'[Date] and an inactive one from Sales[DueDate] to 'Date'[Date]. Write a measure named Due Sales that sums Sales[SalesAmount] by due date, and say what happens to the OrderDate relationship while the measure is evaluated.
Show answer
Due Sales = CALCULATE ( SUM ( Sales[SalesAmount] ), USERELATIONSHIP ( Sales[DueDate], 'Date'[Date] ) ). While the measure is evaluated, the DueDate relationship is active and the OrderDate relationship between the same two tables is not used. Both return to their normal states afterward, and the model itself is unchanged.