List.Generate
Makes a list one value at a time, from a start, for as long as a condition holds.
Group: List. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
List.Generate(initial as function, condition as function, next as function, optional selector as nullable function) as list
| Parameter | Type | Description |
|---|---|---|
initial | function | A function of no arguments, () => ..., that gives the first value. |
condition | function | A function of the current value: while it answers true, the value goes in the list and the next one is made. |
next | function | A function of the current value that gives the next one. |
selector | nullable function optional | A function of each value that gives what goes in the list; the value itself when left out. |
Returns
list. The values made, or what the selector made of each.
Remarks
- The first value that fails the condition is not in the list, and making stops there.
- A record as the value carries several things along at once, such as a counter and a balance.
Examples
Halving a stock until it is under 10
List.Generate(() => 100, each _ >= 10, each _ / 2)
{100, 50, 25, 12.5}
A loan paid down by 300 a month
List.Generate(() => [Month = 1, Balance = 1000], each [Balance] > 0, each [Month = [Month] + 1, Balance = [Balance] - 300], each [Balance])
{1000, 700, 400, 100}