Data Structures

M Lists

Working with Lists

M lists use { } for ordered collections in queries.

Introduction to M Lists

M Lists are a fundamental data structure in Power Query's M language. They are used to store ordered collections of items, allowing you to perform a variety of operations on multiple elements simultaneously. M Lists are analogous to arrays or lists in other programming languages.

In M, lists are defined using curly braces { }. These structures enable you to handle data dynamically and efficiently in queries.

Creating a Basic List

To create a list in M, you simply enclose the elements within curly braces. Below is an example of a simple list containing numbers:

Accessing List Elements

Elements in a list can be accessed using zero-based indexing. This means the first element is at index 0, the second element is at index 1, and so on. Here is how you can access elements in a list:

List Operations

M provides several built-in functions to perform operations on lists, such as finding the length of a list, checking if a list contains a certain value, or performing transformations on each element.

Combining Lists

You can combine two or more lists using the List.Combine function. This is useful when you need to merge data from multiple sources.

Removing Duplicates from Lists

To remove duplicate values from a list, use the List.Distinct function. This function is handy for cleaning data and ensuring each element is unique.

Conclusion

M Lists are versatile and powerful tools for managing collections of data in Power Query. By understanding how to create, access, and manipulate lists, you can significantly enhance your data transformation capabilities.

In the next article, we will explore Records, another crucial data structure in M language.

Data Structures