Sub topic 4
Creating Your First Visual Calculation
This chapter assumes you can build tables, matrices, and charts in Power BI Desktop, and that you know basic DAX: measures, aggregations, and DIVIDE. You will add a running sum to a visual and learn how the editing experience works.
What you need first
A visual calculation belongs to one visual. Before you write one, the visual must already show the fields the calculation will refer to. Build a table or matrix with a date field (for example Month) on the rows and the measure [Sales Amount] in the values. The calculation can only use fields that are in the visual.
Open the calculation editing experience
- Select the visual on the report canvas.
- On the ribbon, choose New calculation. You can also right-click the visual and pick New calculation.
- Power BI Desktop switches to the calculation editing experience. The visual is shown at the top. Below it is a formula bar and a preview of the visual's data.
Write the formula
Type the formula in the formula bar. The first line names the calculation, then an equals sign, then the expression.
Running sum = RUNNINGSUM([Sales Amount])RUNNINGSUM adds up [Sales Amount] cumulatively across the rows of the visual, using the visual's default axis. Press Enter to commit the formula. A new column named Running sum appears in the visual and in the preview. Each row shows its own sales plus all the rows above it.
You can refer to fields in the visual by their names in square brackets. You can also refer to other visual calculations. You can't refer to a column that isn't in the visual.
Edit, rename, and remove
- To change a calculation, open the editing experience again, select the calculation in the visual's field list, and edit the formula.
- To rename it, change the text before the equals sign.
- To remove it, delete it from the visual's field list in the editing experience.
Because the calculation is stored in the visual, the same field list shows it next to the regular fields. Other visuals on the page don't see it.
Add a second calculation
You can add several calculations to one visual, and a later one can build on an earlier one. Add a comparison with the previous row:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])PREVIOUS returns the value from the row above on the axis. In the first row there is no row above it, so the result is blank, and the subtraction treats it as zero.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Cannot find field" error | The field is not in the visual | Add the field to the visual, then rewrite the formula |
| Running sum restarts or runs the wrong way | The visual's layout differs from what you assumed | Check which fields are on rows and columns. Later chapters cover the axis parameter |
| The calculation is missing on another page | Visual calculations belong to one visual | Copy and paste the visual, or recreate the calculation |
| Different values than a measure gives | Visual calculations run on the visual's rows, not on the model's filter context | Compare against the preview data |
Check your result
Return to the report and add a few months to the filter. The running sum follows the rows that remain. It does not include rows that the filter removed. This is the main difference from a measure, which could see the whole model.
Exercise
You select a matrix with Month on the rows and [Sales Amount] in the values. Write the formula for a visual calculation named Running sum, and say why a column that is not in the matrix can't be used in the formula.
Show answer
Running sum = RUNNINGSUM([Sales Amount]). A visual calculation runs against the data of its own visual, so it can only refer to fields that are in that visual. To use another column, add it to the visual first.
Exercise
What does Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount]) show for the first row of the visual?
Show answer
The first row has no previous row, so PREVIOUS returns blank. The subtraction treats the blank as zero, so the result equals the first row's own Sales Amount.