Sub topic 6
Running Sums and Moving Averages
This chapter shows how to build a cumulative total and a smoothed trend line with visual calculations. It assumes you can already create a visual calculation (the previous chapters covered the calculation editing experience). It also assumes you know basic DAX measures and how a matrix lays out rows and columns.
Why these two calculations matter
Running totals and moving averages both depend on row order. A running sum answers "how much so far?" A moving average answers "what is the recent trend, with the noise smoothed out?"
In a regular model you write these with CALCULATE, FILTER, DATESYTD, or window functions over a date table. Visual calculations replace that work with a function that reads the rows already in your visual. You don't need a date table relationship or filter-context manipulation. The calculation looks at the visual's rows in their current order.
The example data
The examples use a matrix with Month on rows and Sales Amount as the value. Each month is one row.
| Month | Sales Amount |
|---|---|
| Jan | 100 |
| Feb | 120 |
| Mar | 90 |
| Apr | 150 |
| May | 130 |
Running sums with RUNNINGSUM
RUNNINGSUM adds up a value cumulatively down the rows of the visual. Each row shows its own value plus all values before it.
Running sum = RUNNINGSUM([Sales Amount])The full signature is:
RUNNINGSUM(<column>, [<axis>], [<blanks>], [<reset>])Only the first argument is required. <column> is the field to accumulate. It can be a measure, such as [Sales Amount], or a column already in the visual. The remaining arguments are optional, and later chapters cover axis and reset in detail.
For the example data, the result is:
| Month | Sales Amount | Running sum |
|---|---|---|
| Jan | 100 | 100 |
| Feb | 120 | 220 |
| Mar | 90 | 310 |
| Apr | 150 | 460 |
| May | 130 | 590 |
The last row equals the sum of the whole column. That is a quick check that the calculation is correct.
Sort order controls the result
The running sum follows the visual's current sort. If you sort the matrix by Sales Amount descending, the running sum accumulates in that order instead of calendar order. The calculation has no idea what a month is. It sees row positions only.
Sort by the field that defines your sequence, usually date or month number, before you trust a running total.
The blanks parameter
The optional blanks parameter controls how empty values are treated. The values are DEFAULT, SKIP, and KEEP. Use it when some rows have no data. Check what each setting does on your own visual before relying on it. A visual with gaps can differ from the example above.
Moving averages with MOVINGAVERAGE
MOVINGAVERAGE averages a value over a sliding window of rows.
Moving average = MOVINGAVERAGE([Sales Amount], 3)The signature is:
MOVINGAVERAGE(<column>, <windowSize>, [<includeCurrent>], [<axis>], [<blanks>], [<reset>])The second argument, windowSize, is the number of rows in the window. With the setting above, each row averages three rows ending at the current row: the two rows before it plus itself.
For the example data:
| Month | Sales Amount | Moving average (3) |
|---|---|---|
| Jan | 100 | 100.00 |
| Feb | 120 | 110.00 |
| Mar | 90 | 103.33 |
| Apr | 150 | 120.00 |
| May | 130 | 123.33 |
The March value is (100 + 120 + 90) / 3. The April value is (120 + 90 + 150) / 3.
The first rows have incomplete windows
January has no earlier rows, and February has only one. Those rows cannot fill a window of three. The calculation averages the rows that exist, as the table shows. A smoothed line therefore looks less smooth at its start. If you need only full windows, you cannot skip the early rows with the function alone. Hide them with a filter on the visual, or choose a measure-based approach (see the limitations chapter).
What the window covers
The window moves one row at a time. Each row's result belongs to the last row in its window.
Choosing a window size
A larger window gives a smoother line that reacts more slowly to change. A smaller window follows the data more closely but keeps more noise. For monthly data, 3 or 12 are common because they match a quarter and a year. For daily data, 7 removes the weekday pattern. Pick the size from the cycle you want to remove, not from habit.
The includeCurrent argument controls whether the current row is part of the window. It defaults to including the current row, which matches the example above.
Building both in one visual
Here is the process in Power BI Desktop.
- Add a matrix with
Monthon rows andSales Amountas a value. - Sort the matrix by month in calendar order.
- Select the visual, then choose New calculation on the ribbon.
- In the formula bar, enter
Running sum = RUNNINGSUM([Sales Amount])and press Enter. - Add a second calculation:
Moving average = MOVINGAVERAGE([Sales Amount], 3). - Compare the new columns with the table above.
A line chart works the same way. Put Month on the X-axis, add Sales Amount, and then add the two calculations as values. The chart draws the raw series, the cumulative curve, and the smoothed trend in one visual.
Referring to other calculations
A visual calculation can use other visual calculations. For instance, you can compute the share of the total reached so far:
Running share = DIVIDE([Running sum], [Sales Amount] + [Running sum] - [Running sum])That formula is needlessly roundabout, so don't copy it. A clearer approach is to compute the running sum first, then divide by the grand total using the hierarchy functions that a later chapter introduces. The point here is that calculations in the same visual can build on each other, and each one runs against the visual's data.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Running sum goes up and down in strange order | Visual sorted by value | Sort by the date or month number field |
| Moving average starts low or looks flat in early rows | Window not yet full | Expect partial windows, or filter out early rows |
| Results change when you add a filter or slicer | The calculation sees only the rows currently in the visual | Test with the filter state your readers will use |
| Calculation refuses to accept a column | The field is not in the visual | Add the field to the visual, and hide it if needed |
| Total row shows an unexpected value | The calculation runs across visual rows, so a total row is not a final running value | Check the total row on your own visual and decide whether to keep it |
Checking your work
Verify results by hand on a few rows. The final running-sum row should equal the visual's total of Sales Amount. A moving average over a window of 1 should equal the original value. Both checks catch most mistakes in field choice and sort order.
Exercise
A matrix shows quarterly sales for Q1 to Q4 as 200, 300, 100, 400, sorted in calendar order. What does RUNNINGSUM([Sales Amount]) return for each row, and what does MOVINGAVERAGE([Sales Amount], 2) return for Q3?
Show answer
The running sum is 200, 500, 600, 1000. The window for Q3 holds Q2 and Q3, so the moving average is (300 + 100) / 2 = 200.
Exercise
A colleague sorts a monthly matrix by Sales Amount descending, adds a running sum, and reports that the cumulative total "peaks early". What is wrong, and how do you fix it?
Show answer
The running sum follows the visual's row order, so it accumulates the largest months first. Sort the visual by the month field in calendar order and the running sum will accumulate over time.