Sub topic 2
Starting from Visual Calculation Templates
Assume you know how to build a matrix or chart in Power BI Desktop and can write basic DAX measures. Visual calculations run on the data already arranged in one visual, so the visual’s rows, columns, and hierarchy affect the result.
Starting from the built-in templates
Select a visual, open the Calculations section, and choose New visual calculation. Power BI offers templates such as running sum, moving average, and difference from previous. A template inserts a DAX expression that you can edit.
| Template | Common purpose | Main function |
|---|---|---|
| Running sum | Cumulative values | `RUNNINGSUM` |
| Moving average | Smoothing recent values | `MOVINGAVERAGE` |
| Difference from previous | Period-over-period change | `PREVIOUS` |
Templates are useful because they provide valid starting syntax. Check the visual’s ordering before trusting the result.
Running sums and moving averages
A running sum adds the current value to all earlier values on the calculation axis. A moving average averages a fixed number of nearby rows.
Running sum = RUNNINGSUM([Sales Amount])
Moving average = MOVINGAVERAGE([Sales Amount], 3)The second expression uses a three-row window ending at the current row. At the beginning of the axis, fewer rows may be available, so inspect the result rather than assuming every window contains three values.
Comparing with previous, next, first, and last rows
Relative functions read another position on the same axis. PREVIOUS and NEXT compare neighboring rows. FIRST and LAST let a calculation reference an endpoint.
Change vs previous = [Sales Amount] - PREVIOUS([Sales Amount])
Change vs next = NEXT([Sales Amount]) - [Sales Amount]The first row has no previous value, and the last row has no next value. Those cells can therefore be blank.
Choosing direction with `axis`
By default, Power BI uses the visual’s default axis. Specify ROWS or COLUMNS when the calculation must move in a particular direction.
Column running sum = RUNNINGSUM([Sales Amount], COLUMNS)Restarting with `reset`
Use reset when a calculation should restart at a hierarchy boundary, such as each product category. The exact reset level depends on the hierarchy in the visual.
Exercise
Which function should you start with for a cumulative monthly sales line?
Show answer
Use RUNNINGSUM, and confirm that the visual axis is ordered by month.