List.Sum Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The List.Sum function in Power Query adds up all the numbers in a list and gives you the total. It skips null values, so blank cells in your data don’t break the result.

A column, the rows in a group, or a few cells from the same row can all be turned into a list. So once you know how to get that list, List.Sum covers column totals, row totals, SUMIF-style sums, and totals for each group.

Follow along with the example file

List.Sum Function Examples.xlsx

Download

List.Sum Function Syntax

List.Sum(list as list, optional precision as nullable number) as any
  • list – the list of numbers you want to add up. It can be a list you type in curly brackets, such as {10,20,30}, or a column of a table.
  • precision (optional) – how precisely the numbers are added. Leave it out for the default (Precision.Double), or use Precision.Decimal when tiny rounding errors matter (Example 9 shows when).

What it returns: The sum of the non-null values in the list. If the list has no values other than null (or is empty), it returns null instead of 0.

The List.Sum function is part of the Power Query M language, so it works wherever Power Query does in Excel: Excel 365, 2024, 2021, 2019, and 2016 for Windows, and Excel for Microsoft 365 on Mac.

When to Use the List.Sum Function

Use this function when you need to:

  • Get the total of a column as a single value
  • Add a row total across several columns, even when some cells are blank
  • Sum only the rows that meet a condition, like the SUMIF function in Excel
  • Get a total for each group (each project, region, or customer) with Table.Group
  • Pull a matching total from another table
  • Work out what share of the total each row makes up

Example 1: Add Up a List of Numbers

Let’s start with the simplest use, adding up a few numbers. Suppose a bakery sold 84, 97, 73, 110, and 92 loaves over five days, and you want the total for the week.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= List.Sum({84,97,73,110,92})

Result: 456

List.Sum adds 84, 97, 73, 110 and 92 and returns 456

Here, the curly brackets create a list of five numbers, and List.Sum adds them up to give 456.

You’ll rarely type the numbers yourself like this. But it shows the one thing List.Sum needs, which is a list, and the rest of the examples are about getting that list out of your data.

In the example file, Examples 1 and 9 type their numbers right into the query, so they don’t have a sheet. You can open them from Data > Queries & Connections.

Example 2: Get the Total of a Column

Here’s the most common use of List.Sum. Below is a list of trip expenses in an Excel Table named TripExpenses, and I want the total of the Amount column as a single number.

TripExpenses table with an Expense and an Amount column

In the code below, the Source line uses the Excel.CurrentWorkbook function to pull the TripExpenses table into Power Query by its name.

If you try this on your own data, format it as a table (Ctrl + T) and put your table’s name in place of “TripExpenses”.

Create a new blank query (as in Example 1), open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="TripExpenses"]}[Content],
Total = List.Sum(Source[Amount])
in
Total

Result: 1805

List.Sum of the Amount column returns the trip total, 1805

Here, Source is the name of the first step, and it holds the TripExpenses table. Source[Amount] takes the Amount column of that table and returns it as a list (412, 638, 74, 186, and 495), and List.Sum adds that list up.

So whenever you want a column total, put the step name, followed by the column name in square brackets, inside List.Sum. If the column name has spaces, it still goes in the same brackets, like Source[Total Cost].

List.Sum gives you one total for the whole list. If you want a running total instead (the total so far on each row), the List.Accumulate function can build that list of totals for you.

Example 3: Add a Row Total Across Several Columns

Now let’s add up values across a row instead of down a column. Below is a table named QuarterlySales with each sales rep’s sales for the four quarters, and I want a Total column with each rep’s sales for the year.

QuarterlySales table with Q1 to Q4 sales per rep and some blank quarters

Some cells are blank because a rep joined mid-year or was on leave for a quarter. In Power Query, those blank cells come in as null.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="QuarterlySales"]}[Content],
#"Added Total" = Table.AddColumn(Source,"Total",each List.Sum({[Q1],[Q2],[Q3],[Q4]}),type number)
in
#"Added Total"

Result: A new Total column (82350, 33000, 87900, 52550, and 49450)

A new Total column that adds the four quarters and skips the blank ones

Here, the Table.AddColumn function adds a Total column and runs the formula after each once for every row. For each row, {[Q1],[Q2],[Q3],[Q4]} builds a list of that row’s four quarter values, and List.Sum adds them.

Madison King has no sales in Q1 and Q2, so her list is {null, null, 15200, 17800}. List.Sum skips the two nulls and returns 33000.

If a rep had all four quarters blank, the list would hold only nulls, and List.Sum would return null for that row instead of 0.

Important: Don’t add the columns with plus signs, like [Q1]+[Q2]+[Q3]+[Q4]. In M, adding a null to a number gives null, so Madison King, Evan Bell, and Susan Williams would all get a blank (null) total.

Example 4: Sum Only the Rows That Meet a Condition (Like SUMIF)

This next one works like the SUMIF function in Excel. Below is an order log in a table named Orders, with the channel each order came from, and I want the total amount of the Online orders only.

Orders table with Order ID, Channel and Amount columns

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
OnlineTotal = List.Sum(Table.SelectRows(Source,each [Channel]="Online")[Amount])
in
OnlineTotal

Result: 882

The total of the Online orders only is 882

Here, the Table.SelectRows function keeps only the rows where Channel is “Online”. That’s four orders (ORD-301, ORD-303, ORD-304, and ORD-308).

[Amount] at the end then takes the Amount column from those four rows as a list, and List.Sum adds 245, 310, 128, and 199 to get 882.

The text comparison is case-sensitive, so “online” in lowercase would not match. For more than one condition (like SUMIFS), join them with and, for example each [Channel]="Online" and [Amount]>150.

If no row meets the condition, List.Sum gets an empty list and returns null, not 0. Example 6 shows how to turn that null into a 0.

Follow along with the example file

List.Sum Function Examples.xlsx

Download

Example 5: Get Totals for Each Group (Table.Group with List.Sum)

This is one of the most common places you’ll use List.Sum. Below is a timesheet in a table named Timesheet, with the hours spent on each task, and I want the total hours for each project in a Total Hours column.

Timesheet table with the hours spent on each task of three projects

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Timesheet"]}[Content],
#"Grouped Rows" = Table.Group(Source,{"Project"},{{"Total Hours",each List.Sum([Hours]),type number}})
in
#"Grouped Rows"

Result: One row per project with its Total Hours (Website Redesign 27, Mobile App 21, and Data Migration 14.5)

Table.Group with List.Sum gives the total hours for each project

Here, the Table.Group function groups the rows by Project. For each project, [Hours] is the list of hours from that project’s rows only, and List.Sum adds them up.

For example, the Mobile App rows have 8.5, 7, and 5.5 hours, so its total is 21.

The type number at the end sets the new column’s data type to number. Without it, the column would have no data type (Power Query calls this type any).

If you use the Group By button (Home > Group By) with the Sum operation, Power Query writes a List.Sum step like this for you. Writing it yourself is handy when you want to change it, like adding a second total.

Example 6: Sum Matching Values from Another Table

This one comes up a lot when your data is split across two tables. Below is a list of club members in a table named Members, and I want a Total Paid column with how much each member has paid.

Members table with each club member and their plan

The payments are in a separate table named Payments, on the Example 6 Payments sheet. A member can have more than one payment, and Linda Ward has none.

Payments table with one row per payment and no payments for Linda Ward

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Members"]}[Content],
Payments = Excel.CurrentWorkbook(){[Name="Payments"]}[Content],
#"Added Total Paid" = Table.AddColumn(Source,"Total Paid",each
List.Sum(Table.SelectRows(Payments,(p)=>p[Member]=[Member])[Amount]) ?? 0,
type number)
in
#"Added Total Paid"

Result: A new Total Paid column (125, 75, 90, 100, and 0)

A new Total Paid column with each member's payments added up and 0 for Linda Ward

Here, for each member, Table.SelectRows keeps the rows of the Payments table where the Member matches the member in the current row.

Both tables have a Member column, so the code needs a way to tell them apart. The (p)=> part gives each Payments row being checked the name p.

So p[Member] is the member in that Payments row, while [Member] on its own is the member in the Members row.

Then [Amount] gets those payments as a list, and List.Sum adds them. Marcus Howard has two payments (75 and 50), so his total is 125.

Linda Ward has no payments, so the list is empty and List.Sum returns null, not 0. The ?? 0 part fixes that, because the ?? operator returns the value on its right (0) whenever the value on its left is null.

If your version of Power Query gives an error on ??, use the code below instead. It stores the List.Sum result in a variable named total, and the if part turns a null total into 0, so you get the same result:

let
Source = Excel.CurrentWorkbook(){[Name="Members"]}[Content],
Payments = Excel.CurrentWorkbook(){[Name="Payments"]}[Content],
#"Added Total Paid" = Table.AddColumn(Source,"Total Paid",each
let total = List.Sum(Table.SelectRows(Payments,(p)=>p[Member]=[Member])[Amount])
in if total = null then 0 else total,
type number)
in
#"Added Total Paid"

This goes through the whole Payments table once for every member, so it can get slow when both tables are large. In that case, group the payments by member first (like Example 5), and then merge those totals into the Members table.

Example 7: Calculate Each Row’s Percent of the Total

Now let’s use the total inside a calculation. Below is a table named WebTraffic with the visits a website got from each channel, and I want a Share column with each channel’s percentage of all visits.

WebTraffic table with visits from each channel

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="WebTraffic"]}[Content],
TotalVisits = List.Sum(Source[Visits]),
#"Added Share" = Table.AddColumn(Source,"Share",each [Visits]/TotalVisits,Percentage.Type)
in
#"Added Share"

Result: A new Share column (40.00%, 15.00%, 12.00%, 25.00%, and 8.00%)

A new Share column with each channel's percentage of the 8000 total visits

Here, the TotalVisits step adds up all the visits once, which gives 8000. Then for each row, the Share column divides that row’s visits by 8000, so Organic Search gets 3200/8000, which is 40%.

Percentage.Type sets the new column as a percentage, so it shows as 40.00% instead of 0.4.

Putting the total in its own step means Power Query works it out once. You could put List.Sum(Source[Visits]) inside the each part instead, but then it can be worked out again for every row.

Example 8: Sum Numbers Stored as Text

This is a common problem with data that comes from other systems. Below is a stock count in a table named StockCount, where the Units column holds numbers stored as text, and I want the total units.

StockCount table where the Units column holds numbers stored as text

If you use List.Sum(Source[Units]) on this column, you get the error “We cannot apply operator – to types Text and Text.” That’s because List.Sum only adds numbers, and “125” in text form is not a number.

So convert the values to numbers first. Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="StockCount"]}[Content],
TotalUnits = List.Sum(List.Transform(Source[Units],Number.From))
in
TotalUnits

Result: 520

Converting the text to numbers first gives a total of 520 units

Here, the List.Transform function runs the Number.From function on every item in the Units list. That turns “125” into the number 125, and so on, and List.Sum then adds the numbers to get 520.

Number.From gives an error for text that isn’t a number, like “n/a”, and that error stops List.Sum too. So if your column has values like that, replace them with null first (Transform > Replace Values).

If you’ll use the Units column in other steps too, convert the whole column once instead. With the Source step selected, right-click the Units header and choose Change Type > Whole Number.

Power Query adds a step named Changed Type after Source. In the step after that, List.Sum(#"Changed Type"[Units]) works without any conversion.

Example 9: Avoid Tiny Rounding Errors with Precision.Decimal

This last one is about the optional second argument. Suppose a customer paid an invoice of 60.6 in three parts (10.1, 20.2, and 30.3), and you want to check that the payments add up to the invoice amount.

Create a new blank query, open the Advanced Editor, and use this code:

let
Amounts = {10.1,20.2,30.3},
Check = #table({"Precision","Matches 60.6?"},{
{"Default",List.Sum(Amounts)=60.6},
{"Precision.Decimal",List.Sum(Amounts,Precision.Decimal)=60.6}})
in
Check

Result: A small table where the Default row shows FALSE and the Precision.Decimal row shows TRUE

The default sum does not match 60.6 (FALSE) while Precision.Decimal does (TRUE)

Here, the #table function builds a two-row table so you can see both checks side by side. The first row adds the payments with the default precision, and the second uses Precision.Decimal.

By default, List.Sum adds numbers as binary floating-point values, which can’t store some decimals like 0.1 exactly. So the default sum is 60.599999999999994, which is not equal to 60.6, and the check returns FALSE.

With Precision.Decimal, the numbers are added as exact decimals, so the sum is exactly 60.6 and the check returns TRUE. Use it when you compare totals with an exact amount, like reconciling payments.

If you only need a total to display, the default is fine, because the difference is far too small to show up once the number is rounded to two decimals.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.