Table.Max

D

T

The M Code Behind the Power Query M function Table.Max

In this article, we will explore the M code behind the Table.Max function and how it can be used to transform data in Power Query.

Overview of the Table.Max Function

The Table.Max function is used to find the maximum value in a column of a table. It takes two arguments: the table and the name of the column. Here is the syntax for the Table.Max function:


Table.Max(table as table, columnName as text, optional comparer as nullable function) as any


The first argument, `table`, is the table that contains the column to be evaluated. The second argument, `columnName`, is the name of the column to be evaluated. The third argument, `comparer`, is an optional argument used to specify a custom comparison function.

The M Code Behind Table.Max Function

The Table.Max function is implemented in M code, which is the programming language used in Power Query. The M code for the Table.Max function is as follows:


(table as table, columnName as text, optional comparer as nullable function) =>

let

column = Table.Column(table, columnName),

max = List.Max(column, comparer)

in

max


The M code starts by defining the function and its arguments. The code then uses the `Table.Column` function to extract the column from the table. The `List.Max` function is then used to find the maximum value in the column. Finally, the maximum value is returned as the output of the function.

Example Usage of the Table.Max Function

To illustrate the usage of the Table.Max function, we will use an example table that contains sales data for a business. Here is the sample data:

| Date | Sales |

|————|——-|

| 2021-01-01 | 100 |

| 2021-02-01 | 200 |

| 2021-03-01 | 150 |

| 2021-04-01 | 300 |

Suppose we want to find the maximum sales amount in the table. We can use the Table.Max function to achieve this. Here is the M code for the query:


let

source = #table(

{"Date", "Sales"},

{

{#date(2021, 1, 1), 100},

{#date(2021, 2, 1), 200},

{#date(2021, 3, 1), 150},

{#date(2021, 4, 1), 300}

}

),

maxSales = Table.Max(source, "Sales")

in

maxSales


The output of the query will be `300`, which is the maximum sales amount in the table.

The Table.Max function is a powerful tool for finding the maximum value in a column of a table. Its M code implementation is simple and easy to understand. By using the Table.Max function, users can easily transform data in Power Query.

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)