Code Generator | M (Power Query)

listAccumulateExample

This function showcases the usage of List.Accumulate in M (Power Query) to perform a cumulative operation on a list of values. It takes a list of values, an initial accumulator value, and an accumulator function, using List.Accumulate to execute


Empty image or helper icon

Prompt

List.accumulate exemple

Answer

/*
Function: listAccumulateExample

This function demonstrates the usage of List.Accumulate in M (Power Query) to perform a cumulative operation on a list of values.

Parameters:
values (list): The input list of values on which the cumulative operation will be performed.
accumulator (any): The initial value for the accumulator.
accumulatorFunction (function): The function to be applied to each element of the list to update the accumulator.

Return Type: any

Example: 
listAccumulateExample({1, 2, 3, 4, 5}, 0, (accumulator, current) => accumulator + current)
Result: 15
*/

listAccumulateExample = (values as list, accumulator as any, accumulatorFunction as function) =>
    List.Accumulate(values, accumulator, (state, current) => accumulatorFunction(state, current));

Usage Example:

// Applying listAccumulateExample function
listAccumulateExample({1, 2, 3, 4, 5}, 0, (accumulator, current) => accumulator + current)
// Output: 15

In this example, the listAccumulateExample function takes a list of values, an initial accumulator value, and an accumulator function. The List.Accumulate function is used to perform a cumulative operation on the list by applying the accumulator function to each element of the list. The result of the cumulative operation is returned as the output.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This function showcases the usage of List.Accumulate in M (Power Query) to perform a cumulative operation on a list of values. It takes a list of values, an initial accumulator value, and an accumulator function, using List.Accumulate to execute the cumulative operation and return the result.