Sub topic 9
Alternative: a calculation group that switches the date role
This chapter assumes you can write CALCULATE measures and use USERELATIONSHIP. It also assumes you know that the Sales table has an active relationship on Sales[OrderDate] and inactive relationships on Sales[ShipDate] and Sales[DueDate], all pointing to 'Date'[Date]. If you don't, review the earlier chapters on inactive relationships first.
The measure-per-role approach works, but it scales badly. With 20 measures and 3 date roles, you maintain 60 measures. A calculation group moves the date-role choice out of the measures and into one selectable object. The user picks "Order Date" or "Ship Date" once, and every compatible measure follows.
The problem a calculation group solves
Here is what the measure-per-role approach looks like with only a few measures:
Sales Amount by Ship Date =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)You would repeat that wrapper for Order Quantity, Margin, Shipped Sales YTD, and every other measure. Each copy has to be kept in sync with the base measure. The only thing that changes between copies is the relationship being activated.
A calculation group lets you write that wrapper once. The group applies it to whichever measure is being evaluated.
What a calculation group is
A calculation group is a special table in the model with one text column and several calculation items. Each item is a DAX expression that modifies a measure. Inside the expression, SELECTEDMEASURE () stands for "whatever measure is being calculated right now".
Three facts matter for this chapter:
- The column holds the item names, such as "Order Date" and "Ship Date". You use it in a slicer, a matrix, or a filter like any other column.
- When an item is selected, its expression wraps each measure in the visual.
- When no item is selected, measures evaluate normally. For the date role, that means the active relationship (order date) applies.
You create calculation groups in Tabular Editor, or in the model view of recent Power BI Desktop versions. You can't create them with a DAX statement in a report. In Power BI Desktop, creating one turns on "Discourage implicit measures" for the model. After that, visuals can't auto-sum a column, so every value you show must come from an explicit measure.
Building the Date Role group
Create a calculation group named Date Role. Rename its column to Date Role. Add three calculation items.
The first item keeps the default behavior:
-- Calculation item "Order Date"
SELECTEDMEASURE ()The second item activates the ship-date relationship:
-- Calculation item "Ship Date"
CALCULATE (
SELECTEDMEASURE (),
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)The third item does the same for the due date:
-- Calculation item "Due Date"
CALCULATE (
SELECTEDMEASURE (),
USERELATIONSHIP ( Sales[DueDate], 'Date'[Date] )
)Set the item ordinals so the slicer lists Order Date, Ship Date, Due Date in that order. Without ordinals, items sort alphabetically.
You don't need to change your base measures. [Sales Amount] stays SUM ( Sales[SalesAmount] ).
How the evaluation flows
When a card shows [Sales Amount] with "Ship Date" selected, the engine substitutes the measure into the item's expression. The result behaves as if you had written Sales Amount by Ship Date. The relationship switch applies to everything inside the CALCULATE, so it also reaches measures that contain their own CALCULATE calls.
Time intelligence works unchanged
This is the main payoff. Take the year-to-date measure from the earlier chapter:
Shipped Sales YTD =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)With a calculation group you can write one date-role-neutral measure instead:
Sales Amount YTD =
CALCULATE (
[Sales Amount],
DATESYTD ( 'Date'[Date] )
)With "Order Date" selected, the year-to-date filter travels through the active order-date relationship. With "Ship Date" selected, the calculation item activates the ship-date relationship around the whole measure. DATESYTD filters the Date table, and that filter reaches Sales through the ship-date relationship. You get a ship-date running total without a second YTD measure.
The same holds for SAMEPERIODLASTYEAR, DATEADD, TOTALYTD, and similar functions. They filter the Date table, and the relationship decides which fact column that filter hits.
Testing with a query
Before building visuals, check the result in DAX query view, DAX Studio, or Tabular Editor. This query assumes your Date table has a Year column:
EVALUATE
SUMMARIZECOLUMNS (
'Date'[Year],
'Date Role'[Date Role],
"Sales", [Sales Amount]
)
ORDER BY 'Date'[Year], 'Date Role'[Date Role]You get one row per year and date role. Rows for different roles should differ when orders ship in a later year than they were placed. If every role returns the same number in every year, the relationship switch isn't taking effect. See the pitfalls below.
Showing all roles side by side
Putting Date Role on the columns of a matrix evaluates each item separately. The matrix shows order-date sales and ship-date sales next to each other for each month. That comparison needs separate measures in the measure-per-role approach and a duplicated Date table in the other approach. With a calculation group it takes one drag.
Limiting which measures change
A calculation item applies to every measure the visual evaluates. That is often wrong. Consider a measure such as [Customer Count] that does not depend on the date role. Under the "Ship Date" item it is still wrapped in USERELATIONSHIP. Whether that changes the number depends on the model, and the surprise is hard to trace.
Use ISSELECTEDMEASURE to restrict an item to the measures you intend:
-- Calculation item "Ship Date", restricted to listed measures
IF (
ISSELECTEDMEASURE ( [Sales Amount], [Order Quantity], [Sales Amount YTD] ),
CALCULATE (
SELECTEDMEASURE (),
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
),
SELECTEDMEASURE ()
)[Order Quantity] is a measure assumed to exist in this model. Replace the list with your own measures. Measures not on the list pass through unchanged, so they keep the active order-date behavior.
Maintain the list in each date-role item. If you add a new measure and forget to add it to the list, it silently ignores the slicer. That is a quiet failure, so document the list.
Pitfalls specific to calculation groups
Multiple items selected at once
If a slicer allows several items, the engine can't decide which date role applies. Results are confusing or the visual shows an error. Set the slicer to single select with "Select all" turned off, or filter to one item in a report-level filter.
Implicit measures
Calculation groups only work on explicit measures. If you drag Sales[SalesAmount] straight into a visual and let the report sum it, the calculation item has no SELECTEDMEASURE to wrap. Create [Sales Amount] as a measure and hide the raw column.
Combining with a measure that has its own USERELATIONSHIP
Suppose Shipped Sales YTD already contains USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ), and the user selects "Due Date". Two different relationship choices now apply to the same pair of columns, one outside and one inside. Do not rely on your memory of which wins. Test it, and avoid mixing the two approaches on the same measure. If you adopt a calculation group, remove the hard-coded USERELATIONSHIP from the measures it controls.
Calculation group precedence
When a model has more than one calculation group, such as a Date Role group and a Time Intelligence group, each has a precedence number. The group with the higher precedence is applied first, which makes it the outer layer. If the order is wrong, one group's items may not take effect as expected. Check the Precedence property when you add a second group.
Format strings
An item can override the format string, which is useful for items like "% of total". The date role items should not change formatting, so leave the format string expression empty and the measure keeps its own format.
The relationship must still exist
The item names a specific pair of columns. If someone deletes or recreates the Sales[ShipDate] relationship, USERELATIONSHIP fails with an invalid-argument error. A calculation item fails at query time, not when you save it, so run your test query after changing relationships.
Strengths and weaknesses
| Aspect | Calculation group | Measure per role |
|---|---|---|
| Number of measures to maintain | One group, regardless of measure count | One copy per measure per role |
| Works with new measures | Only if listed or unrestricted | Only if you write a copy |
| Side-by-side comparison | Matrix columns | Separate measures |
| Requires extra tooling | Tabular Editor or recent Power BI Desktop | None |
| Visible to report authors | A slicer that changes every number | Explicit measure names |
| Risk of misreading a report | Higher, since the role is a hidden state | Lower |
The last row matters. A measure named Sales Amount by Ship Date explains itself on the page. A slicer set to "Ship Date" is easy to overlook, especially in a screenshot or export.
When to choose it
Choose a calculation group when:
- Many measures need the same set of date roles.
- Users want to flip an entire report between order, ship, and due date.
- Your team has the tooling and the skill to maintain calculation groups.
Prefer plain USERELATIONSHIP measures when only a few measures need an alternate date, or when report authors in your organization can't use external tools. Prefer a duplicated Date table when users need to filter by two roles at the same time, for example orders placed in March that shipped in April. A calculation group selects one role per evaluation, so it can't do that.
Exercise
You have a Sales Amount measure and a Margin % measure, and a Date Role calculation group with items Order Date and Ship Date. A user selects Ship Date, but Margin % shows the same values as with Order Date. Name two likely causes and how you would check each.
Show answer
First, Margin % may not be in the ISSELECTEDMEASURE list of the Ship Date item, so it passes through as SELECTEDMEASURE () unchanged. Check the item expression and add the measure. Second, Margin % may contain its own USERELATIONSHIP, or the visual may use an implicit measure instead of the explicit one. Check the measure definition for a hard-coded relationship and confirm the visual uses the explicit measure. In both cases, run the SUMMARIZECOLUMNS test query with Date Role on rows and compare values by role.
Exercise
Write the calculation item for a "Due Date" role that applies only to [Sales Amount] and [Order Quantity] and leaves all other measures unchanged.
Show answer
IF ( ISSELECTEDMEASURE ( [Sales Amount], [Order Quantity] ), CALCULATE ( SELECTEDMEASURE (), USERELATIONSHIP ( Sales[DueDate], 'Date'[Date] ) ), SELECTEDMEASURE () )
Exercise
A report needs a table showing, for each month, the number of orders placed that month and the number of orders shipped that month, side by side. Which approach fits without extra measures, and what is the setup?
Show answer
A calculation group fits. Put 'Date'[Month] on the matrix rows, put the Date Role column on the matrix columns, and use one order-count measure. Each column evaluates the measure under its own item, so the Order Date column uses the active relationship and the Ship Date column uses the ship-date relationship.