Type.ClosedRecord

D

T

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

Understanding the Type.ClosedRecord Function

The Type.ClosedRecord function is used to define a record type with a fixed set of fields. It is useful when you want to ensure that your data follows a predefined structure. For example, if you are working with financial data, you may want to ensure that each record contains fields for Date, Amount, and Description.

Here is an example of how to define a closed record type using the Type.ClosedRecord function:


Type.ClosedRecord [

Field1 = type,

Field2 = type,

...

]


In this example, Field1 and Field2 are the names of the fields in the record type, and type is the data type of each field. You can define as many fields as you need, separated by commas.

The M Code Behind the Type.ClosedRecord Function

The M code behind the Type.ClosedRecord function is relatively straightforward. When you define a closed record type, Power Query generates a record type value that contains the names and data types of each field.

Here is an example of the M code behind the closed record type we defined earlier:


#shared

let

typeClosedRecord = Type.ClosedRecord [

Field1 = type,

Field2 = type

]

in

typeClosedRecord


In this example, we define a shared variable called typeClosedRecord that uses the Type.ClosedRecord function to define a closed record type with two fields. Each field is defined with the name and data type we specified earlier.

Using the Type.ClosedRecord Function

Once you have defined a closed record type, you can use it in your queries to ensure that your data follows the predefined structure. Here is an example of how to use the closed record type we defined earlier:


let

data = [

[Field1 = "Value1", Field2 = 123],

[Field1 = "Value2", Field2 = 456]

],

source = Table.FromRecords(data, typeClosedRecord)

in

source


In this example, we define a variable called data that contains two records with the Field1 and Field2 fields we defined earlier. We then use the Table.FromRecords function to convert the data into a table. We specify the closed record type as the second argument to the function to ensure that the data follows the predefined structure.

The Type.ClosedRecord M function is a useful tool for ensuring that your data follows a predefined structure. It allows you to define a record type with a fixed set of fields and ensures that your data conforms to that structure. With the M code behind the function, you can easily define and use closed record types in your Power Query queries.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

20-22 Nov 23 (London or Online)

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)