Type.TableRow

D

T

The M Code Behind the Power Query M function Type.TableRow

Introduction to Type.TableRow

Type.TableRow is a built-in M function in Power Query that is used to create a table row type. A table row type is a data type that describes the structure of a single row in a table. It is commonly used in Power Query to define the expected data types of columns in a table.

The Type.TableRow function takes a record type as its argument and returns a table row type. A record type is a data type that describes a collection of fields, each with a name and a data type. The table row type returned by Type.TableRow has the same fields as the input record type, but with the addition of an index field.

Here is an example of how to use the Type.TableRow function:


let

Source = #table(

type table [Name = text, Age = number],

{

{"John", 25},

{"Jane", 30}

}

),

RowType = type table [Name = text, Age = number, Index = number],

Transform = Table.TransformColumnTypes(Source, RowType)

in

Transform


In this example, we start with a table called Source that has two columns: Name and Age. We then define a row type called RowType that includes the Name and Age fields, as well as an Index field. Finally, we use the Table.TransformColumnTypes function to convert the Source table to the RowType table.

The M Code behind Type.TableRow

The M code behind the Type.TableRow function is relatively simple. Here is the code:


let Type.TableRow = (recordType as type) =>

let

fields = Record.FieldNames(recordType),

indexedFields = List.InsertRange(fields, List.Count(fields), {"Index"}),

types = Record.FieldTypes(recordType),

indexedTypes = List.InsertRange(types, List.Count(types), {type number}),

indexedRecordType = Type.Record(

List.Zip(indexedFields),

List.Zip(indexedTypes)

),

tableType = Type.Table(indexedRecordType)

in

tableType

in Type.TableRow


Let’s break down this code into its individual parts:

Input Parameter

The Type.TableRow function takes a single input parameter called recordType. This parameter is the record type that describes the structure of a single row in a table.

Creating the Indexed Fields List

The first step in the Type.TableRow function is to create an indexed fields list. This list includes all of the fields from the input record type, as well as an additional Index field. The Index field is created using the List.InsertRange function, which takes the fields list and inserts the string “Index” at the end.


fields = Record.FieldNames(recordType),

indexedFields = List.InsertRange(fields, List.Count(fields), {"Index"}),


Creating the Indexed Types List

The next step is to create an indexed types list. This list includes all of the data types from the input record type, as well as an additional data type of number for the Index field. The indexed types list is created using the List.InsertRange function, which takes the types list and inserts the number data type at the end.


types = Record.FieldTypes(recordType),

indexedTypes = List.InsertRange(types, List.Count(types), {type number}),


Creating the Indexed Record Type

The indexed record type is created using the Type.Record function. This function takes two input parameters: a list of field names and a list of data types. The List.Zip function is used to combine the indexed fields and indexed types lists into a single list of tuples.


indexedRecordType = Type.Record(

List.Zip(indexedFields),

List.Zip(indexedTypes)

),


Creating the Table Type

The final step in the Type.TableRow function is to create the table type using the Type.Table function. This function takes a single input parameter, which is the indexed record type created in the previous step.


tableType = Type.Table(indexedRecordType)


The Type.TableRow function is an important tool in Power Query for creating custom data types that describe the structure of a single row in a table. Understanding the M code behind the function can help you to better understand how Power Query works and how you can use it to transform and analyze your data.

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)