Record.HasFields
RecordReturns true if a record contains all of the specified field names.
Syntax
Record.HasFields(record as record, fields as any) as logicalParameters
| Name | Type | Required | Description |
|---|---|---|---|
record | record | Yes | The record to check. |
fields | any | Yes | A single field name as text, or a list of field name text values to check. |
Return Value
logical — True if all specified fields exist in the record, false otherwise.
Remarks
Record.HasFields checks whether a record contains all of the specified field names. Pass a single text value to check for one field, or a list of text values to require that all listed fields are present (AND logic — all-or-nothing). If any one field is absent, the function returns false.
This is the preferred alternative to wrapping field access in try/otherwise for schema checks. It is useful for:
- Defensive coding: verifying fields exist before accessing them with [FieldName] syntax
- Schema validation: confirming that records from semi-structured sources (JSON, API responses) have the expected shape
- Conditional branching: routing records to different transformation paths based on their structure
To check whether any of a list of fields is present (OR logic), combine with List.AnyTrue:
``powerquery
List.AnyTrue(List.Transform({"Email", "Phone"}, each Record.HasFields(record, _)))
``
Examples
Example 1: Check for a single field that exists
Record.HasFields([Name = "Alice", Age = 30], "Name")Result | |
|---|---|
| 1 | TRUE |
Example 2: Check for multiple fields — returns false if any are missing
Record.HasFields([Name = "Alice", Age = 30], {"Name", "Email"})Result | |
|---|---|
| 1 | FALSE |
Example 3: Use as a gate before accessing an optional field per Sales row
Table.AddColumn(
Table.SelectColumns(Table.FirstN(Sales, 4), {"OrderID", "CustomerName", "UnitPrice"}),
"FinalPrice",
each if Record.HasFields(_, "Discount")
then [UnitPrice] * (1 - [Discount])
else [UnitPrice],
type number
)OrderID | CustomerName | UnitPrice | FinalPrice | |
|---|---|---|---|---|
| 1 | 1 | Alice | 25 | 25 |
| 2 | 2 | Bob | 50 | 50 |
| 3 | 3 | Charlie | 15 | 15 |
| 4 | 4 | Alice | 75 | 75 |