Implement and Manage Semantic Models · Topic 4 of 6
Calculation Groups, Format Strings, Parameters
Build calculation groups with SELECTEDMEASURE, dynamic format strings that keep values numeric, and field parameters that switch measures or columns.
- 5 min
- Calculation items use
SELECTEDMEASURE()and work only with explicit measures. - Adding a calculation group turns on Discourage implicit measures.
- Precedence orders multiple calculation groups; each item can have its own format string expression.
What it is
Three model features reduce the number of measures and make reports more flexible.
- A calculation group applies the same logic, such as year to date or prior year, to any measure.
- A dynamic format string changes how a value is displayed, such as currency symbol or K and M suffixes, while the value stays a number.
- A field parameter lets report users choose which measure or column a visual shows.
How it works
Calculation groups
A calculation group is a special table with one column. Each row is a calculation item, a DAX expression that uses SELECTEDMEASURE() to stand for whatever measure the visual is showing.
-- Calculation group: Time Intelligence
Current = SELECTEDMEASURE ()
YTD = CALCULATE ( SELECTEDMEASURE (), DATESYTD ( 'Date'[Date] ) )
PY = CALCULATE ( SELECTEDMEASURE (), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
YoY % =
VAR CurrentValue = SELECTEDMEASURE ()
VAR PriorValue =
CALCULATE ( SELECTEDMEASURE (), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN
DIVIDE ( CurrentValue - PriorValue, PriorValue )
With three base measures (Sales, Cost, Margin) and four items, users get twelve results while you maintain only seven definitions.
You create calculation groups in Power BI Desktop's Model view or with an external tool such as Tabular Editor. Key details:
| Detail | What to know |
|---|---|
| Explicit measures only | Calculation items apply to measures, so Desktop turns on Discourage implicit measures when you add a calculation group |
| Format string expression | Each item can set its own format, such as "0.0%" for YoY % |
| Precedence | When several calculation groups apply, the precedence number decides the order |
ISSELECTEDMEASURE and SELECTEDMEASURENAME | Let an item treat some measures differently, for example skip YTD for a ratio |
Dynamic format strings for measures
Set a measure's format to Dynamic and write a DAX expression that returns a format string. The value stays numeric, so it still sorts, totals and plots correctly.
-- Format string expression for the measure [Sales in Currency]
SELECTEDVALUE (
'Currency'[Format String],
"\$#,0.00;(\$#,0.00);\$#,0.00"
)
The same idea works in a calculation item's format string expression, where SELECTEDMEASURE() gives the value being formatted:
SWITCH (
TRUE (),
SELECTEDMEASURE () >= 1000000, "#,0,,.0\M",
SELECTEDMEASURE () >= 1000, "#,0,.0\K",
"#,0"
)
The older way was the FORMAT function, which returns text. A text result cannot be plotted on a chart axis and sorts alphabetically, which is why dynamic format strings are the preferred answer.
Field parameters
You create one with Modeling > New parameter > Fields and pick measures or columns. Power BI builds a calculated table that uses NAMEOF, and can add a slicer for it:
Metric = {
( "Sales", NAMEOF ( 'Sales'[Total Sales] ), 0 ),
( "Margin", NAMEOF ( 'Sales'[Total Margin] ), 1 ),
( "Orders", NAMEOF ( 'Sales'[Order Count] ), 2 )
}
Put the parameter on a visual's axis or values and a slicer on the page. When users pick "Margin" in the slicer, the visual shows the Margin measure instead of Sales. A field parameter of columns works the same way for dimensions, such as switching the axis between Region, Product and Channel.
Which feature to use
| Requirement | Feature |
|---|---|
| Apply YTD, PY and YoY to many measures without copies | Calculation group |
| Show the right currency symbol or K and M suffixes, keep numbers numeric | Dynamic format string |
| Let users choose which measure or column a visual shows | Field parameter |
| Show a different format per calculation item | Format string expression on the calculation item |
A worked example
A finance model has Revenue, Cost and Margin, reported in several currencies. Managers want each with YTD and prior year, and one chart where they choose the measure.
- Create a Time Intelligence calculation group with Current, YTD, PY and YoY % items; give YoY % the format string
"0.0%". - Give the converted revenue measure a dynamic format string that reads the selected currency's format from a Currency table.
- Create a field parameter with Revenue, Cost and Margin, put it on the chart's values, and add its slicer to the page.
If the answer options include FORMAT() and "dynamic format string", choose the dynamic format string whenever the result must stay numeric for charts or sorting.
How the exam asks about this
- "Avoid creating YTD and PY versions of 30 measures." A calculation group with
SELECTEDMEASURE(). - "Show values in the currency the user selects while keeping them numeric." A dynamic format string.
- "Users must choose whether a chart shows sales, margin or units." A field parameter.
What to remember for the exam
- Calculation items use
SELECTEDMEASURE()and work only with explicit measures. - Adding a calculation group turns on Discourage implicit measures.
- Precedence orders multiple calculation groups; each item can have its own format string expression.
- Dynamic format strings change display only;
FORMAT()turns the value into text. - Field parameters are calculated tables built with
NAMEOFthat switch measures or columns through a slicer.
Calculation items do not apply to implicit measures, such as a numeric column dragged into a visual and summed. Create explicit measures for every value the calculation group must affect.
Common mistakes
Writing a separate YTD measure for every base measure. The model becomes hard to maintain. One calculation group covers them all.
Formatting with FORMAT() in a measure that feeds a chart. The chart cannot plot text. Use a dynamic format string.
Using a calculation group to switch between measures. A calculation group changes how a measure is calculated. Choosing which measure to show is a field parameter's job.
Check yourself
What does
SELECTEDMEASURE()return inside a calculation item?The measure currently being evaluated in the visual, so the item's logic applies to any measure.
Why is a dynamic format string better than
FORMAT()for a currency measure?It changes only the display, so the value stays numeric and still sorts, totals and plots correctly.
Which function do field parameter tables use to reference measures and columns?
NAMEOF, which returns the name of the referenced measure or column.
Topic 4 of 6 in Implement and Manage Semantic Models
Test yourself on Implement and Manage Semantic Models
The full Microsoft DP-600 question bank has 648 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.