List.First

D

T

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

In this article, we will explore the M code behind the List.First function and how it can be used to extract the first element of a list in Power Query.

Understanding Lists in Power Query

Before we dive into the List.First function, let’s first understand what lists are in Power Query. A list is an ordered collection of values, where each value can be of a different type. A list is enclosed in curly braces {} and each value is separated by a comma. Here’s an example of a list in Power Query:


{1, “Apple”, true, #date(2022,1,1)}


As you can see, this list contains four values, each of a different type - an integer, a string, a boolean, and a date.

The List.First Function

Now that we understand what lists are in Power Query, let's take a closer look at the List.First function. The List.First function is used to extract the first element of a list. It takes a list as an input and returns the first element of that list.

Here's the syntax of the List.First function:


List.First(list as list) as any


As you can see, the List.First function takes a list as an input and returns the first element of that list. The function returns an "any" type, which means that it can return any type of value.

How the List.First Function Works

Now that we understand the syntax of the List.First function, let's take a deeper look at how it works. The List.First function works by taking the input list and returning the first element of that list.

Here's an example of how the List.First function works:


let

myList = {1, 2, 3},

firstElement = List.First(myList)

in

firstElement


In this example, we have defined a list called "myList" that contains three values - 1, 2, and 3. We then use the List.First function to extract the first element of the list and store it in a variable called "firstElement". Finally, we return the value of "firstElement".

Using the List.First Function

Now that we know how the List.First function works, let's take a look at how it can be used in Power Query. The List.First function is particularly useful when you want to extract the first element of a list and use it in a subsequent transformation.

Here's an example of how the List.First function can be used:


let

myList = {“Apple”, “Banana”, “Cherry”},

firstElement = List.First(myList),

transformedList = List.Transform(myList, each Text.Upper(_)),

firstElementUpperCase = Text.Upper(firstElement)

in

{transformedList, firstElementUpperCase}


In this example, we have defined a list called "myList" that contains three string values - "Apple", "Banana", and "Cherry". We then use the List.First function to extract the first element of the list and store it in a variable called "firstElement". We then transform the original list using the List.Transform function, which converts each string to uppercase. Finally, we convert the first element to uppercase using the Text.Upper function and store it in a variable called "firstElementUpperCase". We then return a new list that contains both the transformed list and the first element in uppercase.

The List.First function is a useful function in Power Query that allows you to extract the first element of a list. It is particularly useful when you want to use the first element in a subsequent transformation. Understanding how the function works and how to use it can help you manipulate data in a more efficient and effective way.

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)