Power Query Record.FieldOrDefault Guide
In this article
To use the Power Query Record.FieldOrDefault function, you provide a record and a field name as the first two arguments, followed by a value that the function will return if the specified field does not exist in that record. This function is essential for preventing errors in your M code when you are attempting to access specific data points that may be missing from certain rows or nested JSON objects. Instead of your entire query failing with a "Field not found" error, this function allows your data transformation process to continue smoothly by substituting a safe, predefined value.
What You'll Need
- Microsoft Excel or Power BI Desktop with the Power Query editor enabled.
- A dataset containing records, such as a JSON import, a web API response, or a nested table structure.
- Basic familiarity with the M formula language and the Advanced Editor.
Step-by-Step
- Load your data into Power Query: Open Excel or Power BI, go to the Data tab, and select Get Data. Choose your source (e.g., JSON, Web, or a Table). Once the data is loaded, click 'Transform Data' to open the Power Query Editor window.
- Identify the target record: Locate the column containing the records you wish to inspect. In Power Query, records usually appear as a cell containing the word 'Record'. If you have a list of records, you will likely need to expand them or use a custom column to access individual elements.
- Open the Custom Column dialog: In the Power Query Editor ribbon, navigate to the 'Add Column' tab and select 'Custom Column'. This opens a dialog box where you can write your own M code.
- Construct the Record.FieldOrDefault formula: In the 'Custom column formula' box, you will enter the function using the following syntax:
Record.FieldOrDefault([ColumnName], "FieldName", "DefaultValue").[ColumnName]: This is the name of the column that contains the record. Always wrap column names in square brackets."FieldName": This is the name of the specific field you are looking for within that record. It must be enclosed in double quotes."DefaultValue": This is the value the function returns if the field is missing. This can be a string, a number, a date, or evennull. It must also be enclosed in double quotes if it is text.
- Apply and verify: Click 'OK'. A new column will appear in your preview window. Look closely at the rows where the field was expected to be missing; you should see your 'DefaultValue' appearing in those cells instead of an error message.
- Check for case sensitivity: Ensure that the field name in your formula matches the casing in the source data exactly. M is a case-sensitive language, so "Price" and "price" are treated as different fields.
- Finalize the transformation: Once you are satisfied that the function is handling missing fields correctly, you can remove the original record column if it is no longer needed by right-clicking the column header and selecting 'Remove'.
Common Mistakes
One of the most frequent errors is forgetting to wrap the field name in double quotes. Because Record.FieldOrDefault expects a text string for the second argument, omitting quotes will cause the engine to look for a variable or a column named after that field, leading to a syntax error. Another common mistake is a mismatch in data types. If your default value is a number (e.g., 0) but the existing fields in that column are text, you may encounter errors in subsequent steps when you try to perform mathematical operations on the column. Always ensure your default value matches the intended data type of the column.
Users also frequently struggle with case sensitivity. If your JSON source provides a field called CustomerID and you write Record.FieldOrDefault([Data], "customerid", 0), the function will not find the match and will return 0 every single time, even if the data is actually present. Always verify the exact casing of your source keys.
Tips
For maximum efficiency, use null as your default value if you intend to use the 'Replace Values' or 'Fill Down/Up' features later in your workflow. Using null tells Power Query that the data is truly absent, which is often more semantically correct than using a dummy value like "N/A" or 0, especially for numeric columns where 0 might be a valid data point.
If you are working with deeply nested records, you can chain this function or use it within a Table.AddColumn step to extract values from multiple levels of a JSON hierarchy. This is much safer than using the dot notation (e.g., [Record][SubRecord][Field]), which will crash the entire refresh if any single level of that hierarchy is missing for even one row.
Troubleshooting
If the function is not working as expected, follow these steps in order:
- Verify the Data Type: Ensure the column you are referencing is actually typed as a 'Record'. If the column is typed as 'Text' (which often happens with JSON imports that haven't been parsed), the function will fail. You must first use
Json.Documentto convert the text into a record structure. - Check for typos: Double-check the spelling of both the column name and the field name. Remember that the column name is in brackets
[Column]and the field name is in quotes"Field". - Inspect the Source: Click on the white space next to the word 'Record' in a cell to see the record preview at the bottom of the screen. This allows you to confirm that the field you are looking for actually exists in the source data and to check its exact spelling and casing.
- Test with a single row: Create a temporary custom column that only looks at one specific record to see if the logic holds up. This helps isolate whether the issue is with your formula or the broader dataset.
Frequently Asked Questions
Can I use Record.FieldOrDefault with a list instead of a record?
No. This function is specifically designed for the Record data type. If you are trying to find a value in a list, you should use List.Contains or List.PositionOf combined with an if statement to handle missing values.
What happens if the field exists but the value is null?
Record.FieldOrDefault only triggers the default value if the field is missing from the record entirely. If the field exists but contains a null value, the function will return null, not your default value. To handle both missing fields and null values, you may need to use if [Field] = null then ... else ....
Is this function faster than using an 'if' statement?
In most practical scenarios, the performance difference is negligible. However, Record.FieldOrDefault is more concise and readable than writing a complex if Record.HasFields(...) then ... else ... block, making your M code easier to maintain.
Can the default value be another record or a list?
Yes. The third argument can be any valid M data type, including another record, a list, a table, or even a function. This is particularly useful when you want to provide a standard set of default attributes for a missing object.
Do I need to use the Advanced Editor for this?
While you can write the code directly in the Advanced Editor, it is much easier and safer to use the 'Custom Column' dialog in the user interface, which provides a preview of your work as you type.