Sub topic 12
Role-Playing Dimensions: Order Date and Ship Date
This chapter assumes you can create relationships in the Model view, know that an active relationship is the one Power BI uses by default, and can write a simple measure.
What a role-playing dimension is
A role-playing dimension is one dimension table used for several purposes by the same fact table. The usual example is a Date table. A Sales row has an order date, a ship date and perhaps a due date. All three columns hold dates, so all three could relate to the same 'Date' table. Each relationship gives the Date table a different role.
Why Power BI allows only one active relationship
Power BI permits only one active relationship between two tables. If both Sales[OrderDate] and Sales[ShipDate] were active, a slicer on 'Date'[Year] would be ambiguous. It could mean orders placed in that year or orders shipped in that year. So you create several relationships, keep one active, and leave the others inactive. An inactive relationship appears as a dashed line in the Model view and filters nothing until a measure asks for it.
Setting it up
- In the Model view, drag
'Date'[Date]ontoSales[OrderDate]. This relationship is active by default. - Drag
'Date'[Date]ontoSales[ShipDate]. Power BI creates it as inactive because an active one already exists. - Open Manage relationships to check that exactly one is active, and that both are one-to-many from Date to Sales.
Using the inactive relationship in a measure
USERELATIONSHIP activates an inactive relationship for the duration of one CALCULATE. The active relationship is switched off for that calculation.
Total Sales = SUM ( Sales[SalesAmount] )
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)Put 'Date'[Month] on a chart's axis, and [Total Sales] groups sales by order month while [Sales by Ship Date] groups the same sales by ship month. The two measures can differ in any month where orders ship later than they were placed.
Common errors
- Both measures show the same numbers. The relationship you named may already be active, or the two dates always fall in the same period. Check the dashed and solid lines.
- An error that the relationship does not exist. The columns you passed to
USERELATIONSHIPmust match an existing relationship exactly, including which table each column belongs to. - Row-level filtering. A relationship activated this way applies only inside that
CALCULATE. Calculated columns and other measures still use the active relationship.
Exercise
A Sales table has OrderDate and ShipDate, each related to the Date table. A matrix shows [Total Sales] by 'Date'[Year] and returns order-year totals. Why, and how do you get ship-year totals?
Show answer
The OrderDate relationship is the active one, so Date filters Sales through it. Create a measure that wraps [Total Sales] in CALCULATE with USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ), and use that measure in the matrix.