List.Generate in Power Query – A Complete Guide With Examples

If you don’t know what  List.Generate is then see the docs from Microsoft first. If you look carefully then you will notice that it has four parameters and all are of type function.

Here is the function definition and their param breakdown.

List.Generate(initial as function, condition as function, next as function, optional selector as nullable function) as list
  • initial as function – > This will generate the first initial value(candidate value) to work on.
  • condition as function – > This will check if previously generated value is valid or not. If valid then add that as approved value. ( See first example for more clarity).
  • next as function -> this will generate next value(candidate value , need to test to be approved) using the previously approved value. And we need to check for the condition again and pass that to next function if approved. But if it fails to be valid then it will stop the list generation and that will not be listed in the final result.
  • optional selector as nullable function -> This is an optional argument to transform the approved result.

We will start with the first example from Microsoft and try to test for our different scenarios to learn in depth.

Here is the first example :

				
					List.Generate(() => 10, each _ > 0, each _ - 1)
				
			

Now let’s break down for parameters

  • () = > 10 -> initial function. So it will start with 10(Candidate value).
  • each _ >0 -> condition function. So basically it is like this 10 >0 which is true. So it is now approved value. So we can pass this 10 to the next function.
  • each _ – 1 -> next function. So _ represent 10. 10 -1 will be 9 which is our next candidate value ( not approved). 

Leave a Reply

Subscribe to Blog via Email

Enter your email to subscribe to this blog and receive notifications of new posts by email.

You may also like

Please login to Continue Reading