Sub topic 13
Role-playing dimensions such as order date and ship date
This chapter assumes you can create relationships in the Model view and write basic DAX measures. It also assumes you have already built a date dimension with a numeric DateKey.
What a role-playing dimension is
A fact table can reference the same dimension more than once. A sales row has an order date, a ship date, and often a due date. All three describe a point in time, so all three should use the same Date table. Each reference plays a different role.
Power BI allows only one active relationship between two tables. The active relationship is the one that filters by default. Any other relationship between the same pair of tables is inactive and shown as a dashed line.
Setting up the relationships
Give the fact table one key column per role, for example OrderDateKey and ShipDateKey. Both hold the same integer format as 'Date'[DateKey].
- Create a one-to-many relationship from
Date[DateKey]toSales[OrderDateKey]. Leave it active. - Create a second one-to-many relationship from
Date[DateKey]toSales[ShipDateKey]. Power BI makes it inactive, or you can clear the Make this relationship active box. - Keep both relationships single direction, from
DatetoSales.
Now Total Sales sliced by month uses the order date. Nothing else is needed for that role.
Activating the other role with a measure
USERELATIONSHIP switches to the inactive relationship for the duration of one CALCULATE.
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDateKey], 'Date'[DateKey] )
)Place Date[Month] on a matrix with [Total Sales] and [Sales by Ship Date]. The two columns differ because each measure reaches Sales through a different relationship. Orders placed on 30 January and shipped on 2 February count in January in the first column and in February in the second.
Common errors
- The measure returns the same number as the original. The relationship named in
USERELATIONSHIPmust already exist in the model. If it doesn't, the function raises an error. If the relationship is active, the measure matches the default. - Using it outside
CALCULATE.USERELATIONSHIPonly works as a filter argument ofCALCULATEorCALCULATETABLE. - Unshipped orders. If
ShipDateKeyis blank or missing fromDate, those rows fall into the blank member. Use a placeholder key and a matching row inDate, or accept the blank group. - Time intelligence on the wrong role. Functions such as
DATESYTDuse the active relationship. Wrap them inCALCULATEwithUSERELATIONSHIPto run them on ship date.
Alternative: one date table per role
You can instead load Date again as Ship Date, with its own active relationship. The trade-offs are:
| Approach | Strength | Cost |
|---|---|---|
| One `Date` plus `USERELATIONSHIP` | Smaller model, one table to maintain | A separate measure for each role |
| One date table per role | Both dates can be sliced at once with plain measures | More tables to keep in sync, and more field names to explain |
Choose measures when users want one calendar and a few ship-date metrics. Choose separate tables when users must filter by order month and ship month together.
Exercise
A Sales table has an active relationship to Date on OrderDateKey and an inactive one on ShipDateKey. A report page needs a slicer on order year and a card showing sales by ship date for the selected orders. Which measure pattern gives the card its value, and why does the slicer still work?
Show answer
Use CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[ShipDateKey], 'Date'[DateKey] ) ) for the card. The slicer filters Date, which reaches Sales through the active OrderDateKey relationship. Inside the measure, USERELATIONSHIP swaps that path for ShipDateKey. The slicer's Date filter then applies to ship date instead, so for this card the order-year slicer filters by ship year. To filter by order year and show ship-date sales together, you need a separate Ship Date table.