Power Query Record.FieldNames Function Guide
In this article
The Record.FieldNames function in Power Query is used to extract a list of all the field names (column headers) from a specific record. To use it, you simply wrap the record object in the function: Record.FieldNames(YourRecordName). This is primarily used when you need to dynamically identify which columns exist in a record without hard-coding the names, which is essential for creating flexible data pipelines that don't break when source columns change.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- The Power Query Editor open.
- A data source that contains Record objects (such as a JSON import, a Web API response, or a table where you have converted rows into records).
- Basic familiarity with the Power Query formula bar.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Identify Your Record: Ensure you have a record to work with. If you have a table, you can right-click a cell in a column containing records and select Drill Down. This converts the current view from a table to a single record object. Alternatively, if you are importing a JSON file, Power Query often presents the data as a record initially.
- Access the Formula Bar: If the formula bar is not visible, go to the View tab and check the box for Formula Bar. This allows you to manually enter M code.
- Insert a Custom Step: Click the
fxicon to the left of the formula bar. This creates a new step in the Applied Steps pane. - Enter the Function: In the formula bar, type
= Record.FieldNames(NameOfPreviousStep). Replace "NameOfPreviousStep" with the actual name of the step immediately preceding this one (usually something likeSourceorCustom1). - Execute the Command: Press Enter. Power Query will now transform the record into a List object. Each item in this list is a text string representing one of the field names from the original record.
- Utilize the Resulting List: You can now use this list for further transformations. For example, you can convert this list back into a table by clicking the "To Table" button in the Transform ribbon, or use it inside a
Table.ColumnNamesorTable.SelectColumnsfunction to dynamically filter data.
Common Mistakes
The most frequent error is attempting to use Record.FieldNames on a Table instead of a Record. In Power Query, a Table and a Record are fundamentally different data types. If you apply this function to a table, you will receive an expression error stating that the value cannot be converted to a record. If you have a table and want the column names, you must use Table.ColumnNames instead.
Another common mistake is case sensitivity. Power Query's M language is strictly case-sensitive. Writing record.fieldnames (all lowercase) will result in a syntax error because the function must be written exactly as Record.FieldNames.
Users also often forget that the output of this function is a List, not a Table. If you try to perform table-specific operations (like filtering rows or merging) immediately after using this function, it will fail. You must first convert the list to a table or use list-specific functions like List.Contains or List.Select.
Tips
To make your queries truly dynamic, combine Record.FieldNames with List.Select. This allows you to find only the fields that meet certain criteria, such as those starting with a specific prefix (e.g., "Date_"). This is incredibly useful when dealing with APIs that return a varying number of fields based on the date range requested.
If you are dealing with a table where each row is a record, you can use Table.AddColumn to apply Record.FieldNames to every row. This allows you to see if certain rows have missing fields or unexpected extra fields, which is a great way to perform data quality audits on semi-structured JSON data.
When working with large records, use the resulting list to create a dynamic "Unpivot" operation. By knowing exactly which fields exist, you can programmatically tell Power Query to unpivot all columns except for a few primary keys, ensuring that new columns added to the source data in the future are automatically captured and unpivoted without manual intervention.
Troubleshooting
If you encounter an error saying "We cannot convert the value to type Record," check the icon to the left of your data in the preview window. If you see a table grid icon, you are dealing with a table. To fix this, either use Table.ColumnNames or drill down into a specific row to create a record before applying the function.
If the list returned is empty, verify that the record actually contains fields. Some API responses return a record that is essentially a wrapper with no internal fields, or the record might be nested deeper than you think. Use the navigation pane to drill down into the specific nested record that contains the actual data fields.
If you see a "Token Literal expected" error, check your parentheses and commas. Ensure the record name is inside the parentheses and that you haven't accidentally deleted a closing bracket in the step preceding the function call.
Frequently Asked Questions
Does Record.FieldNames return the values of the fields?
No, it only returns the names (the keys) of the fields. If you need the values, you should use Record.FieldValues, which returns a list of the data contained within those fields.
Can I use this function to rename columns?
Not directly, but you can use the list it generates as an input for Table.RenameColumns. By creating a list of pairs (old name, new name) based on the output of Record.FieldNames, you can automate renaming processes.
Is there a difference between this and Table.ColumnNames?
Yes. Table.ColumnNames is designed for Table objects, whereas Record.FieldNames is designed for Record objects. While they both return a list of text strings, they cannot be used interchangeably on different data types.
Will this function update automatically if the source record changes?
Yes. Because it is a functional step in the Power Query pipeline, every time the query refreshes, the function re-evaluates the record and returns the current list of field names, making it ideal for dynamic schemas.
Can I filter the list of field names returned?
Yes. Since the output is a standard Power Query List, you can wrap the function in List.Select to filter for specific names or use List.RemoveItems to exclude specific fields from the result.