Sub topic 13
Role-playing dimensions such as order date and ship date
This chapter assumes you can load tables into Power BI Desktop, create relationships in Model view, and read simple DAX. You should also know what a fact table, a dimension table, and a one-to-many relationship are. The earlier chapters cover them.
What a role-playing dimension is
A fact table often has several columns that point to the same kind of thing. A sales row has an order date, a ship date, and perhaps a due date. All three are dates. All three should be sliced by year, month, and weekday.
A role-playing dimension is one dimension table that serves several of these roles. You keep a single Date table. The fact table holds one foreign key for each role: OrderDateKey, ShipDateKey, and so on. Each key joins to the same Date[DateKey] column.
The alternative is to copy the date table once per role. That works, but it creates extra tables to maintain. We compare the two approaches later in this chapter.
The rule: one active relationship between two tables
Power BI allows only one active relationship between any two tables. It lets you create more, but the extra ones are inactive. An inactive relationship appears as a dashed line in Model view. It does not filter anything until a measure turns it on.
The reason is ambiguity. If two active paths connected Date and Sales, a filter on Date[Year] would not say whether to filter by order year or ship year. Power BI avoids the problem by letting only one path be active.
The usual choice is to make the most common role active, normally the order date. Every visual, slicer, and measure that uses Date then filters by order date unless you say otherwise.
Setting up the model
Start with a fact table that carries two date keys. This is the table from the earlier chapters with a ship date added:
CREATE TABLE DimDate (
DateKey INT PRIMARY KEY,
FullDate DATE NOT NULL,
CalendarYear INT NOT NULL,
MonthNumber INT NOT NULL,
MonthName VARCHAR(10) NOT NULL
);
CREATE TABLE FactSales (
OrderNumber VARCHAR(20) NOT NULL,
OrderLineNumber INT NOT NULL,
OrderDateKey INT NOT NULL REFERENCES DimDate (DateKey),
ShipDateKey INT NOT NULL REFERENCES DimDate (DateKey),
ProductKey INT NOT NULL,
Quantity INT NOT NULL,
SalesAmount DECIMAL(18, 2) NOT NULL,
PRIMARY KEY (OrderNumber, OrderLineNumber)
);Both foreign keys reference the same DimDate table. That is the role-playing pattern in relational form.
In Power BI Desktop, the steps are:
- Create a date table, as in the date dimension chapter, and mark it as a date table.
- Create a relationship from
Date[DateKey]toSales[OrderDateKey]. Choose one-to-many, with a single filter direction fromDatetoSales. Leave it active. - Create a second relationship from
Date[DateKey]toSales[ShipDateKey]. Power BI sets it to inactive, because an active relationship already exists. If you are in the Properties pane, make sure Make this relationship active is cleared.
In Model view you now see one solid line and one dashed line between Date and Sales.
Activating the other role with USERELATIONSHIP
The USERELATIONSHIP function tells a measure to use an inactive relationship in place of the active one, for the duration of that measure only. You pass it the two columns that the relationship joins.
Total Sales = SUM ( Sales[SalesAmount] )
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDateKey], 'Date'[DateKey] )
)Total Sales follows the active relationship, so it reports by order date. Sales by Ship Date reuses it and switches the path. Put Date[Month] on a matrix with both measures, and each month shows what was ordered in that month beside what shipped in that month.
You can write the same pattern for any other role:
Sales by Due Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[DueDateKey], 'Date'[DateKey] )
)This assumes Sales also has a DueDateKey column with an inactive relationship to Date.
How the filter path changes
The diagram shows the two paths. Only one is live at a time for any given measure.
When the measure runs, USERELATIONSHIP deactivates the active relationship for that calculation, and the named one takes its place. Power BI does not allow two active relationships between the same tables at once, so the swap is automatic. The selected rows in Date then filter Sales through ShipDateKey.
Gotchas and common errors
- Time intelligence on the inactive role. Functions such as
TOTALYTDandDATESYTDwork on the date table, and they follow whichever relationship is live. To get a year-to-date total by ship date, nest them inside the sameCALCULATEasUSERELATIONSHIP. - Visuals that use columns directly. A bar chart that sums
Sales[SalesAmount]byDate[Month]uses the active relationship only. Only measures withUSERELATIONSHIPcan use the others. For this reason, expose measures to report authors and hide the raw fact columns. - Inactive relationships on a one-sided key.
USERELATIONSHIPneeds a defined relationship. If you call it with columns that have no relationship in the model, DAX returns an error. - Blank keys. If some orders have not shipped,
ShipDateKeyhas no valid date. Use a dummy key such as-1with a matching row inDate, or accept that those rows are blank under the ship-date measure. Make the choice on purpose, because it changes totals. - Confusing the roles in a visual. Two measures on one axis can mislead readers. Name measures with the role, as in
Sales by Ship Date, so the label tells them which date applies.
Alternative: separate date tables per role
Instead of using USERELATIONSHIP, you can load or create a second date table named Ship Date and relate it actively to ShipDateKey. You can build it by duplicating the calculated table or by referencing the original query in Power Query.
| Approach | Strength | Weakness |
|---|---|---|
| One `Date` table with `USERELATIONSHIP` | One table to maintain. Small model. | Report authors need measures for each role. Slicers cannot filter both roles independently. |
| One date table per role | Each role has its own slicer and can be combined with the others. Plain visuals work with no special measures. | More tables. Field lists are longer. Changes to the date table must be repeated. |
Choose separate tables when users need to filter by order date and ship date at the same time, for example "orders placed in January that shipped in February". A single Date table cannot hold two independent filters through a single measure without extra DAX. Choose the single table when most reports use one role and the others are occasional.
If you duplicate, give the copy distinctive column names or table names such as Ship Date, so field lists do not show two columns both called Month.
Checking your work
Create a small matrix with Date[MonthName] in the rows, and Total Sales and Sales by Ship Date as values. Then compare the grand totals. They should match, provided every order has a ship date that exists in the date table. A mismatch points to blank or out-of-range ship keys, which is the first thing to check.
Exercise
Your Sales table has OrderDateKey (active relationship to Date) and ShipDateKey (inactive). A report author drags Date[Month] and Sales[SalesAmount] into a chart and says the numbers are order-based, but they want a ship-based version of the same chart. What should you give them, and why can't they get it by changing the chart fields?
Show answer
Give them a measure such as Sales by Ship Date = CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[ShipDateKey], 'Date'[DateKey] ) ). A chart that uses the raw column Sales[SalesAmount] with Date[Month] always follows the single active relationship, which is the order date. Only a measure can switch to the inactive relationship for its own calculation.
Exercise
Users want a report page with one slicer for order month and another for ship month, applied together. Which design fits better, a single Date table with USERELATIONSHIP or a separate Ship Date table, and why?
Show answer
A separate Ship Date table. Each date table has its own active relationship to Sales, so each slicer filters its own key, and both filters apply at once. One Date table with USERELATIONSHIP can only use one path per measure, so it cannot apply two independent date filters to the same fact table without extra DAX.