Sub topic 4
Creating your first visual calculation
This chapter walks through making a visual calculation from start to finish in Power BI Desktop. It assumes you can build a table, matrix, or chart in a report. It also assumes you know what a measure is, can write a simple DAX aggregation, and know how DIVIDE works. Earlier chapters covered what visual calculations are and how they differ from measures and calculated columns. Here you will use them.
What you need before you start
A visual calculation belongs to one visual, so the visual has to exist and show the fields the calculation will use. Build this example first:
- Add a matrix visual to a report page.
- Put a date hierarchy field, such as Year, on Rows.
- Add the measure
[Sales Amount]to Values.
The matrix now shows one row per year with its sales. A visual calculation can only reference what the visual contains. This is the biggest difference from a measure, which can reach any table in the model.
Opening the calculation editing experience
You enter a separate editing mode to write a visual calculation. There are two ways in:
- Select the visual, then choose New calculation on the Home tab of the ribbon.
- Right-click the visual and choose New calculation.
Power BI Desktop then switches the page into the editing mode. It has three parts:
- Visual preview at the top. It shows the visual as it will look in the report and updates as you write.
- Visual matrix below the preview. It shows the data the visual receives, as a grid of rows and columns, and each calculation you add appears as a new column.
- Formula bar at the top of the editing area, where you type DAX.
Writing the formula
The formula bar takes a name, an equals sign, and a DAX expression. The first calculation is a running sum:
Running sum = RUNNINGSUM([Sales Amount])Press Enter to commit it. A new column named Running sum appears in the visual matrix, and the preview adds it to the matrix visual. For each year, the value is the sales for that year plus all earlier years.
The pieces are:
Running sumis the name you give the calculation. It becomes the field's name in the visual, so pick a name a report reader would understand.RUNNINGSUMis a visual calculation function. It exists only in this context. You cannot use it in a measure.[Sales Amount]is a reference to a field in the visual, written in square brackets with no table name.
How references work
In a measure you write Table[Column] or [Measure] and DAX looks it up in the model. In a visual calculation, square brackets refer to a field in the visual. The names you see in the visual matrix are the names you use.
If you rename a field in the visual, formulas that reference it are not affected, because they point at the field and not at its label. If you need a field the visual does not have, add it to the visual first. You can add it and then hide it so it does not clutter the display. Hiding is covered in a later chapter.
Adding more calculations
You can add as many calculations as you like to one visual. Choose New calculation again, and the new one is added alongside the others. A later calculation can reference an earlier one by name, which lets you build in steps:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])This subtracts the previous row's value from the current row's. On the first row there is no previous row, so PREVIOUS returns blank. The subtraction treats blank as zero, so the first row simply shows its own sales amount. This surprises many people. If you want the first row to stay empty, test for the blank explicitly. Do this in a later calculation so each step stays readable:
Previous sales = PREVIOUS([Sales Amount])Change vs previous = IF(ISBLANK([Previous sales]), BLANK(), [Sales Amount] - [Previous sales])Splitting a formula into named steps also makes the visual matrix useful for debugging. You can see each intermediate result in its own column and find where a number went wrong.
Reading the visual matrix
The visual matrix is the main debugging tool in this mode. It shows every row the visual receives, including the grand total row, and one column per field. When a calculation gives an unexpected number, look at the matrix. It shows which rows exist and what order they are in. Functions like PREVIOUS and RUNNINGSUM work by position in this grid, so the order matters.
The default direction follows the visual's layout. For a matrix with a hierarchy on rows, calculations move down the rows. Choosing a different direction uses the axis parameter, which a later chapter covers.
Committing, editing, and deleting
Your work is only kept once you commit it with Enter. To leave the editing mode and see the result in the full report, choose Back to report at the top of the screen.
After that, the calculations appear in the visual's field wells with a calculator icon. This tells you they belong to the visual and are not model fields. They do not appear in the Data pane, and no other visual can use them.
To change a calculation:
- Select the visual.
- Select the calculation in the field well, or reopen New calculation.
- Edit the formula in the formula bar.
To remove one, delete it from the visual's field wells or from the visual matrix in editing mode. Deleting it does not change the model, because the calculation never lived there.
Common errors
These problems come up most often with a first visual calculation:
| Symptom | Likely cause | Fix |
|---|---|---|
| The formula says a field cannot be found | The field is not in the visual | Add the field to the visual, then reference it |
| `Table[Column]` syntax gives an error | Visual calculations use field names in brackets, not model references | Use `[Field name]` |
| Values restart or run the wrong way | The calculation follows the default axis, which is not the one you wanted | Set the axis parameter explicitly |
| The first row of a comparison looks wrong | `PREVIOUS` returns blank on the first row | Test for blank, as shown above |
| The option to add a calculation is missing | The selected visual type does not support visual calculations | Use a supported visual, such as a table, matrix, or common chart |
A short worked check
Test your understanding of the result with a small example. Suppose the matrix shows these values, by year:
| Year | Sales Amount |
|---|---|
| 2022 | 100 |
| 2023 | 150 |
| 2024 | 130 |
Then RUNNINGSUM([Sales Amount]) gives 100, 250, and 380. The calculation [Sales Amount] - PREVIOUS([Sales Amount]) gives 100 for 2022, because the previous value is blank. It gives 50 for 2023 and -20 for 2024. If any of your numbers differ, open the visual matrix and check the row order first. A different sort order gives different running totals.
Where to go next
You now know the basic cycle: build a visual, open New calculation, write a formula that references fields in the visual, check it in the visual matrix, and go back to the report. The next chapters build on it. They cover the built-in templates that fill in common formulas for you, then the running and relative functions in more detail.
Exercise
A matrix shows Year on rows and [Sales Amount] in values, with 2022 = 100, 2023 = 150 and 2024 = 130. You add the visual calculation Change = [Sales Amount] - PREVIOUS([Sales Amount]). What does the 2022 row show, and what could you do to leave it empty?
Show answer
The 2022 row shows 100. There is no previous row, so PREVIOUS returns blank, and the subtraction treats blank as zero. To leave it empty, store PREVIOUS in its own calculation (for example Previous sales = PREVIOUS([Sales Amount])) and write Change = IF(ISBLANK([Previous sales]), BLANK(), [Sales Amount] - [Previous sales]).
Exercise
You write Running sum = RUNNINGSUM(Sales[Sales Amount]) in the formula bar and get an error. What is wrong, and how do you fix it?
Show answer
Visual calculations reference fields in the visual, not model tables and columns, so the Table[Column] syntax does not work here. Make sure Sales Amount is in the visual, then write Running sum = RUNNINGSUM([Sales Amount]).