Record.HasFields Function (Power Query M)

If you want to check whether a record contains a certain field before you try to read it, the Record.HasFields function is what you reach for. It returns true or false so you can test for a field instead of risking a missing-field error. Available in Excel (Power Query), Power BI Desktop, and Power BI Service.

Syntax of Record.HasFields Function

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

where

  • record (required, record). The record you want to inspect.
  • fields (required, any). The field name to look for, given as plain text like "Name". You can also pass a list like {"Name","City"} to check for several at once.

Returns: a logical value, either true or false. With a list of names it returns true only when every name is present.

In plain terms, you hand it a record and one or more field names, and it tells you whether those fields are in there.

Example 1: Check if a record has a field

Say you have a person record and you want to know whether it has a Name field.

let
Source = Record.HasFields([Name="Ava",City="Pune",Age=29],"Name")
in
Source

Result: true

The record does have a Name field, so the function returns true.

Example 2: A missing field returns false

The same record has no Email field, so asking for it returns false.

let
Source = Record.HasFields([Name="Ava",City="Pune",Age=29],"Email")
in
Source

Result: false

There is no Email field in the record, so the check comes back false rather than raising an error.

Example 3: Check for several fields at once

Pass a list of names to test for more than one field in a single call.

Here the record has Name, City, and Age, and you check for both Name and City.

let
Source = Record.HasFields([Name="Ava",City="Pune",Age=29],{"Name","City"})
in
Source

Result: true

Both Name and City are present, so you get a single true covering the whole list.

Example 4: With a list, every field must be present

A list works as an AND check. If even one name in the list is missing, the whole result is false.

Here you ask for Name and Email, but the record has no Email.

let
Source = Record.HasFields([Name="Ava",City="Pune",Age=29],{"Name","Email"})
in
Source

Result: false

Name is present but Email is not, so the list check fails and you get false.

Example 5: Field-name matching is case-sensitive

The names you pass must match the record’s field names exactly, including their casing. Here you ask for name in lowercase, but the field is Name.

let
Source = Record.HasFields([Name="Ava",City="Pune",Age=29],"name")
in
Source

Result: false

"name" does not match the field Name, so the function returns false even though the field is really there.

Example 6: Guard a field before reading it

The most common real use is to check for a field before you access it, so you never hit a missing-field error.

Here a person record has no Email. You read Email only if it exists, otherwise you fall back to a default message.

let
Person = [Name="Ava",City="Pune"],
Source = if Record.HasFields(Person,"Email") then Person[Email] else "No email on file"
in
Source

Result: No email on file

Because Email is missing, the if takes the else branch and returns the fallback text instead of erroring. This same guard idea is how Table.SelectColumns avoids errors on missing columns with its MissingField option.

Things to keep in mind with Record.HasFields

  • A list of names is an AND, not an OR. It returns true only when every name in the list is present (Example 4). For an OR, check each name separately and join them with or.
  • The order of names in the list does not matter. Record.HasFields only checks presence, so {"City","Name"} and {"Name","City"} give the same result.
  • It pairs naturally with safe field access. Wrap a record[Field] read in if Record.HasFields(...) then ... else ... so a missing field returns a fallback instead of throwing Expression.Error: The field 'X' of the record wasn't found.
  • To act on the missing names instead of a yes/no, use Record.FieldNames. Record.HasFields only tells you all-or-nothing. Compare your list against Record.FieldNames(record) when you need to know which fields are absent.
  • For checking a column instead of a field, this is the wrong function. Record.HasFields works on a record. To test whether a table has a column, check its header list with List.Contains against Table.ColumnNames(table).

Common questions about Record.HasFields

Does Record.HasFields work with a list of field names?

Yes. Pass a list like {"Name","City"} and it returns true only if the record has all of those fields. A single missing field makes the whole result false.

How do I check whether a record has at least one of several fields?

Record.HasFields with a list is all-or-nothing, so it cannot do an “any of” check on its own. Test each name separately and combine them with or, for example Record.HasFields(rec,"Email") or Record.HasFields(rec,"Phone").

List of All Power Query Functions

Related Power Query Functions / Articles:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.