Visual calculations in Power BI: DAX on the visual

What visual calculations in Power BI are, how they differ from measures, RUNNINGSUM, MOVINGAVERAGE, PREVIOUS, COLLAPSE and EXPAND, the Axis and Reset parameters, templates, hiding helper fields, and when a measure is still the better choice.

Short answer: a visual calculation is a DAX formula stored on one visual, calculated on the numbers the visual already shows. Running sum = RUNNINGSUM([Sales Amount]) adds up the rows of the visual from top to bottom, with no date table logic. They can refer to the visual's structure: the previous row, the parent level, the grand total. Because they work on aggregated data, they are often faster than the equivalent measure. They live on the visual, not in the model, so other visuals cannot reuse them.

A running total by month as a measure needs a date table, DATESYTD or a FILTER over ALLSELECTED, and care with the sort order. As a visual calculation it is one word: RUNNINGSUM. The trade is reuse: the measure works everywhere, the visual calculation only where it was written.

What does a visual calculation look like?

MonthSales AmountRunning sumVersus previous
Jan8080
Feb95175+15
Mar90265-5
Apr118383+28
May130513+12
A matrix by month with two visual calculations
Running sum = RUNNINGSUM ( [Sales Amount] )

Versus previous = [Sales Amount] - PREVIOUS ( [Sales Amount] )

How do you add one?

  1. Select a visual (a table, matrix or chart with the fields you need).
  2. Home, New visual calculation, or right-click the visual, New calculation. The visual calculations editor opens with the visual matrix on top.
  3. Type the formula, or pick a template: Running sum, Moving average, Percent of parent, Percent of grand total, Average of children, Versus previous, Versus next, Versus first, Versus last, and two lookup templates.
  4. Fields used only to feed a calculation can be hidden on the visual: they stay available to the calculation but do not show.

A visual calculation can refer to columns, measures, or other visual calculations that are in the same visual. A model field must be on the visual before a calculation can use it.

Which functions are made for them?

FunctionDoesExample
RUNNINGSUMrunning total along an axisRUNNINGSUM([Sales Amount])
MOVINGAVERAGEaverage over a windowMOVINGAVERAGE([Sales Amount], 2)
PREVIOUS, NEXTthe row before or after[Sales Amount] - PREVIOUS([Sales Amount])
FIRST, LASTthe first or last row of the axisDIVIDE([Sales Amount], FIRST([Sales Amount]))
COLLAPSEthe value one level upDIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))
COLLAPSEALLthe grand total of the axisDIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))
EXPAND, EXPANDALLevaluate one level down, or at the leaf levelEXPAND(AVERAGE([Sales Amount]), ROWS)
RANGEa slice of rows of the axisSUMX(RANGE(3), [Sales Amount])
LOOKUP, LOOKUPWITHTOTALSa value at another place in the visual matrixLOOKUP(SUM([Sales Amount]), [Category], "Bikes")
Functions for visual calculations

What do Axis and Reset do?

Axis says which way to go through the visual matrix: ROWS, COLUMNS, ROWS COLUMNS, COLUMNS ROWS, or including small multiples (ROWS ROWPAGES). The default is the first axis of the visual.

Reset restarts the calculation at a level of the hierarchy. With Year and Month on rows:

Running sum per year = RUNNINGSUM ( [Sales Amount], HIGHESTPARENT )

HIGHESTPARENT (equivalent to 1) restarts at each year; LOWESTPARENT (-1) restarts at the immediate parent; NONE (0, the default) never restarts. Reset needs several levels on the axis; with a single field, use PartitionBy instead. Microsoft's advice: prefer Axis and Reset to follow the visual's structure, and use OrderBy and PartitionBy to pin a calculation to specific fields whatever the layout.

Visual calculation or measure?

ChooseWhen
visual calculationthe result belongs to one visual: running totals, differences between rows, percent of parent in this matrix
measurethe number is reused in several visuals, in tooltips, cards, other measures, or by other report authors
measurethe logic depends on the model, not on the visual: year over year with a date table, customer counts across tables

The custom totals of tables and matrices are built on visual calculations: picking "Sum" for a total creates one.

What goes wrong?

  • Reusing a visual calculation elsewhere. It exists only on its visual; copy the visual or write a measure.
  • Sort order. PREVIOUS and RUNNINGSUM follow the visual's order on the axis; a visual sorted by value runs the total in value order.
  • Fields missing from the visual. A calculation can only use fields placed on the visual; add them and hide them.
  • Format. Field formatting does not carry over; format the visual calculation itself.
  • Reset on one level. It needs a hierarchy; with one field use PartitionBy.

Where can you check this?

Where do you start?

Add a Running sum and a Versus previous template to one matrix, then compare the time it took with writing the measures. Visual calculation functions in DAX goes through each function with its parameters.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.