List.Transform

D

T

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

Understanding the List.Transform Function

The List.Transform function takes two arguments: a list and a function. The function applies a transformation to each element of the list and returns a new list with the transformed elements. The syntax for the List.Transform function is as follows:


List.Transform(list as list, transform as function) as list


Here, `list` is the list to be transformed, and `transform` is the function that will be applied to each element of the list. The `as list` at the end of the function definition is used to specify the output type of the function, which is also a list.

Examples of Using the List.Transform Function

Let's take a look at some examples of using the List.Transform function in Power Query.

Example 1: Converting All Text to Uppercase

Suppose we have a list of names that we want to convert to uppercase. We can use the List.Transform function to apply the `Text.Upper` function to each element of the list as follows:


let

names = {“john”, “jane”, “jim”},

uppercaseNames = List.Transform(names, each Text.Upper(_))

in

uppercaseNames


In this example, we define a list called `names` that contains three elements: john, jane, and jim. We then use the List.Transform function to apply the `Text.Upper` function to each element of the `names` list, which converts all text to uppercase. The resulting list is stored in the `uppercaseNames` variable.

Example 2: Extracting the Date from a List of Date-Times

Suppose we have a list of date-time values, and we want to extract only the date portion of each value. We can use the List.Transform function to apply the `DateTime.Date` function to each element of the list as follows:


let

dates = {#datetime(2021, 1, 1, 0, 0, 0), #datetime(2021, 1, 2, 0, 0, 0), #datetime(2021, 1, 3, 0, 0, 0)},

dateList = List.Transform(dates, each DateTime.Date(_))

in

dateList


In this example, we define a list called `dates` that contains three date-time values. We then use the List.Transform function to apply the `DateTime.Date` function to each element of the `dates` list, which extracts only the date portion of each value. The resulting list is stored in the `dateList` variable.

The List.Transform function is a powerful tool in Power Query that allows users to apply a transformation to each element of a list. By understanding the M code behind this function, users can better leverage its power and flexibility to transform data in a variety of ways. In this article, we explored the syntax of the List.Transform function and provided examples of how it can be used to perform common data transformations.

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)