Type.ForRecord

Type

Constructs a record type from a record of field type descriptors.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Type.ForRecord(fields as record, optional isOpen as nullable logical) as type

Parameters

NameTypeRequiredDescription
fieldsrecordYesA record where each field name maps to a descriptor record with fields Type (a type value) and Optional (a logical).
isOpenlogicalNoWhen true, the resulting type is an open record type. Defaults to false (closed).

Return Value

typeA record type with the specified field types.

Remarks

Type.ForRecord programmatically constructs a record type from a descriptor record. The fields parameter is a record where each field name matches the desired field name in the resulting type, and each field value is itself a record with two required sub-fields:

- Type — the M type value for that field (e.g., type text, type nullable number) - Optional — a logical indicating whether the field can be absent: true for optional, false for required

The optional isOpen parameter controls whether the constructed type is an open record type (allowing extra fields beyond those declared, when true) or a closed record type (requiring exactly the declared fields, when false or omitted). The default is closed (false).

Type.ForRecord is the programmatic counterpart to writing a type literal such as type [Name = text, Age = number]. Use it when the set of fields is determined at runtime or when building schemas dynamically — for example, in custom connector GetSchemaTransformByName implementations or generic transformation helpers. The inverse function is Type.RecordFields, which extracts the field descriptors from an existing record type.

To apply a constructed record type to an actual record value, use Value.ReplaceType(record, newType).

Examples

Example 1: Construct a closed record type with required fields

Type.ForRecord(
    [
        Name = [Type = type text, Optional = false],
        Age  = [Type = type number, Optional = false]
    ],
    false
)
Result
Result
1type [Name = text, Age = number]

Example 2: Construct an open record type with an optional field

Type.ForRecord(
    [
        CustomerID = [Type = type number, Optional = false],
        Email = [Type = type text, Optional = true]
    ],
    true
)
Result
Result
1type [CustomerID = number, Email = (optional text), ...]

Example 3: Verify the declared fields of the constructed type

let
    RT = Type.ForRecord(
        [
            Name = [Type = type text, Optional = false],
            Age  = [Type = type number, Optional = true]
        ],
        false
    ),
    Fields = Type.RecordFields(RT)
in
    Fields[Age][Optional]
Applied Steps

The final output — retrieves the field descriptors from the record type using Type.RecordFields, then reads the Optional flag of the Age field to confirm it is true.

Result
1TRUE

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks