Model the data · Topic 5 of 6
Statistical Functions and Calculation Groups
PL-300 statistics and calculation groups: use MEDIAN, STDEV, VAR, PERCENTILE and RANKX in DAX, and build calculation groups with calculation items in Model view.
- 4 min
- STDEV.P and VAR.P use the whole population. STDEV.S and VAR.S use a sample.
- MEDIAN returns the middle value. For an even count it averages the middle two.
- RANKX and TOPN usually need ALL to rank across the whole table.
What it is
DAX includes basic statistical functions for averages, spread and ranking. When you have many measures that need the same variation, such as year to date or prior year for every measure, a calculation group applies one set of calculation items to all of them. That avoids writing a separate measure for every combination.
How it works
Basic statistical functions
| Function | Returns |
|---|---|
| AVERAGE, MEDIAN | The mean and the middle value of a column |
| MIN, MAX | Smallest and largest |
| STDEV.S, STDEV.P | Standard deviation of a sample or of the whole population |
| VAR.S, VAR.P | Variance of a sample or of the population |
| PERCENTILE.INC, PERCENTILE.EXC | A percentile, with inclusive or exclusive range |
| COUNTROWS, DISTINCTCOUNT | Counts of rows and distinct values |
| RANKX | The rank of a value within a table |
| TOPN | The top N rows by an expression |
| AVERAGEX, MEDIANX | Averages computed over an expression, row by row |
For the values 2, 4, 4, 4, 5, 5, 7 and 9, the average is 5, the median is 4.5 (the middle two values are 4 and 5), the population variance is 4 and the population standard deviation is 2. The S versions divide by n minus 1 and so give a larger result for the same data. Use .P when the column is the whole population, and .S when it is a sample.
Median Order Value = MEDIAN ( Sales[Amount] )
Product Rank = RANKX ( ALL ( Product[Name] ), [Total Sales] )
Top 3 Sales = CALCULATE ( [Total Sales], TOPN ( 3, ALL ( Product[Name] ), [Total Sales] ) )
RANKX needs ALL (or a similar table) so that the ranking covers every product, not only the one in the current filter context.
What a calculation group is
A calculation group is a special table with one column. Each row of the column is a calculation item, a DAX expression that modifies whatever measure is in use. The placeholder SELECTEDMEASURE() stands for that measure.
| Item | Expression |
|---|---|
| Current | SELECTEDMEASURE () |
| YTD | CALCULATE ( SELECTEDMEASURE (), DATESYTD ( 'Date'[Date] ) ) |
| PY | CALCULATE ( SELECTEDMEASURE (), SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) |
| YoY % | DIVIDE ( SELECTEDMEASURE () - CALCULATE ( SELECTEDMEASURE (), SAMEPERIODLASTYEAR ( 'Date'[Date] ) ), CALCULATE ( SELECTEDMEASURE (), SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) ) |
With three measures and four items you get twelve results from seven definitions.
Creating a calculation group
- Open Model view, and select Calculation group in the ribbon (the table appears in the Model explorer).
- If Discourage implicit measures is off, Power BI asks you to turn it on. Accept, because calculation items apply only to explicit measures.
- Edit the first calculation item, then add more with New calculation item.
- Rename the group and its column. The column is what you put on matrix columns or in a slicer.
- Optionally set a dynamic format string on an item, such as a percentage format for YoY %, and set the precedence if the model has several groups.
You can also build one in TMDL view. Use the calculation group column on matrix columns to show each item side by side, or in a slicer to apply one item to every measure.
Considerations
- Calculation items affect explicit measures only, not implicit ones.
- Once a calculation group exists, measures use the variant data type in reports.
- A calculation item that does math on a text measure, such as a title measure, can show an error. Protect it with
ISNUMERIC ( SELECTEDMEASURE () ). - With several calculation groups, precedence decides which is applied first.
A worked scenario
A finance model has Sales, Cost and Margin measures, and users also want YTD, prior year and year over year for each. Instead of writing twelve measures, the analyst creates a calculation group named Time Calculation with items Current, YTD, PY and YoY %, and a dynamic format string for YoY % so it shows as a percentage. In a matrix she places the Time Calculation column on columns and the three measures in values. Adding a fourth measure later needs no extra items.
What to remember for the exam
- STDEV.P and VAR.P use the whole population. STDEV.S and VAR.S use a sample.
- MEDIAN returns the middle value. For an even count it averages the middle two.
- RANKX and TOPN usually need ALL to rank across the whole table.
- A calculation group has calculation items that use SELECTEDMEASURE().
- Creating a calculation group requires Discourage implicit measures. Items apply to explicit measures only.
A question about many repeated time variations of many measures points to a calculation group.
How the exam asks about this
- "Apply YTD and prior year to every existing measure." A calculation group.
- "Return the middle value of order amounts." MEDIAN.
- "Rank each product by total sales." RANKX with ALL.
- "The data is the entire population." STDEV.P.
Common mistakes
Using .S for a full population.
RANKX without ALL. Every product ranks first within its own context.
Expecting calculation items to change implicit measures.
Forgetting a format string. A YoY % item shows as a plain number.
Check yourself
What does SELECTEDMEASURE return?
The measure that the calculation item is currently applied to.
Which function gives the middle value of a column?
MEDIAN.
What setting must be on to create a calculation group?
Discourage implicit measures.
Topic 5 of 6 in Model the data
Test yourself on Model the data
The full Microsoft PL-300 question bank has 657 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.