List.Accumulate Function of Power Query
The List.Accumulate function in Power Query is used for iterative calculations or transformations on a list. It allows you to accumulate a result by applying a function to each element of the list, passing along an intermediate result (often called an "accumulator") as you go.
Syntax
The function has the following parameters:
1. list: The list of values we want to process.
2. seed: The initial value of the accumulator. This is the starting point for our accumulation.
3. accumulator: A function that takes two arguments:
o The current value of the accumulator.
o The current element of the list.
The function returns the updated value of the accumulator.
How It Works 1. The function starts with the seed as the initial value of the accumulator.
2. It then iterates over each element in the list, applying the accumulator function to the current accumulator value and the current list element.
3. The result of the accumulator function becomes the new value of the accumulator for the next iteration.
4. After processing all elements in the list, List.Accumulate returns the final value of the accumulator.
Example: Summing a List of Numbers. Suppose we have a list of numbers, and we want to calculate their sum.
Power Query M
let numbers = {1, 2, 3, 4, 5}, sum = List.Accumulate(numbers, 0, (acc, current) => acc + current) in sum
- list: {1, 2, 3, 4, 5}
- seed: 0 (initial sum)
- accumulator: (acc, current) => acc + current
Explanation:
- Start with acc = 0.
- Iterate through the list:
- acc = 0 + 1 = 1
- acc = 1 + 2 = 3
- acc = 3 + 3 = 6
- acc = 6 + 4 = 10
- acc = 10 + 5 = 15
- Final result: 15