List.Count

D

T

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

What is the M language?

Before we dive into the M code behind List.Count, let’s first discuss what the M language is. M is the formula language used by Power Query, which allows users to create custom functions and formulas to manipulate data. The M language is a functional language, which means that it focuses on the evaluation of expressions rather than the execution of commands.

List.Count function overview

The List.Count function is used to return the number of items in a list. It takes one argument, which is the list to count. Here is an example of using List.Count to count the number of items in a list:


let

myList = {1, 2, 3},

count = List.Count(myList)

in

count


In this example, we define a list called `myList` with three items and then call the List.Count function on it to get the count of items. The result of this expression is `3`.

The M code behind List.Count

Now that we've seen how List.Count works, let's take a closer look at the M code behind it. Here is the M code for the List.Count function:


(List as list) as number =>

List.Count(List)


This code defines a function that takes a list parameter called `List` and returns a number. The `List.Count` function is then called on the `List` parameter, which returns the number of items in the list.

Understanding the M code

Breaking down the M code for List.Count, we can see that the function takes a single parameter called `List` of type `list`. The `as list` in the parameter definition specifies the data type of the parameter as a list.

The `as number` after the parameter definition specifies the return type of the function as a number. This tells Power Query that the function should return a numeric value.

Finally, the body of the function simply calls the built-in List.Count function on the `List` parameter and returns the result.

In this article, we explored the M code behind the Power Query List.Count function. We learned that List.Count is a built-in function in Power Query that is used to return the number of items in a list. We also saw how the M code for List.Count is structured and how it is used to evaluate expressions rather than execute commands. Understanding the M code behind built-in functions like List.Count can help users create custom functions and formulas to manipulate data in Power Query.

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)