Expressions

M Each Expressions

Using Each Expressions

M each expressions iterate rows for transformations.

Introduction to Each Expressions in M

In the M language, each expressions are a powerful tool for data transformation. They allow you to iterate over rows of a table and apply transformations or calculations to each row individually. This makes them incredibly useful for tasks that require row-by-row processing.

Each expressions are often used in conjunction with other M functions like Table.TransformColumns or Table.AddColumn to perform operations on each row of a column.

Basic Syntax of Each Expressions

The basic syntax of an each expression in M is straightforward. It uses the keyword each followed by an expression that defines the transformation. The expression can reference the current row using the _ underscore symbol, which represents the current row or item being iterated over.

Here’s a simple example:

Using Each Expressions with Table Functions

Each expressions are commonly used with table functions to apply transformations to table columns. For instance, you can use Table.TransformColumns to increase the age of each person in a table by 1, as shown in the previous example. This is particularly useful for data cleaning, formatting, or calculations that need to be applied uniformly across table rows.

Another common use case is when you need to add a new column with calculated values using Table.AddColumn. Here's how you can use each expressions to add a new column that concatenates the name and age of each person:

Advanced Usage with Conditional Logic

You can also integrate conditional logic within each expressions to perform more complex transformations. This allows you to apply different operations based on the value of each row. For example, you might want to categorize ages into different groups.

Here's how you can add a column that categorizes each person as "Young" or "Adult" based on their age:

Conclusion

Each expressions in M are a versatile and powerful feature for data transformation, allowing you to iterate over rows and perform complex operations with ease. Whether you are modifying existing columns, adding new calculated columns, or integrating conditional logic, each expressions provide a robust solution for row-wise data manipulation in M.

Understanding how to leverage each expressions effectively will greatly enhance your data transformation capabilities in M.

Expressions