Table.Last

D

T

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

In this article, we will explore the M code behind the Table.Last function in Power Query M, including its syntax, usage, and examples.

Understanding the Table.Last Function

The Table.Last function in Power Query M is a function used to extract the last row of a table. It takes a table as its input and outputs a record with the values from the last row of the table.

The syntax for the Table.Last function is as follows:


Table.Last(table as table) as record


In this syntax, the table parameter is the input table, and the function returns a record with the values from the last row of the table.

Examples of the Table.Last Function

To better understand the Table.Last function, let’s explore some examples of how it can be used in Power Query M.

Example 1: Extracting the Last Row of a Table

Suppose we have a table with the following data:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKyklPw0GJiBmI2PQzKz0zPSdJSSnILUxRiEjMgA==", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t])),

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}})

in

#"Changed Type"


To extract the last row of this table, we can use the Table.Last function as follows:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKyklPw0GJiBmI2PQzKz0zPSdJSSnILUxRiEjMgA==", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t])),

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),

LastRow = Table.Last(#"Changed Type")

in

LastRow


This code will output a record with the values from the last row of the table:


[Column1=3, Column2=2]


Example 2: Using the Table.Last Function in a Custom Function

The Table.Last function can also be used within custom functions in Power Query M. Suppose we have a custom function that takes a table as its input and outputs the last row of the table. We can define this function using the Table.Last function as follows:


let

GetLastRow = (table as table) =>

let

LastRow = Table.Last(table)

in

LastRow,

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKyklPw0GJiBmI2PQzKz0zPSdJSSnILUxRiEjMgA==", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t])),

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),

LastRow = GetLastRow(#"Changed Type")

in

LastRow


This code defines a custom function called GetLastRow that takes a table as its input and outputs the last row of the table using the Table.Last function. The function is then called on the input table, and the output is a record with the values from the last row of the table:


[Column1=3, Column2=2]


In conclusion, the Table.Last function in Power Query M is a powerful tool for extracting the last row of a table. Understanding the syntax, usage, and examples of this function can help you to better manipulate and analyze data in Power Query M. Whether you’re a beginner or an advanced user, the Table.Last function is a valuable addition to your data transformation and query toolkit.

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)