August 26, 2024

Using Lists and Functions with List.Transform()

Another thought experiment on Power Query. I was thinking about how to do a number of checks against text in one shot. Records have been a great way to return several values at once, so I knew that would be something I could use. Also, I’ve used lists with custom Switch() functions, so I thought possibly I could use this. But could functions be stored in lists?

Answer is yes. Lists can contain functions. If you recognize this, then you can write several functions to check for things as items in a list and then iterate through the list.

The Code

The sample function above takes in a number of parameters, which are used to perform the checks. Using List.Transform(), I iterate through functions that each return a record. The first item of the record performs the check, while the second item describes what the check performed.

Afterwards, if I wanted to see all of the true checks, I can do so easily with List.Select() and filtering on the check field being true:

Takeways

Is this a great use case? Not entirely sure, but it was useful to conceptualize the use of functions within lists. Once you step outside the GUI and learn about how flexible M is, you start to ask questions like “I wonder if I could use X with Y?” This has helped me further develop my use of M with Power BI and Dataverse.

You may also like