Table.First

D

T

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

Understanding the Table.First Function

The Table.First function is used to return the first row of a table. It takes a table as input and returns a record representing the first row of the table. The syntax for the Table.First function is as follows:


Table.First(table as table) as record


The Table.First function takes one argument, which is a table. The function returns a record representing the first row of the table.

The M Code Behind Table.First

The M code behind the Table.First function is straightforward. When you call the Table.First function, Power Query generates the following M code:


(table as table) =>

let

Source = Table.FirstN(table,1),

#"Converted to Table" = Record.ToTable(Source{0})

in

#"Converted to Table"


The M code above defines an anonymous function that takes a table as input. The function then uses the Table.FirstN function to return the first row of the table as a table value. Next, the function uses the Record.ToTable function to convert the record into a table.

Breaking Down the M Code

Now that we’ve seen the M code behind Table.First, let’s break down each step in the function.

Defining the Function


(table as table) =>


The first line of the M code defines an anonymous function that takes a table as input. The argument is named “table”.

Using the Table.FirstN Function


Source = Table.FirstN(table,1),


The next line of the M code uses the Table.FirstN function to return the first row of the table as a table value. The Table.FirstN function takes two arguments – the first is the table to return the rows from, and the second is the number of rows to return. In this case, we’re only returning the first row.

Converting the Record to a Table


#"Converted to Table" = Record.ToTable(Source{0})


Finally, the M code uses the Record.ToTable function to convert the record into a table. The Source variable contains the first row of the table as a record. We access the first record by using the {0} index. We then convert the record to a table using the Record.ToTable function.

The Table.First function is a useful function in Power Query that is frequently used by data analysts and business intelligence professionals. The M code behind the Table.First function is relatively straightforward, and understanding it can help you better understand how Power Query works under the hood. We hope this article has been helpful in breaking down the M code behind the Table.First function.

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)