Sub topic 5
Starting from the built-in templates
This chapter assumes you can already open the visual calculations editing experience and add a calculation to a table or matrix. It also assumes you know basic DAX, including the DIVIDE function. If you haven't done that yet, go back to the chapter on creating your first visual calculation.
What the templates are
Power BI Desktop includes a set of ready-made visual calculations called templates. Each template inserts a formula skeleton into the formula bar. You pick a field and adjust a few arguments. The templates save you from remembering function names and argument order.
The templates cover the patterns used most often:
| Template | Function it is built on | What it returns |
|---|---|---|
| Running sum | `RUNNINGSUM` | Cumulative total along the axis |
| Moving average | `MOVINGAVERAGE` | Average over a sliding window of rows |
| Percent of parent | `DIVIDE` with `COLLAPSE` | Share of the row one level up |
| Percent of grand total | `DIVIDE` with `COLLAPSEALL` | Share of the top-level total |
| Versus previous, next, first, last | `PREVIOUS`, `NEXT`, `FIRST`, `LAST` | Difference from the row in that position |
Later chapters cover each function in depth. Here the goal is to use the templates efficiently.
Using a template
- Select a visual that already shows the fields you need, for example a matrix with
Monthon rows andSales Amountas a value. - Choose New calculation to open the visual calculations editing experience.
- Open the template list in the formula bar area and pick a template.
- Replace the placeholder with the field you want to calculate over. Rename the calculation.
- Press Enter to commit it. The new column appears in the visual.
Here is a moving average template after you fill it in:
Moving average = MOVINGAVERAGE([Sales Amount], 3)The template's default window size may differ from the 3 shown here. The second argument sets how many rows the window covers, ending at the current row.
A "versus previous" calculation takes the same approach:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])Common errors
- Leaving a placeholder in the formula. The calculation shows an error until every placeholder is replaced with a real field reference.
- Picking the wrong field. Templates work on the fields in your visual. A field that is not in the visual is not available, so add it to the visual first.
- Trusting the result without checking the layout. The template computes along the visual's default axis. If the numbers run in the wrong direction, the axis parameter needs changing. The next chapters explain it.
- Using "percent of parent" at the top level. The top level has no parent row, so the result is not meaningful. Use "percent of grand total" there instead.
Summary
Templates give you a correct first draft of the common patterns. Replace the placeholder, rename the result, and check it against the visual's layout. Then edit the formula to fit your case.
Exercise
You inserted the "Percent of parent" template into a matrix with Year and Quarter on rows. The Year rows show 100%, and the Quarter rows show each quarter's share of its year. Why do the Year rows show 100%, and which template gives each year's share of everything?
Show answer
The Year rows are the top level, so the value one level up is the value of the row itself, and the share is 100%. The "Percent of grand total" template, which divides by COLLAPSEALL([Sales Amount], ROWS), gives each year's share of the overall total.