Type.ListItem

D

T

The M Code Behind the Power Query M function Type.ListItem

Understanding the Type.ListItem Function

The Type.ListItem function is used to extract a single item from a list. It takes two arguments: the list and the index of the item you want to extract. For example, if you have a list of numbers and you want to extract the third item in the list, you would use the following code:


List.Select({1, 2, 3, 4, 5}, 2)


This would return the value 3, which is the third item in the list. The Type.ListItem function is a shorthand way of writing this code. The equivalent code using the Type.ListItem function would be:


{1, 2, 3, 4, 5}{2}


This code does the same thing as the previous code, but it is shorter and easier to read. The Type.ListItem function is especially useful when you are working with large lists or when you need to extract multiple items from a list.

The M Code Behind the Type.ListItem Function

The M code behind the Type.ListItem function is relatively simple. The function is defined as follows:


Type.ListItem(list as list, index as number) as any =>

List.PositionOf(list, index, 0)


The function takes two arguments: the list and the index of the item you want to extract. It then uses the List.PositionOf function to extract the item from the list. The List.PositionOf function returns the position of the item in the list, and the Type.ListItem function returns the value of the item at that position.

Using the Type.ListItem Function in Your Data Analysis

The Type.ListItem function can be used in a variety of ways to enhance your data analysis. Here are a few examples:

Extracting Data from Nested Lists

Sometimes, your data may be structured as a nested list. For example, you may have a list of customers, and each customer has a list of orders. If you want to extract a specific order from a specific customer, you can use the Type.ListItem function to extract the order from the nested list. Here is an example code:


let

customers = {{"John", {123, 456}}, {"Jane", {789, 345}}},

order = Type.ListItem(Type.ListItem(customers, 1), 1)

in

order


This code extracts the second customer (Jane) from the list of customers and then extracts the second order (345) from that customer’s list of orders.

Filtering Lists

You can also use the Type.ListItem function to filter lists based on specific criteria. For example, if you have a list of sales data and you want to filter the list to only include sales that occurred on a specific date, you can use the Type.ListItem function to extract the date from each sales record and then filter the list based on that date. Here is an example code:


let

sales = {{"Product A", "2022-01-01", 100}, {"Product B", "2022-01-02", 200}, {"Product C", "2022-01-03", 300}},

filteredSales = List.Select(sales, each Type.ListItem(_, 1) = "2022-01-02")

in

filteredSales


This code filters the list of sales data to only include sales that occurred on January 2, 2022.

Creating Dynamic Queries

Finally, you can use the Type.ListItem function to create dynamic queries that can be used to extract data from multiple sources. For example, if you have a list of Excel files and you want to extract data from a specific sheet in each file, you can use the Type.ListItem function to extract the sheet name from each file path and then use that sheet name in your query. Here is an example code:


let

files = {"C:DataFile1.xlsx", "C:DataFile2.xlsx", "C:DataFile3.xlsx"},

queries = List.Transform(files, each Excel.Workbook(_){[Item = Type.ListItem(Sheets, 0)]}[Data])

in

queries


This code creates a list of queries that extract data from the first sheet in each Excel file in the list of files.

The Type.ListItem function is a powerful tool that can be used to extract a single item from a list. It is a shorthand way of writing more complex code, and it can be used in a variety of ways to enhance your data analysis. By understanding the M code behind the Type.ListItem function, you can take your Power Query M skills to the next level and become a more effective data analyst.

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)