Smart Systems for Home

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

Power Query Record.RemoveFields Function 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.RemoveFields function in Power Query (M language) removes one or more specified fields from a record, returning a new record that contains only the remaining fields. To use it, you provide the source record and a list of the field names you wish to delete, such as Record.RemoveFields(SourceRecord, {"Field1", "Field2"}).

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query installed.
  • A data source that results in a Record object (such as a single row from a table, a JSON object, or a custom record created via [Field=Value] syntax).
  • Access to the Advanced Editor or a Custom Column dialog box to write M code.

Step-by-Step

  1. Identify Your Record Source: Before using the function, ensure you are targeting a Record, not a Table. If your data is in a Table, you must first drill down into a specific row or use a function like Table.TransformRows to apply the record logic to every row in the table.
  2. 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.
  3. Create a Custom Step: Click the fx icon next to the formula bar to create a new custom step. This allows you to manually enter the M code.
  4. Define the Record: If you are testing the function, you can create a dummy record. In the formula bar, type = [ID = 1, Name = "John", City = "New York", Age = 30] and press Enter. This creates a record with four fields.
  5. Apply Record.RemoveFields: Wrap your record source in the function. For example, if your previous step was named Source, enter: = Record.RemoveFields(Source, {"City", "Age"}).
  6. Specify the Fields as a List: Note that the second argument of the function must be a list. In M, lists are defined using curly brackets {}. Even if you are only removing one field, it must be written as {"FieldName"}.
  7. Verify the Result: Look at the Power Query preview window. The record should now only display the fields that were not included in your list (in the example above, only ID and Name).
  8. Integrate into a Table (Optional): If you need to remove fields from records nested within a table column, go to Add Column > Custom Column and use the formula Record.RemoveFields([ColumnName], {"FieldToRemove"}). This will create a new column containing the modified records.

Common Mistakes

One of the most frequent errors is forgetting the curly brackets for the field list. Because Record.RemoveFields expects a list of text values, passing a single string like "City" instead of {"City"} will result in an expression error stating that the function expected a list but received text.

Another common mistake is case sensitivity. Power Query is strictly case-sensitive. If your record field is named "City" and you attempt to remove "city" (lowercase), the function will fail because it cannot find a field with that exact name. Always double-check the capitalization of your field headers.

Users also often confuse Record.RemoveFields with Table.RemoveColumns. While they achieve similar goals, they operate on different data types. Table.RemoveColumns works on the entire table structure, whereas Record.RemoveFields works on a single record object. Attempting to use the record function on a table object will trigger a type mismatch error.

Finally, some users attempt to remove fields that do not exist in every single record of a dataset. If the function encounters a record that lacks one of the fields specified in the removal list, it will throw an error, halting the data refresh process.

Tips

To make your query more resilient, you can combine Record.RemoveFields with Record.FieldNames. If you want to remove all fields except for a few specific ones, it is often safer to use Record.SelectFields instead. However, if you have a record with 50 fields and only want to drop two, Record.RemoveFields is significantly more efficient than listing the 48 you want to keep.

When working with dynamic data where field names might change, avoid hard-coding the list of fields. Instead, create a separate list in Power Query that contains the names of the fields to be removed, and reference that list variable inside the function. This allows you to update the list in one place without editing the core logic of your transformation steps.

For those dealing with JSON API responses, Record.RemoveFields is invaluable for cleaning up metadata. APIs often return a large object containing the actual data along with various status codes, timestamps, and pagination links. By applying this function to the root record of the JSON response, you can strip away the noise and focus only on the data payload.

If you are applying this function within a Table.AddColumn step, consider naming your new column clearly (e.g., "CleanedRecord") to distinguish it from the original record column. This makes it easier to debug your steps if the output isn't what you expected.

Troubleshooting

If you encounter an error stating "The field 'X' of the record was not found," it means the function is looking for a field that doesn't exist in the record provided. To fix this, you can use a conditional check or a custom function to verify the field exists before attempting to remove it. Alternatively, use Record.FieldNames to get a list of all existing fields and use List.Intersect to only pass fields to Record.RemoveFields that actually exist in the record.

If the function seems to be doing nothing, check if you are actually assigning the result to a step. In Power Query, functions do not modify the data in place; they return a new version of the data. Ensure your step is defined as StepName = Record.RemoveFields(...) and that the subsequent step references StepName.

If you are seeing a "Type Mismatch" error, verify that the first argument is indeed a Record. If you are working with a table, you cannot apply this function directly to the table. You must use Table.TransformColumns or a custom column to reach the record level inside the table cells.

When the query runs slowly on large datasets, check if you are calling Record.RemoveFields inside a loop or a complex custom column. While the function itself is fast, applying it to millions of records can add overhead. If possible, try to filter or remove columns at the table level using Table.SelectColumns before breaking the table down into records.

Frequently Asked Questions

Can I remove multiple fields at once?

Yes. You simply add the names of all fields you want to remove into the list (the second argument), separated by commas. For example: {"Field1", "Field2", "Field3"}.

Is there a difference between Record.RemoveFields and Record.SelectFields?

Yes. Record.RemoveFields takes a list of fields to delete and keeps everything else. Record.SelectFields takes a list of fields to keep and deletes everything else. Use the former when you have a few fields to drop and the latter when you only want a few specific fields.

Does this function permanently delete data from the source?

No. Power Query is a transformation engine, not a database editor. Record.RemoveFields only removes the fields from the data stream as it is being loaded into your model; it does not modify the original source file or database.

What happens if I try to remove a field that isn't there?

The function will return an error. It requires all fields listed in the removal list to be present in the record. To avoid this, you can pre-filter the list of fields to remove using List.Intersect against the actual Record.FieldNames of the record.

Can I use this function inside a Table.AddColumn step?

Yes, this is a common use case. If a column in your table contains records (common with JSON or Web API imports), you can use Record.RemoveFields([ColumnName], {"Field"}) to clean the records within that column.