Record.HasFields

Updated on

Record.HasFields is a Power Query M function that checks if a record has the specified fields and returns a logical value (true or false). The function returns true if all the specified fields are present in the input record, otherwise false.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Record.HasFields(
   record as record,
   fields as any,
) as logical

Description

Record.HasFields examines a record to confirm the presence of specified fields, indicated by fields. The outcome is a boolean value that indicates whether all fields exist within the record.

Examples

Let’s look at some examples using the record [Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach"].

Check for a Single Field

To check if the record has the field “Citrus”, use the following code:

// Output: true
Record.HasFields( 
  [ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ], 
  "Citrus" 
)

In this example, the Record.HasFields function checks if the field “Citrus” exists in the record. Since we use a single value here you can provide it as text. The output is true because “Citrus” is indeed a field in the record.

To check if the record has the field “Tropical”, use the following code:

// Output: false
Record.HasFields(
  [ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ], 
  "Tropical"
)

In this example, the function checks if “Tropical” is a field in the record. The output is false because “Tropical” is not a field in the record.

Check for Multiple Fields

To check if the record has the fields “Citrus” and “Berry”, use the following code:

// Output: true
Record.HasFields( 
  [ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ], 
  { "Citrus", "Berry" }
)

The output is true because both fields are present in the record. Since we’re dealing with multiple values, you should provide the field names to search for within a list.

To check if the record has the fields “Citrus” and “Tropical”, use the following code:

// Output: false
Record.HasFields(
  [ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ], 
  { "Citrus", "Tropical" }
)

The output is false because, while “Citrus” is a field in the record, “Tropical” is not.

Other functions related to Record.HasFields are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/record-hasfields

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy