List.AnyTrue

D

T

The M Code Behind the Power Query M function List.AnyTrue

Understanding the List.AnyTrue Function

Before we dive into the M code behind the List.AnyTrue function, let’s first understand what this function does. The List.AnyTrue function takes a list of logical values and returns true if any of the values in the list are true. If all the values are false, it returns false.

Here is an example of using the List.AnyTrue function in Power Query:


let

myList = {true, false, true},

result = List.AnyTrue(myList)

in

result


In this example, we have a list of three logical values: true, false, and true. We then pass this list to the List.AnyTrue function, which returns true because at least one of the values in the list is true.

The M Code Behind List.AnyTrue

Now that we understand what the List.AnyTrue function does, let's take a look at the M code behind this function. The code for the List.AnyTrue function is as follows:


(List as list) as logical =>

List.Contains(List.Transform(List, each if _ then 1 else 0), 1)


Let's break this code down and understand what each part does:

`(List as list) as logical` - This is the function signature, which specifies that the List.AnyTrue function takes a list as input and returns a logical value.

`List.Contains` - This function is used to check if a list contains a specific value.

`List.Transform` - This function is used to transform each value in a list using a given function.

`each if _ then 1 else 0` - This is the function that is passed to the List.Transform function. It checks if each value in the list is true or false, and returns 1 if it's true and 0 if it's false.

`1` - This is the value that the List.Contains function is looking for. If the List.Transform function returns a list that contains 1, it means that at least one value in the original list was true.

So, the List.AnyTrue function works by first transforming the input list into a new list of 1s and 0s, where 1 represents true and 0 represents false. It then checks if this new list contains the value 1. If it does, it means that at least one value in the original list was true, and the function returns true. Otherwise, it returns false.

In this article, we looked at the M code behind the List.AnyTrue function in Power Query. We saw that this function works by transforming a list of logical values into a list of 1s and 0s, and then checking if this new list contains the value 1. If you are building complex data transformation workflows in Power Query, understanding the M code behind the functions you are using can help you optimize your queries and improve performance.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)