Record.FieldOrDefault

Record

Returns the value of a named field from a record, or a default value if the field does not exist.

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

Syntax

Record.FieldOrDefault(record as record, field as text, optional defaultValue as nullable any) as nullable any

Parameters

NameTypeRequiredDescription
recordrecordYesThe record to retrieve the field from.
fieldtextYesThe name of the field to retrieve.
defaultValueanyNoThe value to return when the field does not exist. Defaults to null.

Return Value

anyThe value of the specified field, or defaultValue (null if omitted) when the field is absent.

Remarks

Record.FieldOrDefault is the safe alternative to direct field access (record[FieldName]) and Record.Field. Direct field access raises an error if the field does not exist; Record.FieldOrDefault returns null by default or a custom fallback value you specify.

This is essential when working with semi-structured or inconsistent data — for example, JSON expanded into records where some rows have fields that others do not, or API responses where optional fields are absent rather than null.

The comparison to related approaches:

| Approach | Missing field behavior | |---|---| | record[FieldName] | Raises error | | Record.Field(record, name) | Raises error | | Record.FieldOrDefault(record, name) | Returns null | | Record.FieldOrDefault(record, name, default) | Returns default value |

The defaultValue parameter accepts any type and defaults to null when omitted. You can use any expression as the default, including function calls.

Examples

Example 1: Retrieve a field that exists — returns its value

Record.FieldOrDefault([Name = "Alice", Age = 30], "Name")
Result
Result
1Alice

Example 2: Field is absent — returns null by default

Record.FieldOrDefault([Name = "Alice", Age = 30], "Email")
Result
Result
1null

Example 3: Specify a custom fallback value for missing fields

Record.FieldOrDefault([Name = "Alice"], "Email", "no-email@example.com")
Result
Result
1no-email@example.com

Example 4: Safely extract an optional "Region" field from each Sales row

Table.AddColumn(
    Table.SelectColumns(Table.FirstN(Sales, 4), {"OrderID", "CustomerName"}),
    "Region",
    each Record.FieldOrDefault(_, "Region", "Unknown"),
    type text
)
Result
OrderID
CustomerName
Region
11AliceEast
22BobWest
33CharlieEast
44AliceNorth

Compatibility

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