Power Query Record.Field: Access Values Safely
In this article
The Record.Field function in Power Query (M language) is used to extract a specific value from a record based on its field name. Unlike the dot notation (record[Field]) which can cause errors if a column is missing or renamed, Record.Field allows you to handle these scenarios gracefully, often returning null or a default value instead of crashing the query. This makes it essential for building robust data transformation steps that survive source schema changes.
What You'll Need
- Power BI Desktop, Excel, or Dataflows: Any environment where you can access the Power Query Editor.
- A Source Table with Records: Typically, this occurs when you have expanded JSON data, nested tables, or lists of records within your dataset.
- Basic M Language Knowledge: Understanding how to open the Advanced Editor and write simple expressions.
Step-by-Step
- Open the Power Query Editor: Load your data into Power Query. If you are using Excel, go to the Data tab and select Get Data. In Power BI, click Transform Data.
- Identify Your Record Column: Locate the column containing the records. These columns usually display as "Table" or "Record" icons rather than standard text or numbers. Click on one cell to see the preview in the bottom pane; if you see key-value pairs like
{Name = "John", Age = 30}, you are working with a record. - Add a Custom Column: Go to the Add Column tab in the ribbon and click Custom Column. Give your new column a descriptive name, such as "ExtractedValue".
- Write the Record.Field Expression: In the formula box, use the following syntax:
= Record.Field([ColumnName], "FieldName"). Replace[ColumnName]with the actual name of your record column (e.g.,[Data]) and"FieldName"with the specific key you want to retrieve (e.g.,"Email"). Note that the field name must be enclosed in double quotes. - Handle Missing Fields (Optional but Recommended): To prevent errors if the field does not exist in some rows, wrap the function in an
try...otherwiseblock:= try Record.Field([Data], "Email") otherwise null. This ensures that if the "Email" key is missing, the result isnullinstead of an error. - Confirm and Close: Click OK to create the column. Verify that the values appear correctly in the new column. If they do, click Close & Apply to load the transformed data into your model.
Common Mistakes
One frequent error is forgetting the double quotes around the field name. Writing Record.Field([Data], Email) will fail because Power Query interprets Email as a variable name rather than a string literal. Always use "Email".
Another common issue is confusing Record.Field with Table.Column. If your data is stored in a table within a record, you cannot directly extract a single value without first accessing the table. Ensure you know whether you are dealing with a flat record or a nested structure.
Users also often forget that field names are case-sensitive. If the source data has a field named "firstName" and you request "FirstName", the function will return an error or null depending on your error handling setup.
Tips
Use Record.FieldOrDefault for cleaner code. Instead of writing a complex try...otherwise statement, you can use = Record.FieldOrDefault([Data], "Email", "N/A"). This function automatically returns the specified default value ("N/A" in this example) if the field is missing, making your queries more readable and concise.
If you need to extract multiple fields from the same record, consider using Table.ExpandRecordColumn via the UI instead of creating multiple custom columns. This approach is generally more performant and easier to maintain for large datasets.
Always check the data type of the extracted field. Sometimes, a value that looks like a number might be returned as text. Use Text.From or Number.From functions if necessary to ensure consistency before loading the data.
Troubleshooting
If you receive an error stating "The field 'X' was not found," verify the exact spelling and casing of the field name. You can inspect the available fields by clicking on the expand icon next to the column header in the Power Query Editor. This shows all keys present in the record for that row.
If the result is always null despite the field existing, check if the field name contains special characters or spaces. While rare, some sources may encode these differently. Try copying the field name directly from the preview pane to ensure accuracy.
For performance issues, avoid using Record.Field inside iterative loops or complex conditional logic across millions of rows. If possible, expand the record column once using the built-in UI options, then filter or transform the resulting flat table.
Frequently Asked Questions
What is the difference between Record.Field and dot notation?
Dot notation (record[Field]) is shorter but throws an error if the field is missing. Record.Field is a function call that can be wrapped in error-handling structures like try...otherwise or replaced by Record.FieldOrDefault, allowing for safer data extraction when schemas vary.
Can I use Record.Field on a list?
No, Record.Field works only on records. For lists, you would use index-based access like list{0} or functions like List.Item or List.Select.
How do I extract a nested record value?
You chain the functions. For example, if Address is a record inside Data, and you want the City, use: = Record.Field(Record.Field([Data], "Address"), "City").
Does Record.Field change the data type?
It preserves the original data type of the field value. If the field contained a date, the result is a date. If it contained text, the result is text. You may still need to cast types explicitly if downstream steps require them.