Running Total functions
This page shows how to add a running total to a table in Power Query, first across the whole table and then restarting for each group.
The example data holds monthly sales for two years:

DAX can calculate a running total in a report without much trouble. Calculating it in M instead stores the result in the data, which is worth doing when the same totals would otherwise be recalculated every time the report is used.
A running total across the whole table
Section titled “A running total across the whole table”1. Load the data into Power Query
Section titled “1. Load the data into Power Query”In the Power Query Editor, the data looks like this:

2. Add an index column
Section titled “2. Add an index column”A running total needs to know each row’s position, so that it can add up every row up to that point. An index column provides it.
- On the Add Column tab, add an Index Column that starts from 1.

3. Add a custom column
Section titled “3. Add a custom column”- On the Add Column tab, click Custom Column, and enter the formula shown below.
![The Custom Column dialog with the new column name Cumulative Total and the formula List.Sum(List.Range(Source[Sales], 0, [Index]))](/_astro/running-total-4.DedaCogv_ZfwdLY.webp)
For each row, the inner function, List.Range, takes the Sales values from the start of the column, counting as many values as the row’s Index. The outer function, List.Sum, adds them up, which gives the running total at that row.
- Click OK. The table gains a column holding the running total.

- Remove the Index column, which is no longer needed.

A running total that restarts each year
Section titled “A running total that restarts each year”The steps above produce a single running total that carries on from the first row to the last. A year-to-date total needs to restart at the start of each year, which takes a different approach built from two functions:
- fnRunningTotal returns a running total for a table, as a list. It takes two parameters: the table, and the name of the column to add up.
- fnGroupedRunningTotal splits the table into groups and adds a running total to each group separately. It takes three parameters: the table, the name of the column that defines the groups, and the name of the column to add up.
1. Create the fnRunningTotal function
Section titled “1. Create the fnRunningTotal function”This function does the same job as the query in the first part of this page, written as a function. Instead of returning the whole table with an extra column, it returns only the running total, as a list.
Its M code is as follows:

The inner let block receives two parameters, tbl for the table and sumcolumn for the name of the column to add up. It then takes these steps:
- It adds an Index column that starts at 1 and goes up by 1.
- It renames the column named by sumcolumn to Temp, so that the later steps can refer to it by a fixed name.
- It adds a Running Total column, which for each row adds up the values in Temp from the first row to the row given by the Index.
- It returns only the Running Total column, as a list, rather than the whole table.
Once the code is saved, the query becomes a function and shows its two parameters:

To test it, invoke the function with these parameters:
- tbl: the Sales query, which holds the sales data
- sumcolumn:
Sales, the name of the column to add up
It returns a list with the same running totals as the query in the first part of this page.

- Name the function fnRunningTotal.
2. Group the data
Section titled “2. Group the data”Next, build a query that calls this function once for each year.
- Start from the sales data:

- Choose Group By, grouping on Year with the operation All Rows and the new column name Data.

There is now one row per year, and the Data column holds a table of that year’s rows.

3. Why a plain custom column does not work
Section titled “3. Why a plain custom column does not work”The obvious next step does not work, and seeing why explains the step that follows.
It would be natural to call the function from a new column, using Invoke Custom Function:

Each year now has a table in Data and a list of running totals in Running Total.

The trouble comes when expanding them. Expanding only Running Total gives the correct totals, but the Month and Sales values stay inside the unexpanded Data tables, so they cannot be seen beside their totals:

Expanding only Data restores Month and Sales, but the Running Total column still holds an unexpanded list on every row:

Expanding both multiplies the rows, because every month is paired with every running total in its year:

The running total list therefore has to be added to each year’s table as an extra column before anything is expanded, so that a single expansion brings everything out together. The next step shows how.
4. Change the M code
Section titled “4. Change the M code”Go back to the query as it stood straight after the Group By step. Its M code looks like this:

- Change the M code as follows:

The inner let block works like this:
- Each year’s table,
_, is passed to a new function that takes a table as its parameter. - Table.ToColumns breaks the table into a list of its columns, each column itself a list.
&{fnRunningTotal(_, "Sales")}appends the running total list to that list of columns.- Table.FromColumns rebuilds a table from the columns. Its second argument, built with Value.Type and Table.AddColumn, supplies the column names, which are the original table’s names plus Running Total.
Each year’s table now carries its own running total.

5. Expand the Data column
Section titled “5. Expand the Data column”- Add a step that expands the Data column, with this M code:

The step expands only the columns that are needed. These are the original table’s columns plus Running Total, minus Year, because Year is already present from the Group By step.
The result has every original column, with a running total that restarts each January:

6. Turn the query into a second function
Section titled “6. Turn the query into a second function”- Finally, turn the query into a function called fnGroupedRunningTotal, by changing the code as follows:

The query becomes a function with three parameters:

It can be called from any new query like this:
= fnGroupedRunningTotal(Sales, "Year", "Sales")See also
Section titled “See also”- Inventory Utilisation uses a shorter version of the grouped running total function, which uses List.Accumulate( ) in place of List.Sum( ). Read the Inventory Utilisation page