Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Record.HasFields: Usage Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Record.HasFields function in Power Query checks whether a record contains one or more specified fields, returning true if all fields exist and false if any are missing. It is a simple, reliable way to validate data before performing operations that depend on certain columns existing. You can use it in the Power Query Editor (available in Excel and Power BI) by adding a custom column or writing an M expression. No additional setup is needed—just have a query with a record (often a row from a table) and the field names you want to check.

What You'll Need

  • A Power Query environment: Excel (Get & Transform) or Power BI Desktop.
  • An existing query that returns records—typically a table imported from a source like Excel, CSV, or a database.
  • Basic familiarity with the Power Query Editor interface (the ribbon and the formula bar).

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and click Get Data > Launch Power Query Editor. In Power BI, click Transform Data from the Home tab. If you already have a query, select it in the Queries pane.
  2. Identify the record you want to test: In Power Query, each row of a table is a record. You can test a specific row by referencing it, or you can test all rows by adding a custom column. For this guide, we'll add a custom column to check each row.
  3. Add a custom column: Go to the Add Column tab and click Custom Column. This opens a dialog where you can enter an M formula.
  4. Enter the formula: In the Custom Column Formula box, type something like:
    = Record.HasFields([Column1], {"Field1", "Field2"})
    Replace [Column1] with the name of a column that contains a record (or just use _ if you're working with a list of records), and replace "Field1", "Field2" with the actual field names you want to check. For a single field, you can use a text value instead of a list: = Record.HasFields([RecordColumn], "FieldName").
  5. Click OK: The new column will appear with true or false for each row, indicating whether all specified fields exist in that row's record.
  6. Use the result in further steps: You can now filter on this column to keep only rows where the fields exist, or use it in conditional logic. For example, filter the column to true to remove rows that lack the required fields.

If you are testing a single record outside a table (e.g., a record created with [A=1, B=2]), you can write = Record.HasFields([A=1, B=2], {"A", "B"}) directly in a query. The function works the same way—it just checks for the presence of the field names in the record.

Common Mistakes

  • Using a list when a single field is expected: If you pass a list like {"Field1"} when you meant to check just one field, it still works, but some users accidentally use a comma-separated string like "Field1, Field2"—that will fail because it's treated as a single field name.
  • Forgetting that field names are case-sensitive: Power Query records are case-sensitive. If your field is named Name but you type name, the function returns false even though the field exists.
  • Checking fields on a table instead of a record: Record.HasFields expects a record, not a table. If you pass a table, you'll get an error. Make sure you're referencing a single row (like Table.First(YourTable)) or a column that contains records.
  • Assuming it checks for nested fields: The function only checks top-level fields. If you need to verify a field within a nested record, you must first access that nested record (e.g., Record.HasFields([NestedRecord], "InnerField")).

Tips

  • Use it for data validation: Before merging queries or expanding columns, use Record.HasFields to ensure the expected columns exist, avoiding errors later.
  • Combine with Table.SelectRows: To filter a table to only rows that have certain fields, use Table.SelectRows(YourTable, each Record.HasFields(_, {"Field1"})). This is more efficient than adding a custom column and then filtering.
  • Check multiple fields at once: Pass a list of field names to verify all of them exist. The function returns true only if every field in the list is present.
  • Use in error handling: Wrap a Record.Field call in an if statement with Record.HasFields to avoid errors when a field might be missing: if Record.HasFields(rec, "Price") then Record.Field(rec, "Price") else null.
  • Remember that field names are text: Always enclose field names in quotes. If you use a variable that contains the field name, that's fine, but the variable must be a text value.

Troubleshooting

If the function returns an error or unexpected results, try these checks in order:

  1. Check the error message: If you see "The value is not a record," it means you passed something other than a record. Ensure you're referencing a record, not a table or a list. Use Table.First(YourTable) to get a single record from a table.
  2. Verify field names: Double-check the spelling and case of the field names. Use Record.FieldNames(YourRecord) to see the exact names available. This is especially helpful when working with data from external sources where column names might be transformed (e.g., spaces removed or changed to underscores).
  3. Test with a simple example: Create a new blank query and enter = Record.HasFields([A=1, B=2], "A"). This should return true. If it doesn't, your environment might have a different version of the function, but the behavior is standard.
  4. Check if the field is actually present but null: Record.HasFields checks for the existence of the field, not whether the value is non-null. If a field exists with a null value, the function returns true. If you need to check for non-null values, combine with Record.Field and test for null.
  5. Look at the data type: If you're referencing a column that contains records, make sure the column is of type record. Sometimes Power Query might treat it as text or a list. Use the data type indicator in the column header to verify.

Frequently Asked Questions

Can Record.HasFields check for multiple fields at once?

Yes, pass a list of field names: Record.HasFields(record, {"Field1", "Field2"}). It returns true only if all fields exist.

Does the function work with nested records?

It only checks top-level fields. To check a nested field, first access the nested record, e.g., Record.HasFields(record[SubRecord], "InnerField").

What happens if the record is null?

If the record itself is null, the function returns an error. You can handle this with a null check: if record <> null then Record.HasFields(record, "Field") else false.

Is there a performance difference between using Record.HasFields and checking with Record.Field?

Record.HasFields is generally faster and cleaner because it avoids the error handling that Record.Field would require. For large datasets, using it in a filter condition is efficient.

Can I use Record.HasFields in a custom function?

Yes, you can define a custom function that takes a record and a field name, and uses Record.HasFields inside. This is useful for repeated validation logic.

Why does Record.HasFields return false even though the field appears in the data preview?

This often happens due to case sensitivity or invisible characters in the field name. Use Record.FieldNames to copy the exact name, or check for leading/trailing spaces.