75 - Recursion in Power Query

Published: 19 June 2024
on channel: WesleySon
775
9

An example of a recursive function in Power Query: I'm looping through all the numbers in a text string and adding them together.

There are two "tricks" to recursion:
1) Start out with your break-out condition. In my case, I want to stop when my looper is 0, because I've gone through all the numbers in my text-string
2) Make sure that when you recursively call the function, you have to change your looper - either increase or decrease it. In my case, I go through one character at a time and I move to the left, so I do looper minus 1

Other than that, you call the function using "+ @NameOfFunction(argument1)"

If you actually have to do this in real life, I'd instead split the only-numbers text string into a list using Text.Split, then using List.Sum, i.e.
1) Text.Select(myText1, {"0" .. "9"})
2) Text.ToList(myText2)
The list is a list of text, not list of numbers. We need type conversion first
3) List.Transform(myText3, each Number.From(_))
4) List.Sum(myText4)


00:00 Example of recursion
01:37 Logic of the function
01:50 Remove non-numbers
02:30 Length of only-numbers
03:15 Recursive loop through my text
06:16 My function has two arguments!
07:38 Adding the last step
08:52 Summary


Watch video 75 - Recursion in Power Query online without registration, duration hours minute second in high quality. This video was added by user WesleySon 19 June 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 775 once and liked it 9 people.