Sub topic 12
Role-playing dimensions: order date and ship date
What you need first
This chapter assumes you can create a relationship in the Model view. It also assumes you know the difference between an active and an inactive relationship, and that you understand one-to-many filtering from a dimension table to a fact table. You should be able to write a simple measure with CALCULATE.
One dimension, several roles
A role-playing dimension is a single dimension table that a fact table uses in more than one way. The standard example is a Date table and a Sales table. Each sales row has an order date, a ship date, and perhaps a due date. All three columns hold calendar dates, so all three should connect to the same Date table. The meaning of "date" changes with the column you follow:
Sales[OrderDate]answers "when was it ordered?"Sales[ShipDate]answers "when did it leave the warehouse?"
Keeping one Date table avoids building several calendars by hand. Year, quarter, month, and holiday flags are defined once and reused.
What the model looks like
You create one relationship per date column, each from Date[Date] (the one side) to the fact column (the many side). Power BI allows only one active relationship between two tables, so only one of the roles can be active. The others are inactive and appear as dashed lines in the Model view.
Power BI does this for you. If you create a second relationship between the same two tables, it marks the new one inactive. This is the ambiguity rule from the earlier chapter. If both were active, a filter on Date would have two paths to Sales, and the engine could not decide which one to use.
Which role should be active
Make active the role that most reports use by default. For sales analysis this is almost always the order date. Every visual and measure that does not say otherwise follows the active relationship. A chart of [Total Sales] by Date[Month] therefore shows sales by order month. Nothing in the chart tells you this, so label the visual or the measure clearly.
Option 1: activate the inactive relationship in a measure
USERELATIONSHIP tells CALCULATE to use a named inactive relationship for the duration of one calculation. 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 both measures in a table or chart with Date[Month] on the axis. [Total Sales] groups by order month and [Sales by Ship Date] groups by ship month, from the same Date table. A slicer on Date[Year] filters both measures, and each applies the year to its own role.
Some details to remember:
- The two columns can be listed in either order, because Power BI matches them to the existing relationship.
- The relationship must already exist in the model.
USERELATIONSHIPcannot create one. - It works with time intelligence. For a year-to-date total by ship date, pass the date function as another filter argument:
Sales YTD by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)- It applies only inside that
CALCULATE. Other measures in the same visual keep using the active relationship.
Option 2: one copy of the Date table per role
The second approach is to create a separate table for each role and give each its own active relationship. A calculated table is the simplest way:
Ship Date = 'Date'Now relate Ship Date[Date] to Sales[ShipDate]. Both relationships are active, because they join different pairs of tables. You do not need any special measures. [Total Sales] filtered by 'Ship Date'[Month] is sales by ship month.
Mark each copy as a date table if you use time intelligence functions. Hide the key columns you do not want report authors to use.
Choosing between the two options
| Question | Inactive relationship plus `USERELATIONSHIP` | Separate date tables |
|---|---|---|
| Model size and clutter | One date table | One table per role |
| Need an order-date slicer and a ship-date slicer at once | Hard, because slicers filter the one `Date` table | Easy, because each role has its own table |
| Order and ship dates on the same chart axis | Not possible; use measures side by side | Not possible on one axis, but both are available as filters |
| Report authors drag fields without writing DAX | Ship-date results need a dedicated measure per calculation | Works with any existing measure |
| Number of measures to maintain | One extra version per measure | None |
Use the measure approach when you have a few ship-date calculations and want a compact model. Use separate tables when users need to slice by several dates independently, or when you want ordinary measures to work in every role without duplicating them.
Edge cases and gotchas
- Blank ship dates. Orders that have not shipped have an empty
ShipDate. They match no row inDate, so they fall into the blank row. WithUSERELATIONSHIP, those orders disappear from any month and appear under a blank group. This is expected. Do not replace the blanks with a fake date like 1900-01-01 unless you also add that date to theDatetable and agree it has meaning. - Date range coverage. The
Datetable must cover every date in every role. A ship date after the last date in the table produces unmatched keys, so the totals do not add up to the grand total. - Data types. Relationships need matching types. If
OrderDatecarries a time of day, it will not match a pure date inDate[Date]. Convert it to a date in Power Query, or add a date-only column. - Totals with both roles in one visual. If you show
[Total Sales]and[Sales by Ship Date]side by side by month, the column totals match, because each measure covers all sales. Individual months can differ between the two. That is correct, and it often confuses report readers. - Measure names. Name role-specific measures after the role ("Sales by Ship Date"). A generic "Sales" next to an unlabeled ship-date version causes wrong readings.
Exercise
A model has one Date table with an active relationship to Sales[OrderDate] and an inactive relationship to Sales[ShipDate]. A report author puts Date[Month] on a chart axis with the measure [Total Sales] and expects ship-month results. What do they see, and what is the smallest change that gives them what they want?
Show answer
The chart shows sales by order month, because [Total Sales] follows the active OrderDate relationship. The smallest change is to use a measure that wraps [Total Sales] in CALCULATE with USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ), such as Sales by Ship Date. Alternatively, create a separate Ship Date table related to Sales[ShipDate] and use its month field on the axis.