Sub topic 1
What Visual Calculations Are and Why They Exist
Prerequisites
This chapter assumes you have the following background:
- You can build reports in Power BI Desktop: adding a table, matrix, or chart and dragging fields onto it.
- You know the difference between a measure and a calculated column.
- You can write a basic DAX measure with an aggregation such as
SUM, and you have usedDIVIDE.
You do not need to know anything about visual calculations yet. This chapter introduces the idea and the reasons behind it. Later chapters cover creating them and the individual functions.
The problem they solve
Many common report requests look simple on screen but are awkward in a data model. Examples:
- A running total of sales across months.
- A moving average over the last three months.
- The change from the previous row.
- Each row's share of its parent group.
In a standard DAX measure, these calculations work on the whole model. A measure does not know which rows the visual shows. To build a running total, you write a filter expression that rebuilds the date range from the filter context. A typical pattern looks like this:
Running total =
CALCULATE (
[Sales Amount],
FILTER (
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX ( 'Date'[Date] )
)
)This measure works, but it has costs. You must know which table and column define the order. You must understand ALL, FILTER, and filter context. The measure also breaks down when the user changes the axis, for example from date to product category.
A visual calculation is a DAX calculation that you define directly on a visual. It runs against the data the visual already holds. It does not need to rebuild that data from the model. You write the same running total like this:
Running sum = RUNNINGSUM([Sales Amount])The function reads the rows of the visual and adds up the values in order.
Definition
A visual calculation has these properties:
- It is defined on one visual and stored with that visual. It does not appear in the model's field list.
- It can refer to any field that is in the visual, including measures and columns that you placed on it.
- It operates on the visual's own layout, meaning its rows and columns, instead of on the model's tables and relationships.
- It can use special functions that only make sense on a visual, such as
RUNNINGSUM,MOVINGAVERAGE,PREVIOUS,COLLAPSE, andCOLLAPSEALL.
The result appears as a new field in the visual, next to the fields you already placed there.
A first look at the code
These five examples show the range of what visual calculations do. Each is explained in more detail in later chapters. For now, read them to see how short they are.
Running sum = RUNNINGSUM([Sales Amount])This adds up Sales Amount cumulatively across the rows of the visual, using the visual's default axis.
Moving average = MOVINGAVERAGE([Sales Amount], 3)This averages Sales Amount over a window of three rows ending at the current row.
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])This subtracts the value from the previous row on the axis from the current row's value.
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))This divides each value by the value one level up the row hierarchy, which gives its share of the parent.
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))This divides each value by the value at the top of the row hierarchy, which gives its share of the grand total.
None of these needs CALCULATE, FILTER, or ALL. Each one names a field from the visual and applies a function that understands the visual's layout.
How a visual calculation works
The model holds tables, relationships, and measures. When a visual renders, Power BI runs a query against the model and produces a result grid. The grid has one row per combination of the fields you placed on the axes, and the measure values for each.
A visual calculation runs after that. It takes the result grid as its input. For each cell, the calculation reads other cells of the grid, such as the previous row or the parent row, and produces a new value.
This order explains most of the behavior you will see:
- A visual calculation can only use what is in the grid. If a field is not on the visual, the calculation cannot see it.
- Changing the visual's fields changes the grid, so the calculation's result changes too.
- The calculation does not change the filter context of the query. It works on values the query already returned.
Why they exist
Visual calculations were introduced to address problems that report authors had with measures. The main motivations are as follows.
Simpler syntax
Calculations such as running totals, moving averages, and percent of parent are common. A measure for each requires a filter pattern. Visual calculations replace the pattern with a single function call.
Direct access to the visual's layout
A measure cannot ask "what is the value in the previous row of this visual?" because a measure does not know about rows. A visual calculation can. PREVIOUS, NEXT, FIRST, and LAST all depend on this ability.
Fewer changes to the model
Adding a measure changes the shared model. Other report authors see it, and it needs a good name and some thought about reuse. A visual calculation affects only the visual it lives on. This makes it suitable for one-off analysis, where you do not want to add to the model.
Work for people without model access
Some report authors can build visuals but cannot edit the semantic model, for example when the model is shared and owned by another team. Visual calculations let those authors add calculations without touching the model.
Performance in some cases
Because the calculation runs on the already aggregated grid, it often works on far fewer rows than a measure that has to scan the model's tables. For visuals with a modest number of rows, this can be faster. Do not assume it is always faster. Measure the result on your own report before relying on it.
Visual calculations compared briefly
The next chapter covers the comparison with measures and calculated columns in detail. This table gives only the headline differences.
| Feature | Visual calculation | Measure | Calculated column |
|---|---|---|---|
| Where it is defined | On one visual | In the model | In a model table |
| Scope | That visual only | Any visual | Any visual, as a column |
| Works on | The visual's result grid | Model data in filter context | Each row of one table |
| Calculated | When the visual renders | When the visual renders | When the model refreshes |
| Reusable elsewhere | No, unless you copy the visual | Yes | Yes |
What to expect in practice
A few behaviors surprise new users. Knowing them early saves time.
- The calculation is tied to the visual. If you delete the visual, the calculation is deleted too. If you copy the visual, the calculation is copied with it.
- Fields must be on the visual. If you remove
Sales Amountfrom the visual, a calculation that uses it can no longer work. - Results depend on layout. A running sum over months gives a different answer if you change the visual to show years. This is by design.
- Order matters. Functions such as
PREVIOUSdepend on the order of rows. The order comes from the visual's sort and axis, so a change in sorting changes the result. - Not every visual is supported equally. Matrices and tables are the clearest cases. Later chapters cover how charts map to rows and columns.
When this is a good fit
Visual calculations suit these cases:
- Running totals, moving averages, and period-to-period changes on a visual that already has the right axis.
- Percent of parent and percent of grand total on a matrix.
- Quick exploratory analysis where adding to the model is not worth the effort.
- Reports where you cannot edit the model.
They suit these cases less well:
- A value that several reports need. Define a measure instead.
- A value that must work in a different visual, or be used in a filter or relationship. The calculation lives only on its own visual.
- Logic that depends on fields that you do not want to show on the visual.
The final chapters of this tutorial give a full decision guide, including limitations. At this stage, hold on to one rule: if the calculation is about the layout of one visual, it is a good candidate. If it is about the meaning of the data across the model, use a measure.
Summary of the key ideas
- A visual calculation is DAX defined on a single visual and evaluated against that visual's data.
- It runs after the query, on the result grid, so it sees rows and columns and not the model's tables.
- It makes common patterns short and avoids filter-context code.
- It stays local to its visual, which is both its strength and its limit.
Exercise
A colleague writes a measure with CALCULATE, FILTER, and ALL to produce a running total of sales by month. They ask why you would use a visual calculation with RUNNINGSUM instead. Give two reasons, and one situation where you would still choose the measure.
Show answer
Reasons: (1) RUNNINGSUM replaces the filter pattern with one function call, so it is shorter and easier to read. (2) It runs on the visual's result grid and follows the visual's axis, so it keeps working if the axis changes, and it does not add anything to the shared model. Still choose the measure when the running total must be reused across several visuals or reports, or used somewhere a visual calculation cannot go, such as a filter. The visual calculation exists only on one visual.
Exercise
You have a matrix with Year and Month on rows and Sales Amount as a value. You write Share = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS)). Then you remove Sales Amount from the matrix. What happens to the calculation, and why?
Show answer
The calculation can no longer produce a result, because it refers to a field that is not on the visual. A visual calculation works on the visual's result grid, and a field that is not in the grid is not available to it. To fix it, add Sales Amount back to the visual, or hide it if you only need it as an input.