Sub topic 1
What Visual Calculations Are and Why They Exist
Prerequisites
This chapter assumes you can build reports in Power BI Desktop and are comfortable with tables, matrices, and charts. It also assumes you know basic DAX: how a measure aggregates data, how DIVIDE works, and how a measure differs from a column. You don't need to know any visual calculation functions yet.
The problem they solve
Many report requests sound simple. Show a running total of sales by month. Show each month's change from the previous month. Show each product's share of its category.
Before visual calculations, you built these as measures. A measure runs in filter context, so you had to write DAX that rebuilt the right set of rows, often with CALCULATE, ALL, FILTER, and date functions. The result was correct but hard to write and hard to read. The measure also had to work in every visual that might use it, even though you only wanted it in one.
A visual calculation is a DAX calculation that you define on a single visual. It runs against the data already in that visual, so you can refer to values in other rows and columns of the visual directly. You don't need to describe the filter context to get at them.
What a visual calculation is
A visual calculation has four defining traits:
- It belongs to one visual. It is stored with that visual and does not appear in the model.
- It reads the visual's own data. Its inputs are the fields currently in the visual, laid out as rows and columns.
- It is evaluated after the visual's query returns, over the rows you can see.
- It can refer to other fields and calculations in the same visual.
Because it works on what the visual shows, a visual calculation can ask questions such as "what is the value in the previous row?" These are awkward to ask from a measure.
A first look
Suppose a matrix shows Sales Amount by month. This visual calculation adds a cumulative total down the rows:
Running sum = RUNNINGSUM([Sales Amount])RUNNINGSUM adds up [Sales Amount] across the rows of the visual, so each row shows its own value plus all values above it. There is no CALCULATE and no date filter. The visual's layout supplies the order.
This one subtracts the previous row's value from the current row's value:
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])The two calculations take the same approach. They use the visual's rows as the unit of work, and later chapters build on that idea.
Why they exist
Visual calculations aim to make common calculations easier in three ways.
- Simpler DAX. Functions such as
RUNNINGSUM,MOVINGAVERAGE, andPREVIOUSreplace long filter-context expressions. - Easier maintenance. The calculation lives next to the visual that uses it, so you don't search the model for it or worry about it affecting other reports.
- Less model clutter. One-off calculations don't add measures that other authors must understand.
Common misunderstandings
- A visual calculation is not a calculated column. A calculated column is computed per row of a model table and stored in the model.
- It is not a measure. A measure is defined in the model and evaluated in filter context for any visual that uses it.
- It depends on the visual's layout. If you change the fields in the visual, the result can change, because the rows it reads have changed.
Exercise
A colleague wants a "change from previous month" figure that appears only in one matrix on one report page. Why might a visual calculation suit this better than a measure?
Show answer
The calculation depends on the row order of that matrix and is needed in only that visual. A visual calculation can use PREVIOUS to read the prior row directly, stays with the visual, and does not add a measure to the model.