Sub topic 9
Alternative: a calculation group that switches the date role
This chapter assumes you know star schemas, one-to-many relationships, and how CALCULATE changes filter context. You should also have seen USERELATIONSHIP wrapped around a single measure. Here you will see how to avoid writing that wrapper for every measure.
The problem with one measure per date role
Suppose the model has Sales[OrderDate] as the active relationship to 'Date'[Date], and Sales[ShipDate] as an inactive one. For ship-date reporting you wrote this measure:
Sales Amount by Ship Date =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)That works for one measure. A real model has many: sales amount, quantity, margin, order count, and their year-to-date variants. A ship-date copy of each one doubles the measure list. Every change to a base measure must then be repeated in its copy.
The idea: one switch for all measures
A calculation group is a table with a single column, where each row is a calculation item. A calculation item is a DAX expression that rewrites whichever measure is being evaluated. Inside the expression, SELECTEDMEASURE () stands for that measure.
A calculation item can therefore apply the alternate relationship to any measure the user picks:
-- Calculation item "Ship Date" in a calculation group (for example created in Tabular Editor)
CALCULATE (
SELECTEDMEASURE (),
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)A second item named "Order Date" holds just SELECTEDMEASURE (). It uses the active relationship, so it needs no modifier.
A report author puts the calculation group column in a slicer or on a visual axis. Selecting "Ship Date" makes every measure in the visual use the ship-date relationship. Selecting "Order Date" returns to the default.
Creating the calculation group
Power BI Desktop has a calculation group feature in model view. Tabular Editor and other external tools also create them. Most steps are the same either way:
- Create a calculation group named
Date Role. Its column is usually namedDate Roletoo. - Add the item
Order Datewith the expressionSELECTEDMEASURE (). - Add the item
Ship Datewith theCALCULATEandUSERELATIONSHIPexpression above. - Add the group's column to a slicer or a visual, and use existing measures such as
[Sales Amount].
The existing [Sales Amount] measure now behaves differently depending on the selected item. You did not create a new measure.
Combining with time intelligence
The calculation item composes with measures that already contain time intelligence. Take a year-to-date measure:
Sales Amount YTD =
CALCULATE (
[Sales Amount],
DATESYTD ( 'Date'[Date] )
)With "Ship Date" selected, the item wraps this measure in USERELATIONSHIP. The DATESYTD filter on 'Date'[Date] then reaches Sales through the ship-date relationship. You get shipped sales year to date without a separate Shipped Sales YTD measure.
Things to watch
- Implicit measures stop working. Once a model has a calculation group, Power BI requires explicit measures. Dragging
Sales[SalesAmount]into a visual and picking Sum no longer works, because the calculation item cannot rewrite it. Define a measure such as[Sales Amount]instead. - No selection means no switch. If nothing is selected in the slicer, the calculation items do not apply, so measures use the active relationship (order date). Decide whether that default is acceptable, or set a single-select slicer.
- Multiple selections. If a user selects both items, the calculation group uses its default evaluation. Make the slicer single-select, or set the group to require a single selection.
- Precedence. When a model has several calculation groups, the
Precedenceproperty decides which item is applied first. Keep the date-role group simple to avoid surprises. - Authoring cost. Calculation groups are less visible than measures. A person reading a report may not realize a slicer changes the date logic, so label the slicer clearly, for example "Date role".
Comparing with the other approaches
| Approach | New measures needed | Both roles side by side in one visual | Model size |
|---|---|---|---|
| USERELATIONSHIP in each measure | One per measure and role | Yes, with separate measures | No change |
| Duplicated Date table | None | Yes | Extra table |
| Calculation group | None | Not by default, one role at a time | One small table |
The calculation group is the compact choice when users want to switch an entire report between order date and ship date. It is a weaker fit when a single visual must show sales by order date and by ship date together. A duplicated Date table or dedicated measures do that more directly.
Exercise
A model has the active relationship Sales[OrderDate] to 'Date'[Date] and an inactive relationship Sales[ShipDate] to 'Date'[Date]. You add a calculation group "Date Role" with the items "Order Date" (SELECTEDMEASURE ()) and "Ship Date" (SELECTEDMEASURE () wrapped in CALCULATE with USERELATIONSHIP on ShipDate). A user selects "Ship Date" and shows [Sales Amount YTD], which uses DATESYTD. Which date column determines the year-to-date window, and why?
Show answer
The ship date. The calculation item wraps [Sales Amount YTD] in CALCULATE with USERELATIONSHIP, so the ship-date relationship is active while the measure runs. The DATESYTD filter on 'Date'[Date] then propagates to Sales through Sales[ShipDate].