Calculate Running AVERAGE in Power Query Excel

Choose the window before writing the query: every record so far, or the current record plus two preceding records. The example calculates both and keeps missing observations separate from measured zeros.

Exact answer

In Power Query, sort the records, add a zero-based index, and average a prefix of the Units list with List.Average(List.RemoveNulls(List.FirstN(Values,[Row]+1))). For the last three rows, select a List.Range window before removing nulls. Both calculations include the current record.

On this page8
▤Annotated stepsExcel
1

Open a blank query in Power Query Editor and open Advanced Editor. Work on a disposable query while checking the sample.

2

Paste the complete M query from the worked example. It constructs its own seven-record source.

3

Confirm that ReadingID 101 appears first and that ReadingID 102 precedes 103 at the repeated timestamp.

4

Check RunningAverage and Last3RowsAverage against every expected row, including null and zero cases.

5

For workbook data, replace only the Source step with the supplied Readings table expression and confirm the column types.

6

Load the result to a separate output table. Refresh after adding or changing source readings; check the output again.

Compare a cumulative average with a last-three-row window

The complete M query below creates its own unsorted sample. In Power Query Editor, open a blank query and paste it into Advanced Editor. Compare the output with the table; no worksheet input or external file is needed for this fixture.

Complete sample M query

let
    Source = #table(
        type table [ReadingID = number, RecordedAt = datetime, Units = nullable number],
        {
        {106, #datetime(2026, 9, 8, 9, 0, 0), 30},
        {102, #datetime(2026, 9, 2, 9, 0, 0), 10},
        {107, #datetime(2026, 9, 9, 9, 0, 0), 60},
        {101, #datetime(2026, 9, 1, 9, 0, 0), null},
        {105, #datetime(2026, 9, 6, 9, 0, 0), null},
        {104, #datetime(2026, 9, 4, 9, 0, 0), 20},
        {103, #datetime(2026, 9, 2, 9, 0, 0), 0}
        }
    ),
    Sorted = Table.Sort(Source, {{"RecordedAt", Order.Ascending}, {"ReadingID", Order.Ascending}}),
    Indexed = Table.AddIndexColumn(Sorted, "Row", 0, 1, Int64.Type),
    Values = List.Buffer(Indexed[Units]),
    Running = Table.AddColumn(Indexed, "RunningAverage", each
        List.Average(List.RemoveNulls(List.FirstN(Values, [Row] + 1))),
        type nullable number),
    LastThree = Table.AddColumn(Running, "Last3RowsAverage", each
        let
            count = [Row] + 1,
            start = if count > 3 then count - 3 else 0,
            size = if count < 3 then count else 3,
            window = List.Range(Values, start, size)
        in
            List.Average(List.RemoveNulls(window)),
        type nullable number),
    Result = Table.RemoveColumns(LastThree, {"Row"})
in
    Result

Optional Source replacement for an Excel table named Readings

Source = Table.TransformColumnTypes(
    Excel.CurrentWorkbook(){[Name="Readings"]}[Content],
    {{"ReadingID", Int64.Type}, {"RecordedAt", type datetime}, {"Units", type number}}
),
Expected sorted result: null means no numeric observation
ReadingIDRecordedAtUnitsRunningAverageLast3RowsAverage
1012026-09-01 09:00:00nullnullnull
1022026-09-02 09:00:00101010
1032026-09-02 09:00:00055
1042026-09-04 09:00:00201010
1052026-09-06 09:00:00null1010
1062026-09-08 09:00:00301525
1072026-09-09 09:00:00602445

If columns are cut off, scroll the table sideways.

  • The first row contains only null, so both averages are null. At ReadingID 103, 10 and 0 give 5: zero remains a measured value.
  • ReadingID 106 has a cumulative average of (10+0+20+30)/4 = 15. Its last three rows contain 20, null and 30, so that window gives 25.
  • At ReadingID 107, the running result is 120/5 = 24. The last three rows contain null, 30 and 60, so the window gives 45. Removing nulls before selecting the three-row window would select different records.
  • Rows with the same timestamp are ordered by ReadingID. Use a stable, unique source key when timestamps repeat; identical values for both sort keys do not define a reliable tie order.
  • To use workbook data, create an Excel table named Readings with ReadingID, RecordedAt and Units columns. Replace the complete sample Source = #table(...) step with the optional Source expression, keeping its trailing comma. A worksheet tab named Readings alone is not enough.
  • The window includes the current row and up to two preceding rows. It does not mean three calendar days or three nonblank measurements. For a seven-day condition, use the related date-window guide.
  • Refresh the query after source changes. Type errors and error values remain errors; do not turn them into zero to make an average appear. This prefix-based example scans repeated windows and is not a performance benchmark for large datasets.
  • The expected numbers were calculated independently. The M example has not been executed in native Excel or the Power Query engine.

Documentation reviewed .

Microsoft: Table.SortMicrosoft: Table.AddIndexColumnMicrosoft: Table.AddColumnMicrosoft: List.FirstNMicrosoft: List.RangeMicrosoft: List.RemoveNullsMicrosoft: List.AverageMicrosoft: List.BufferMicrosoft: Excel.CurrentWorkbookMicrosoft: manage queries and Advanced EditorMicrosoft: Power Query Editor for Mac

Excel for Windows: Open a blank query in Power Query Editor, then use Advanced Editor for the M code. Microsoft query-editor instructions.Excel for Mac: Microsoft documents the query editor for Microsoft 365 version 16.69 or later. Use Data > Get Data (Power Query) > Launch Power Query Editor. If that command is missing, check the version and subscription before following the M workflow. Microsoft Mac instructions.

What determines a running average?

A cumulative average uses every earlier record and the current record in a defined sort order. A moving average retains only the last N records. Neither describes a date window unless dates explicitly control membership. Sort first by RecordedAt and then by a unique ReadingID so repeated timestamps have a defined order.

Check numeric zeros and missing measurements

The fixture includes a numeric zero, two null observations and repeated timestamps. A cumulative average ends at 24; the final three-row average ends at 45. Missing observations occupy a row position but do not enter the numeric sum or divisor. List.RemoveNulls is applied after choosing the row window, so a missing row does not pull in an older measurement.

Power Query and worksheet averages are separate tasks

This is Power Query M in the query editor, not an Excel cell formula or chart trendline. The result changes when the query refreshes. For category conditions across calendar dates, use the related worksheet AVERAGEIFS guide and define the date boundaries explicitly. An Excel installation with Power Query Editor is required for this workflow; availability of that editor has not been verified for every platform or version.

Common mistakes

  • Adding the index before sorting makes the calculation follow the wrong row order.
  • Removing missing rows before selecting the window changes a last-three-row average into a different task.
  • Replacing null with zero changes both the data and the divisor.
  • Treating three records as three days ignores gaps and repeated timestamps.
  • Typing M into an Excel worksheet cell does not create a Power Query transformation.

Frequently asked questions

Does running mean cumulative or moving?

Both names occur in requests. This page shows RunningAverage using every record so far and Last3RowsAverage using at most three rows. Choose the definition required by your report.

Why is the first result null?

The first record has no numeric measurement. After nulls are removed the list is empty, and List.Average returns null. Zero would assert a measurement that the source does not contain.

Can I use a worksheet named Readings?

The optional source expression selects an Excel table named Readings, not the worksheet tab. Give the table that name and keep the required column headers.

Does the calculation reset for each category?

No. This sample follows one ordered stream. Separate per-category calculations require grouping and applying the ordering and window inside each group; a filter or an extra text column does not establish that behavior.