Sub topic 5
Starting from the built-in templates
This chapter assumes you can build a table or matrix in Power BI Desktop and have opened the visual calculations editing experience. It also assumes you know what a measure is and how DIVIDE works. The chapter covers the template menu, what each template inserts, how to turn a template into a working calculation, and where templates stop being enough.
What a template is
A template is a prebuilt DAX pattern for a common visual calculation. When you pick one, Power BI writes a formula into the formula bar of a new calculation. The formula contains a placeholder where a field belongs. You replace the placeholder with a field or measure from your visual, then confirm the formula.
Templates do not add new capabilities. Each one is a formula you could type yourself, using functions covered in other chapters. They save typing and show the correct shape of each pattern.
Two points follow from this:
- A template produces an ordinary visual calculation. After insertion you can edit it freely, rename it, or delete it.
- A template does not read your visual and choose a field for you. You must do that step.
Opening the template list
- Select a table or matrix and choose New calculation in the ribbon. This opens the visual calculations editing experience.
- In the ribbon of that experience, open the Templates dropdown.
- Pick a template. The formula bar fills with the pattern.
The available templates are:
| Template | What it computes | Functions behind it |
|---|---|---|
| Running sum | Cumulative total down the axis | `RUNNINGSUM` |
| Moving average | Average over a sliding window of rows | `MOVINGAVERAGE` |
| Percent of parent | Share of the value one level up | `DIVIDE`, `COLLAPSE` |
| Percent of grand total | Share of the top-level value | `DIVIDE`, `COLLAPSEALL` |
| Average of children | Average of the values one level down | `EXPAND` family |
| Versus previous | Difference from the previous row | `PREVIOUS` |
| Versus next | Difference from the next row | `NEXT` |
| Versus first | Difference from the first row | `FIRST` |
| Versus last | Difference from the last row | `LAST` |
The exact text each template inserts can vary slightly between Power BI Desktop releases. Read the formula after you insert it instead of relying on memory.
Worked example: a running sum from a template
Suppose you have a matrix with Month on rows and Sales Amount as the value. The months run January to December.
- Choose Templates, then Running sum.
- The formula bar shows a pattern like
RUNNINGSUM(<Field>). - Replace
<Field>with[Sales Amount]. You can type the name or click the field in the visual's field list. - Name the calculation. The formula bar takes a
Name = expressionform, so the finished line looks like this:
Running sum = RUNNINGSUM([Sales Amount])- Press Enter. A new column appears in the visual, with each month showing the cumulative total so far.
The placeholder text is not valid DAX. If you press Enter without replacing it, Power BI reports an error and does not create the calculation. This is the most common mistake with templates, and the fix is to replace every <...> token.
Worked example: a comparison template
Insert Versus previous in the same matrix. After you replace the placeholder, the result is a subtraction of the earlier row from the current one:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])January has no previous row, so PREVIOUS returns blank for it. In DAX, [Sales Amount] - BLANK() evaluates to the sales amount itself, so January may show its own value instead of a blank. If you want January to stay empty, wrap the formula in a check, which the templates do not do for you:
Change vs previous =
IF(
ISBLANK(PREVIOUS([Sales Amount])),
BLANK(),
[Sales Amount] - PREVIOUS([Sales Amount])
)This is a first example of a template giving you a correct starting point that you then adapt.
Which field goes in the placeholder
The placeholder expects a field or measure that already exists in the visual. Visual calculations can only see what is in the visual's data layout, so the choice is limited to the fields on the visual.
- You can use a measure such as
[Sales Amount]or a column that has an aggregation applied. - You can also use another visual calculation in the same visual. A calculation can build on an earlier one.
- A field that is not in the visual cannot be used. Add it to the visual first, and hide it afterward if you do not want to see it. A later chapter covers hiding.
If your visual shows several values, each template applies to one of them. To get a running sum for two measures, you create two calculations.
Templates that depend on hierarchy
Two templates behave differently from the others because they depend on the shape of the rows.
Percent of parent divides each value by the value one level above it. It makes sense only when the rows form a hierarchy, for example Year over Quarter over Month, and the matrix is expanded to show more than one level. At the top level there is no parent, and the pattern returns the value divided by itself, which is 100%.
Percent of grand total divides by the value at the very top of the row hierarchy, however deep the current row is.
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))With a flat list, such as one level of Product, both templates give the same result, because the parent and the grand total are the same cell. If a result looks wrong, check whether the visual has the hierarchy you expect.
Defaults hidden in the template
Templates insert the shortest form of each function. Functions that take an axis argument, such as RUNNINGSUM, use the visual's default axis when you leave it out. For a matrix that is normally the rows direction first. If you want the calculation to run across columns instead, add the axis argument yourself:
Running sum across = RUNNINGSUM([Sales Amount], COLUMNS)The same applies to moving average. The template uses a fixed window size. Change that number to widen or narrow the window:
Moving average = MOVINGAVERAGE([Sales Amount], 3)The first rows have fewer rows behind them than the window size. Do not read an early moving average as a full three-month figure.
Common errors
- Placeholder left in place. The formula is rejected. Replace every
<...>token. - Field not in the visual. The field name is not recognized. Add the field to the visual first.
- Percent of parent on a flat axis. The result is 100% on every row. The visual needs a hierarchy.
- Wrong sort order. Running sums and comparisons follow the visual's sort order. If you sort the months by sales instead of by date, the running sum accumulates in sales order. Sort by the axis you mean to accumulate along.
- Percentages formatted as decimals. A share such as 0.25 appears as 0.25 until you set the format to percentage.
When to leave the templates behind
Templates cover the common cases. They do not cover everything. Write the formula yourself when you need to:
- combine two patterns, such as the percent change from the previous row, which needs
DIVIDEwithPREVIOUS - handle the blank in the first row, as in the example above
- restart a calculation at each group, which needs the reset argument covered later
- use ranking or lookup functions, which have no template
A good habit is to start from a template, read the formula it gives you, and then modify it. This shows you the function signature in context, and later you will type these formulas without the menu.
Exercise
You insert the Percent of parent template into a matrix that shows only Product on rows, with no hierarchy. Every row shows 100%. Why, and what do you change?
Show answer
With a single flat level there is no row above the products, so the "parent" value that COLLAPSE returns is the row's own value, or the same total for every row depending on the layout, and the division does not give a share of a group. The template needs a row hierarchy, for example Category over Product, with the matrix showing both levels. Add the higher-level field to the rows and expand to the lower level. If you only want each product's share of the overall total, use the Percent of grand total pattern instead.
Exercise
You insert the Running sum template, type [Sales Amount] over the placeholder, and press Enter, but the running sum for each month does not follow the calendar order. What do you check first?
Show answer
Check the sort order of the axis in the visual. RUNNINGSUM accumulates along the visual's current row order, so if the months are sorted by value instead of by date, the total builds up in that order. Sort the visual by the month field in ascending order.