Reader small image

You're reading from  The Definitive Guide to Power Query (M)

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781835089729
Edition1st Edition
Languages
Right arrow
Authors (3):
Gregory Deckler
Gregory Deckler
author image
Gregory Deckler

Greg Deckler is a 7-time Microsoft MVP for Data Platform and an active blogger and Power BI community member, having written over 6,000 solutions to community questions. Greg has authored many books on Power BI, including Learn Power BI 1st and 2nd Editions, DAX Cookbook, Power BI Cookbook 2nd Edition and Mastering Power BI 2nd Edition. Greg has also created several external tools for Power BI and regularly posts video content to his YouTube channels, Microsoft Hates Greg and DAX For Humans.
Read more about Gregory Deckler

Rick de Groot
Rick de Groot
author image
Rick de Groot

Rick de Groot was born in the Netherlands and has been working in BI for more than 14 years. He went freelance in 2016 and now works as an independent Power BI consultant. On his mission to make Power BI more accessible, he started two blogs: BI Gorilla and PowerQuery. how, and a YouTube channel sharing Power Query and Power BI content. His commitment to offering free content through multiple platforms has led him to earning the Microsoft Data Platform MVP award for two consecutive years.
Read more about Rick de Groot

Melissa de Korte
Melissa de Korte
author image
Melissa de Korte

Melissa de Korte's approach to facing challenges is fueled by relentless curiosity. She is a dedicated community member and content creator. Her portfolio includes blogs, tutorials, courses, and webinars, that make Power Query M more accessible and useful for all. Behind her professional persona lies a genuine dedication to empowering others through education and knowledge sharing, and a desire to encourage professionals to embrace the potential of Power Query, M.
Read more about Melissa de Korte

View More author details
Right arrow

Iteration and Recursion

Iteration and recursion are fundamental concepts in programming that allow for the repetitive execution of code. In the context of Power Query M, these techniques greatly enhance your data transformation and manipulation abilities. Whether you’re looking to apply a function across a list of values, accumulate results, or even refer to previous steps in a sequence, understanding iteration and recursion makes a big difference in these situations.

This chapter delves into key functions and operators that enable iteration and recursion in the Power Query M language. You’ll learn how to loop through lists with List.Transform, perform a function on values with List.Accumulate, generate lists conditionally with List.Generate, and implement true recursion using the @ scoping operator.

The aim is to provide you with the skills needed to incorporate these techniques into your own data workflows. The main topics covered are:

  • Iteration...

Introduction to iteration

Iteration is a core concept in programming, important for performing repetitive actions on data. In Power Query, iteration often happens over rows in a table or items in a list.

Suppose you want to square a list of 1,000 numbers. Doing this without iteration would require writing 1,000 separate lines of code, which is both time-consuming and error-prone. With iteration, the same task can be completed with just a few lines of code.

Traditional languages like Python or Java use for and while loops for such tasks. However, the Power Query M language takes a different approach to iteration. It performs transformations on its underlying data structures by design.

For example, when you use Table.AddColumn to create a new column, Power Query automatically applies the specified operation to each row in the table, similar to how a for loop would iterate over each element in a list. The M language automatically does the iteration for you using its standard...

Recursion

Recursion is a programming term that sounds more complex than it really is. Simply put, recursion happens when a function calls itself within its own definition. Think of it as a loop, but instead of using a typical for or while loop, the function uses itself to perform an operation multiple times.

Recursion is helpful for addressing problems where you need to repeat a task but the number of repetitions isn’t known beforehand. This operation requires an ending condition so that the iterations stop when they reach a specific condition, preventing infinite repetitions.

The concept of recursion has also been compared to the concept of Russian dolls. Imagine opening a Russian doll to find another one inside, and another inside that, and so on. This process continues until you reach the smallest doll that can’t be opened further. Each step is like a function calling itself, eventually reaching a point where it can no longer do so.

Why is recursion important...

Summary

In this chapter, we explored two important concepts: iteration and recursion. Beginning with iteration, we examined the role of the List.Transform and List.Accumulate functions. Both perform their tasks on a list with a fixed number of items, like a for loop. While List.Transform serves its purpose for straightforward operations, List.Accumulate is also helpful for scenarios requiring access to the result of earlier iterations.

We then delved into the List.Generate function, which allows you to dynamically create lists based on custom conditions and logic, similar to a while loop. The function has good performance, provides an easy way to debug its results, and is the preferred choice over traditional recursion in most use cases.

Lastly, we tackled the concept of true recursion, facilitated in M through the @ scoping operator. Although recursion comes with a cost in terms of memory and speed, you can consider using it for tasks involving a limited number of iterations...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Definitive Guide to Power Query (M)
Published in: Mar 2024Publisher: PacktISBN-13: 9781835089729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Gregory Deckler

Greg Deckler is a 7-time Microsoft MVP for Data Platform and an active blogger and Power BI community member, having written over 6,000 solutions to community questions. Greg has authored many books on Power BI, including Learn Power BI 1st and 2nd Editions, DAX Cookbook, Power BI Cookbook 2nd Edition and Mastering Power BI 2nd Edition. Greg has also created several external tools for Power BI and regularly posts video content to his YouTube channels, Microsoft Hates Greg and DAX For Humans.
Read more about Gregory Deckler

author image
Rick de Groot

Rick de Groot was born in the Netherlands and has been working in BI for more than 14 years. He went freelance in 2016 and now works as an independent Power BI consultant. On his mission to make Power BI more accessible, he started two blogs: BI Gorilla and PowerQuery. how, and a YouTube channel sharing Power Query and Power BI content. His commitment to offering free content through multiple platforms has led him to earning the Microsoft Data Platform MVP award for two consecutive years.
Read more about Rick de Groot

author image
Melissa de Korte

Melissa de Korte's approach to facing challenges is fueled by relentless curiosity. She is a dedicated community member and content creator. Her portfolio includes blogs, tutorials, courses, and webinars, that make Power Query M more accessible and useful for all. Behind her professional persona lies a genuine dedication to empowering others through education and knowledge sharing, and a desire to encourage professionals to embrace the potential of Power Query, M.
Read more about Melissa de Korte