Record.FieldOrDefault Function in Power Query

The Record.FieldOrDefault function returns the value of the specified field or column from a record, or the default value if the field does not exist.

Syntax

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

Example: Return the value from the column “Education”.

Power Query M

let
  MyTable = Table.FromRecords(
    {
      [CustomerID = 1, Name = "Ashish", Phone = "123-4567"], 
      [CustomerID = 2, Name = "Katrina", Phone = "987-6543"], 
      [CustomerID = 3, Name = "Alia", Phone = "543-7890"], 
      [CustomerID = 4, Name = "Vicky", Phone = "676-8479"], 
      [CustomerID = 5, Name = "Mohini", Phone = "574-8864"], 
      [CustomerID = 6, Name = "Meenakshi", Phone = "574-8864"], 
      [CustomerID = 7, Name = "Esha", Phone = "574-8864"], 
      [CustomerID = 8, Name = "Anjali", Phone = "574-8864"]
    }
  ),
    ReturnFieldValue = Record.FieldOrDefault(MyTable{2}, "Education")
in
    ReturnFieldValue

The output will be shown in the following image:

Record.FieldOrDefault function in Power Query

Example: Return the value from the column “Education”. This time we have added the optional default value.

Power Query M

let
  MyTable = Table.FromRecords(
    {
      [CustomerID = 1, Name = "Ashish", Phone = "123-4567"], 
      [CustomerID = 2, Name = "Katrina", Phone = "987-6543"], 
      [CustomerID = 3, Name = "Alia", Phone = "543-7890"], 
      [CustomerID = 4, Name = "Vicky", Phone = "676-8479"], 
      [CustomerID = 5, Name = "Mohini", Phone = "574-8864"], 
      [CustomerID = 6, Name = "Meenakshi", Phone = "574-8864"], 
      [CustomerID = 7, Name = "Esha", Phone = "574-8864"], 
      [CustomerID = 8, Name = "Anjali", Phone = "574-8864"]
    }
  ),
    ReturnFieldValue = Record.FieldOrDefault(MyTable{2}, "Education", "The column is not found")
in
    ReturnFieldValue

The output will be shown in the following image:

Record.FieldOrDefault function in Power Query