List.Generate

This process involves creating a list of values through the use of specified functions. The first function generates an initial value, which is then evaluated against a condition. If the value meets t...

Syntax

List.Generate(initial as function, condition as function, next as function, optional selector as nullable function) as list

About

This process involves creating a list of values through the use of specified functions. The first function generates an initial value, which is then evaluated against a condition. If the value meets the condition, it is added to the list. The next value is generated by using the approved value as input to the next function. The process continues until a value fails to meet the condition. Optionally, a selector parameter can be included to modify the items in the final list.

Explanation

Create a list that starts at ten, decreases by one each time, and ensures that each item is greater than zero. Usage: Power Query MList.Generate(() => 10, each _ > 0, each _ - 1) Output: {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} Generate a list of records that contain x and y values where x represents a value and y represents a list. x should be less than 10 and indicate the number of items in the list y. Once the list is created, return only the x values. Usage: Power Query MList.Generate( () => [x = 1, y = {}], each [x] < 10, each [x = List.Count([y]), y = [y] & {x}], each [x] ) Output: {1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}