Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query List.Accumulate: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

List.Accumulate takes a list and folds it into a single value by applying a function repeatedly, carrying an accumulator from one item to the next. It is the M equivalent of a loop with a running total. You need Power Query available (Excel, Power BI, or another host) and a basic familiarity with the Power Query Editor and the M formula language.

What You'll Need

  • Access to Power Query (Excel 2016+ or Power BI Desktop).
  • A list to process, or the ability to create one (for example, a column converted to a list).
  • Basic understanding of M functions and the Advanced Editor (optional but helpful).

Step-by-Step

  1. Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data. Menu labels can differ slightly by version or region.
  2. Create or load a list. You can type a list directly in a query: {1, 2, 3, 4}, or reference a column as a list using the column name in square brackets, e.g., MyTable[MyColumn]. Ensure the list is a simple list of values, not a table.
  3. Decide on the accumulator and the operation. The accumulator is the running result. For a sum, start with 0 and add each item. For a text concatenation, start with "" and combine strings. For a running product, start with 1 and multiply.
  4. Write the List.Accumulate formula. The syntax is: List.Accumulate(list as list, seed as any, accumulator as function) as any. The accumulator function receives two arguments: the current state (accumulator) and the current item. It must return the new state. Example for sum: List.Accumulate({1,2,3,4}, 0, (state, current) => state + current) returns 10.
  5. Enter the formula. In the formula bar or Advanced Editor, type your List.Accumulate expression. You can also use the "Custom Column" dialog with the full expression. For a custom column, reference the list directly or use a variable.
  6. Check the result. After entering, the query will evaluate and show the single value. If you used it in a custom column, each row will compute its own accumulation.
  7. Use with table columns. To accumulate over a column, first convert the column to a list: List.Accumulate(MyTable[MyColumn], 0, (state, current) => state + current). This gives a single total. For a running total per row, you need a different approach (e.g., List.Accumulate with a list of lists or using Table.AddIndexColumn and then accumulating up to that index).
  8. Handle errors. If the list contains nulls or errors, the accumulator function may fail. Wrap the current item in try ... otherwise or filter the list first. For example: List.Accumulate(List.Select(MyList, each _ <> null), 0, (state, current) => state + current).
  9. Test with a small list. Before applying to large data, test with a few items to verify the logic. Use the Advanced Editor to see the full M code and adjust.
  10. Apply and load. Once satisfied, close and load the query to a worksheet or data model.

Common Mistakes

  • Forgetting the seed value. The seed is required; if omitted, the function errors. Always provide a starting value.
  • Using the wrong argument order. The accumulator function takes (state, current), not (current, state). Swapping them gives wrong results.
  • Not returning a value from the accumulator function. The function must return the new state; a missing return causes an error.
  • Assuming List.Accumulate works like a loop with side effects. It is purely functional; you cannot modify external variables inside it.
  • Using it for running totals without index. List.Accumulate processes the whole list at once; to get a running total per row, you need to accumulate over a subset for each row, which is inefficient. Consider other patterns like List.Generate or Table.AddIndexColumn with a custom function.
  • Ignoring data types. If the list contains mixed types, the accumulator may produce unexpected results. Ensure consistent types or convert explicitly.

Tips

  • Use descriptive variable names. Instead of (s, c) => s + c, use (state, current) => state + current for readability.
  • For complex accumulations, define a custom function. Write a separate function for the accumulator logic and call it from List.Accumulate. This makes the code reusable and easier to debug.
  • Combine with List.Transform for preprocessing. Clean or convert the list before accumulating, e.g., List.Accumulate(List.Transform(MyList, each _ * 2), 0, (s,c) => s + c).
  • Use List.Accumulate for string building. Start with an empty string and concatenate with a delimiter: List.Accumulate(MyList, "", (s,c) => if s = "" then c else s & ", " & c).
  • Be mindful of performance. For very large lists, List.Accumulate is efficient, but avoid nested accumulations that create many intermediate lists.
  • Document your accumulator logic. Add comments in the Advanced Editor to explain what the seed and function do.

Troubleshooting

If List.Accumulate returns an error, check the following:

  • "We cannot convert a value of type ... to type ..." – The seed type and the accumulator function's return type may not match. Ensure the seed is of the same type as the expected result. For example, if you start with 0 (number) but the function returns text, you'll get an error.
  • "The field '...' of the record wasn't found" – If your list contains records, ensure you reference fields correctly in the accumulator function.
  • Function not recognized – Check spelling: it's List.Accumulate (capital L, capital A). M is case-sensitive.
  • Result is null – If the list is empty, List.Accumulate returns the seed. That's expected. If you get null unexpectedly, check if the list itself is null.
  • Performance issues – If the query is slow, consider whether List.Accumulate is the right tool. For simple sums, List.Sum is faster. For running totals, other patterns may be more efficient.
  • Debugging – Break down the expression: first test the list, then test the accumulator function with a single item using a manual call, e.g., MyFunction(seed, firstItem).

Frequently Asked Questions

What does List.Accumulate do in Power Query?

It iterates over a list and applies a function to each item, carrying an accumulator (a running state) from one iteration to the next, and returns the final state.

Can I use List.Accumulate to create a running total column?

Not directly for a column, because List.Accumulate returns a single value. To create a running total per row, you need to accumulate over a subset of the list for each row, which is possible but often inefficient. Alternative approaches like List.Generate or Table.AddIndexColumn with a custom function are more common.

What is the seed value?

The seed is the initial value of the accumulator. It must be provided and should match the type of the final result. For sum, use 0; for product, use 1; for text, use "".

How do I handle nulls in the list?

Filter out nulls before accumulating: List.Accumulate(List.Select(MyList, each _ <> null), 0, (s,c) => s + c). Or handle them inside the accumulator function with if c = null then s else s + c.

Is List.Accumulate case-sensitive?

Yes, M is case-sensitive. Use exact capitalization: List.Accumulate.

Can I use List.Accumulate with a table column?

Yes, by converting the column to a list: List.Accumulate(MyTable[MyColumn], 0, (s,c) => s + c). This returns a single value for the entire column.