Sub topic 2
How Visual Calculations Differ from Measures and Calculated Columns
This chapter assumes you can author basic Power BI Desktop reports, you know the difference between a measure and a column, and you can write simple DAX with aggregations and DIVIDE. It compares the three places where you can put DAX in Power BI. It then covers the errors people hit most often when they pick the wrong one.
Three places to put a calculation
Power BI gives you three ways to add a calculation to a report.
- A calculated column is a column added to a table in the semantic model. DAX evaluates it row by row when the model refreshes, and it stores the result.
- A measure is a named expression in the semantic model. DAX evaluates it at query time, in the filter context of each cell of each visual.
- A visual calculation is DAX defined on one visual. It runs against the data that visual already holds, and it is stored in the report, not in the model.
All three use DAX. What differs is where the calculation lives, what data it can see, and when it runs.
Where each one is stored
A calculated column and a measure both belong to the semantic model. Any report built on that model can use them. Anyone with build permission on the model can see them in the Data pane.
A visual calculation belongs to a single visual in a single report. It does not appear in the model and cannot be reused in another visual. If you want the same calculation on a second visual, you copy the visual or recreate the calculation.
This has one useful consequence. You can add a visual calculation without having permission to change the model. That matters when the model is shared, certified, or owned by another team.
What each one can see
This is the most important difference.
A measure sees the whole model. It can reference any table or column, and it can use functions such as CALCULATE, FILTER, and ALL to change the filter context. It does not know what the visual looks like. It only knows the filter context of the cell it is computing.
A calculated column sees one row of its own table, plus related tables through relationships. It does not know about filters coming from a visual.
A visual calculation sees the visual's own grid of values. That grid contains the fields in the visual's field wells and the measures or aggregated columns you placed there. It does not see model tables directly. You cannot write SUM(Sales[Amount]) or RELATED(...) inside a visual calculation unless the field is already in the visual. Instead, you refer to values that are in the visual, such as [Sales Amount].
Because it sees the grid, a visual calculation can talk about position. It can ask for the previous row, the first row, the running total so far, or the parent row in a hierarchy. Measures have no such concept without extra work.
| Property | Calculated column | Measure | Visual calculation |
|---|---|---|---|
| Stored in | Semantic model | Semantic model | The visual, in the report |
| Evaluated | At model refresh | At query time | At query time, on the visual's result |
| Context | Row context of one table | Filter context of a cell | The visual's rows and columns |
| Can reference | Columns in the model | Anything in the model | Fields and measures in the visual |
| Reusable across visuals | Yes | Yes | No |
| Uses model storage | Yes | No | No |
| Knows about row position | No | Not directly | Yes |
How the same result looks in each form
Take a running total of sales by month. Here is the measure approach.
Running total (measure) =
CALCULATE(
[Sales Amount],
FILTER(
ALL('Date'[Month Start]),
'Date'[Month Start] <= MAX('Date'[Month Start])
)
)The measure removes the month filter. It then adds back every month up to the current one. You must know about filter context, ALL, and MAX to write it, and it only works if a month column is on the visual.
Here is the visual calculation version.
Running sum = RUNNINGSUM([Sales Amount])The visual calculation adds up the values already in the visual, from the first row to the current row. It needs no filter manipulation. The cost is that it only exists on this visual.
A calculated column cannot produce this result in a useful way. A column holds one value per row of a table, and it is fixed at refresh time. It cannot know which months the user has filtered to.
What each one is good for
Use a calculated column when you need a value to slice, filter, group, or sort by. Examples are a price band, a fiscal year label, or a flag. Visuals cannot slice by a measure's result, but they can slice by a column.
Use a measure when the calculation should be shared and consistent. Examples are total sales, margin percent, and year-to-date. Measures also work in any visual, including visuals that have no row order.
Use a visual calculation when the calculation depends on the layout of one visual. Examples are running totals, moving averages, differences from the previous row, and percent of parent. These are easier to write as visual calculations than as measures.
Performance and model size
A calculated column takes space in the model, and it adds time to refresh. A large table with several columns can grow noticeably.
A measure takes almost no space. Its cost is the query-time work, which depends on how complex the expression is.
A visual calculation also takes almost no space. It runs on the visual's result set, which is usually small, so it avoids many of the repeated storage-engine scans that a filter-manipulating measure can trigger. Microsoft's documentation describes this as one of the reasons to use them. The effect depends on the visual and the model, so measure it in your own report instead of assuming it.
Common errors
Expecting to slice by a visual calculation
You cannot use a visual calculation as a slicer, a filter field, or a field in another visual. If you need that, use a calculated column or a measure placed in a filter.
Referencing a model field that is not in the visual
A visual calculation sees only what the visual has. If you write a formula that uses a column that is not in the visual's field wells, the formula fails. Add the field to the visual first. If you do not want to display it, hide it. A later chapter covers hidden fields.
Copying a measure formula unchanged
A measure formula written with SUM(Sales[Amount]) or CALCULATE does not translate directly. Replace model references with references to the fields in the visual, and replace filter-context tricks with visual functions like RUNNINGSUM or PREVIOUS.
Assuming the result is the same in every visual
A running sum depends on the order and layout of the visual. If a user changes the sort order, the running sum changes. If the visual is a card with a single value, there is no row to run over. A measure gives the same answer for the same filter context regardless of layout.
Putting a calculated column where a measure belongs
A calculated column that divides two other columns, such as [Profit] / [Revenue], gives a per-row ratio. If you then average or sum that column in a visual, the total is wrong. A ratio belongs in a measure, where it is computed from the aggregated numerator and denominator.
How to choose
Ask these questions in order.
- Do you need to slice, filter, or sort by the result? If yes, use a calculated column.
- Do you need the result in several visuals or reports, or in a visual that has no row order? If yes, use a measure.
- Does the result depend on the position of a row in one visual, such as previous, running, or parent? If yes, use a visual calculation.
- Do you lack permission to edit the model? A visual calculation may be the only option.
Summary of the key differences
- A calculated column is stored per row in the model and is fixed at refresh.
- A measure is computed at query time from the model and responds to filter context.
- A visual calculation is computed at query time from the visual's own data and responds to the visual's layout.
Exercise
A report author wants a column chart of monthly sales that also shows each month's difference from the previous month. The model is certified and the author cannot edit it. Which of the three options fits best, and why would a calculated column not work?
Show answer
A visual calculation fits best. The difference from the previous month depends on row position in one visual, which PREVIOUS handles directly, as in [Sales Amount] - PREVIOUS([Sales Amount]). It also needs no change to the certified model. A calculated column does not work because it is computed row by row at refresh time and cannot know which months the visual shows or what the previous row is.
Exercise
A colleague adds a calculated column to the Sales table with the formula [Profit] / [Revenue] and puts it in a matrix as a sum. The totals look wrong. What went wrong, and what should they use instead?
Show answer
The column stores a ratio for each row. Summing or averaging those ratios does not give the ratio of total profit to total revenue. They should create a measure that divides the aggregated values, for example DIVIDE(SUM(Sales[Profit]), SUM(Sales[Revenue])).