Calculation groups are a robust modeling feature in Power BI that reduces redundant measures created by grouping standard measure expressions as calculation items. The calculation group is like a table with DAX expressions for each of the calculation items.
Let us consider a scenario where you need time-related calculations like YTD, MTD, QTD for sales amount, sales quantity, profit measures. Here, in general, we will be creating time calculations of each measure resulting in 9 calculations. To avoid the redundant calculation, we can create a calculation group for time-related calculations and use it with any base measure.
Calculation groups are supported in tabular models at the 1500 and higher compatibility level. Calculation groups work with explicit DAX measures. ‘DiscourageImplicitMeasures’ model property should be set to ‘true’ to create calculation groups. As of the July 2020 release, we can access external tools like Tabular editor, DAX studio, ALM toolkit, etc. from the Power BI Desktop. You need to install these external tools individually. These external tools are automatically connected to the Analysis Services engine, providing a seamless experience for users.
After the initial setup, the Power BI Desktop will look like below with external tools ribbon. For creating calculation groups, we will be using Tabular editor.
1. From the External Tools ribbon, launch Tabular editor from Power BI Desktop and right-click on Tables and select Create New -> Calculation Group.
Initially, you will have one unique table created for the calculation group. Provide a suitable name for calculation group and create individual calculation items for each time calculation like below:
- Current – SELECTEDMEASURE()
- YTD – CALCULATE(SELECTEDMEASURE(), DATESYTD(‘Date table'[Date]))
- MTD – CALCULATE(SELECTEDMEASURE(), DATESMTD(‘Date table'[Date]))
- QTD – CALCULATE(SELECTEDMEASURE(), DATESQTD(‘Date table'[Date]))
- PY – CALCULATE(SELECTEDMEASURE(), SAMEPERIODLASTYEAR(‘Date table'[Date]))
- PY YTD – CALCULATE(SELECTEDMEASURE(),SAMEPERIODLASTYEAR(‘Date table'[Date]),’Timecalgroup'[Timecal]= “YTD”)
- YOY– SELECTEDMEASURE()-CALCULATE(SELECTEDMEASURE(),’Timecalgroup'[Timecal] = “PY”)
- YOY % – DIVIDE(CALCULATE(SELECTEDMEASURE(),’Timecalgroup'[Timecal] =”YOY”),CALCULATE(SELECTEDMEASURE(),’Timecalgroup'[Timecal] =”PY”))
Click on the ‘Save’ icon, which will save the changes to the connected database and the Power BI Desktop. You will get a warning to manually refresh whenever you modify the calculation group, like adding or deleting a calculation item.
On refreshing, the calculation group will be available for usage. You can drag and drop in a matrix or slicer with a base measure. The below matrix contains the sales amount measure with our ‘Timecalgroup’ calculation group.
Here the YOY% measure should have value in percentage. We need to override the base format string and use %. In each calculation item, we can update the format string both statically and dynamically using DAX’s format function. Below we have provided the format string as “0.00%; -0.00%;0.00%” to contain the format for positive, negative, and zero values.
This is useful during currency calculations where different formats can be provided for your currency using the ‘format string expression.’ When sorting the calculation items, we can assign a value in ‘ordinal’ property that is sequential. The calculation item with an ordinal property of 1 will appear first, 2 next, and so on. If you see our YOY%, we assigned 7 as the ordinal value and it appears last in the report.
In the calculation group, using the ‘precedence’ property, we can specify the order of evaluation when there is more than one calculation group. Greater value in ‘precedence’ means it is of more precedence and evaluated first.
A use case is to see daily average sales and YTD daily average sales. Create a new calculation group for average called ‘Averagecalgroup’ and add a daily average calculation item.
Daily average =DIVIDE(SELECTEDMEASURE(), COUNTROWS(‘Date table'[Date]))
CALCULATE(SELECTEDMEASURE(), ‘Timecalgroup'[Timecal]= “YTD”, ‘Averagecalgroup'[Averagecal] = “Daily average”)
‘Timecalgroup’ calculation group should have higher precedence than ‘Averagecalgroup’ since we need YTD daily average. YTD should be applied to both numerator and denominator. We will set precedence 20 for ‘Timecalgroup’ and 10 for ‘Averagecalgroup’ to avoid miscalculations.
We can create calculation groups for base measures to use in a slicer or filter for measure selections. Create a calculation group called ‘Measurecalgroup’ and create individual calculation items for each measure. The DAX expression is the base measure itself.
You can see the calculation groups for time calculation and also for the base measure in action below.
Learn more about Microsoft Power BI services offerings from Visual BI solutions here.