Skip to content

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:

An Excel table with Year, Month and Sales columns, holding one row per month from January 2019 to December 2020

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.

In the Power Query Editor, the data looks like this:

The sales data in the Power Query Editor, with Year, Month and Sales columns and 24 rows

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.

The table with an Index column numbering the rows from 1 to 24

  • 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]))

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.

The table with a Cumulative Total column rising from 40 in January 2019 to 1317 in December 2020

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

The finished table with Year, Month, Sales and Cumulative Total columns

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.

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 Advanced Editor for fnRunningTotal, a function of tbl and sumcolumn that adds an index, renames the sum column to Temp, adds a Running Total column using List.Sum and List.Range, and returns that column

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:

The Enter Parameters pane for the function, with a tbl drop-down, a sumcolumn text box and an Invoke button

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.

A list of 24 values running from 40 to 1317

  • Name the function fnRunningTotal.

Next, build a query that calls this function once for each year.

  • Start from the sales data:

The sales data with Year, Month and Sales columns

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

The Group By dialog, grouping by Year, with the new column name Data and the operation All Rows

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

The grouped table with rows for 2019 and 2020, each holding a Table in the Data column, and a preview of the 2020 table below

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:

The Invoke Custom Function dialog, creating a Running Total column with fnRunningTotal, where tbl is the Data column and sumcolumn is Sales

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

The grouped table with a Data column of tables and a Running Total column of lists, and a preview of the 2020 list starting 74, 107, 174, 259

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:

The table after expanding Running Total, with Year, Data and Running Total columns, where the total restarts at 74 in January 2020

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

The table after expanding Data, with Year, Month and Sales columns and a Running Total column showing List on every row

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

The table after expanding both columns, where January 2019 is repeated twelve times against each of the twelve running totals

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.

Go back to the query as it stood straight after the Group By step. Its M code looks like this:

The Advanced Editor showing Source = Sales and a Table.Group step that groups on Year and returns each _ as the Data column

  • Change the M code as follows:

The Advanced Editor showing the Table.Group step with each _ replaced by a function of _ as table, which uses Table.FromColumns and Table.ToColumns to append fnRunningTotal(_, “Sales”)

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.

The grouped table with a preview of the 2020 table, which now has Year, Month, Sales and Running Total columns, the total starting at 74

  • Add a step that expands the Data column, with this M code:

The Advanced Editor with an Expanded Data step that uses Table.ExpandTableColumn on Data, with the column list built by List.RemoveItems from Table.ColumnNames(Sales) plus Running Total, minus Year

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:

The finished table with Year, Month, Sales and Running Total columns, where the total reaches 609 in December 2019 and restarts at 74 in January 2020

  • Finally, turn the query into a function called fnGroupedRunningTotal, by changing the code as follows:

The Advanced Editor for fnGroupedRunningTotal, now a function of tbl, groupbycolumn and sumcolumn, with those parameters used in place of Sales and Year

The query becomes a function with three parameters:

The Enter Parameters pane for fnGroupedRunningTotal, with tbl, groupbycolumn and sumcolumn boxes

It can be called from any new query like this:

= fnGroupedRunningTotal(Sales, "Year", "Sales")
  • 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