Sub topic 6
Running sums and moving averages
This chapter assumes you can add a visual calculation to a table or matrix and that you know basic DAX measures. It builds on the idea that a visual calculation reads the rows of the visual, not the data model.
What a running calculation does
A running calculation produces a value for each row from that row and the rows before it. Visual calculations provide two functions for this:
RUNNINGSUMadds values cumulatively from the first row to the current row.MOVINGAVERAGEaverages the current row with a fixed number of rows before it.
Both read the visual's rows in order. If the rows are sorted by month, the calculation runs by month. If you sort the visual differently, the results change with it.
Running sum
Add this as a new visual calculation on a table with Month and Sales Amount:
Running sum = RUNNINGSUM([Sales Amount])Each row shows its own sales plus all the sales above it. The last row equals the total of the column.
Moving average
A moving average smooths short-term swings by averaging a window of rows:
Moving average = MOVINGAVERAGE([Sales Amount], 3)The second argument is the window size. Here it is 3 rows, ending at the current row. In the first rows, fewer than three rows exist, so the average covers only the rows that are available.
With four months of sales, the results look like this:
| Month | Sales Amount | Running sum | Moving average (3) |
|---|---|---|---|
| Jan | 10 | 10 | 10 |
| Feb | 20 | 30 | 15 |
| Mar | 30 | 60 | 20 |
| Apr | 40 | 100 | 30 |
April is (20 + 30 + 40) / 3 = 30. January has only itself, so its average is 10.
Common errors
Most problems come from the layout of the visual, not from the formula.
- Wrong row order. The functions follow the visual's axis. If a table is sorted by Sales Amount instead of Month, the running sum accumulates in that order and the numbers are not a time series.
- Missing the axis in the visual. A card or a visual with a single row gives one value, so there is nothing to accumulate.
- Expecting totals to add up. The total row of a running sum shows the final value, not a sum of the column. Check it before you present it.
- Window size confusion. The window counts rows in the visual, not days or months. If your visual skips a month, the window spans the rows that are shown, not the calendar.
- Filtered data. Rows removed by a filter are not in the visual, so they do not enter the calculation.
Check your work
After you write the formula, compare the first and last rows against values you can verify by hand. The first running sum must equal the first Sales Amount. The last running sum must equal the sum of all rows. For a moving average, confirm one full-window row by calculating it manually, as with April above.
Exercise
A table shows Q1 to Q4 sales of 100, 200, 300 and 400 in that order. What does MOVINGAVERAGE([Sales Amount], 2) return for Q3, and what does RUNNINGSUM([Sales Amount]) return for Q4?
Show answer
Q3 moving average is (200 + 300) / 2 = 250, because the window of two rows ends at the current row. Q4 running sum is 100 + 200 + 300 + 400 = 1000.