Sub topic 3
Year-to-Date by Ship Date in DAX
Prerequisites: You should understand fact and dimension tables, one-to-many relationships, simple measures, CALCULATE, and filter context.
Year-to-date by ship date
A Date table can filter Sales through OrderDate or ShipDate. If the ship-date relationship is inactive, activate it inside the measure. DATESYTD then filters the Date table for the year-to-date period.
Shipped Sales YTD =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)The visual can still use 'Date'[Date]. USERELATIONSHIP changes the relationship used by this calculation only. The same pattern works with other time-intelligence functions, such as DATESMTD, DATESQTD, and SAMEPERIODLASTYEAR.
Separate Date tables for each role
Another approach duplicates the dimension:
Ship Date = 'Date'Create an active relationship from Ship Date[Date] to Sales[ShipDate]. Users can then place order dates and ship dates in the same visual without changing relationships in measures. This is easy to understand, but it adds columns, relationships, and slicers to maintain.
Calculation groups
A calculation group can switch the date role for any selected measure. A calculation item named Ship Date can use this expression:
CALCULATE (
SELECTEDMEASURE (),
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)This avoids creating separate ship-date versions of every measure. Calculation groups usually require a modeling tool such as Tabular Editor and need clear testing because they affect many measures.
Pitfalls and unexpected totals
- Blank ship dates do not match a normal Date table row. Decide whether those sales should remain in a blank member or be excluded.
USERELATIONSHIPneeds columns from an existing relationship. It cannot repair a missing relationship or accept unrelated columns.- Use the Date table column in
DATESYTD, not the fact table date column. - Totals can differ from adding visible rows when the measure evaluates the full total filter context. Check the measure at the total level, not only row by row.
> callout warning > If a ship-date measure returns the same result as the order-date measure, check that the relationship is inactive, the arguments are reversed correctly, and the visual uses the intended Date column.
Choosing an approach
| Approach | Best when | Main cost |
|---|---|---|
| `USERELATIONSHIP` | A few measures need alternate dates | Measures need explicit logic |
| Separate Date tables | Users compare date roles directly | More model objects and slicers |
| Calculation group | Many measures need date-role switching | More advanced tooling and testing |
Exercise
Which function combines with USERELATIONSHIP to calculate shipped sales year to date?
Show answer
Use DATESYTD on the Date table inside the same CALCULATE expression.