Smart Systems for Home

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

Power Query Record.SelectFields 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.SelectFields function in Power Query (M language) allows you to extract a specific subset of fields from a record, effectively filtering out unwanted data and keeping only the columns or attributes you need. To use it, you provide the record you want to filter and a list of the field names you wish to retain: Record.SelectFields(record as record, fields as list).

What You'll Need

  • Microsoft Excel with Power Query installed or Power BI Desktop.
  • A data source that produces a Record object (such as a single row from a table, a JSON response, or a manually created record).
  • Access to the Advanced Editor or a Custom Column formula bar to write M code.

Step-by-Step

  1. Identify Your Record: Before using the function, ensure you are working with a Record and not a Table. In Power Query, a Record is represented as a single object with named fields. If your data is in a Table, you might be applying this function within a Table.AddColumn step to process a record stored inside a cell.
  2. Open the Formula Bar or Advanced Editor: To implement this function, you cannot rely solely on the ribbon buttons. Go to the View tab and check the Formula Bar box, or click Home > Advanced Editor to see the full M script.
  3. Define the Target Record: Identify the variable name of the record you want to filter. For example, if your previous step was named Source and it resulted in a single record, your target is Source.
  4. Construct the Field List: Create a list of the field names you want to keep. In M, lists are defined using curly braces {}. For example, if your record has fields named "FirstName", "LastName", "Email", and "Phone", and you only want the names, your list would be {"FirstName", "LastName"}.
  5. Apply the Function: Write the function using the following syntax: Record.SelectFields(Source, {"FirstName", "LastName"}). Replace Source with your actual record variable and the list with your desired fields.
  6. Execute and Verify: Press Enter. Power Query will now return a new record containing only the specified fields. Any fields not included in your list are discarded from the resulting object.
  7. Handling Dynamic Lists (Optional): If you do not want to hard-code the field names, you can pass a variable that contains a list of strings. This is useful when the fields you need to extract change based on another parameter in your workbook.

Common Mistakes

One of the most frequent errors is attempting to use Record.SelectFields on a Table instead of a Record. If you apply this function to a Table object, Power Query will return an expression error because the function expects a Record type. If you want to select columns from a table, you should use Table.SelectColumns instead.

Another common mistake is case sensitivity. The M language is strictly case-sensitive. If your record field is named "EmailAddress" and you write {"emailaddress"} in your list, the function will fail to find the field and will throw an error. Always double-check the exact casing of your field names.

Users also frequently forget the curly braces when defining the list of fields. Writing Record.SelectFields(Source, "FirstName") will result in an error because the second argument must be a list, not a text string. Even if you are only selecting one single field, it must be wrapped in braces: {"FirstName"}.

Tips

To make your queries more resilient, consider combining Record.SelectFields with Record.FieldNames. If you need to select all fields except one or two, you can generate a full list of names using Record.FieldNames(Source) and then use List.RemoveItems to filter out the unwanted fields before passing the final list into Record.SelectFields.

When working with API responses (JSON), you often end up with nested records. You can chain Record.SelectFields to drill down into specific parts of a nested structure. For example, you can select a specific record field that contains another record, and then apply the function again to that inner record to clean up the data further.

For better performance in large datasets, apply Record.SelectFields as early as possible in your transformation pipeline. Reducing the number of fields carried through the query reduces the memory footprint and can improve the speed of subsequent steps, especially when dealing with complex record transformations inside Table.AddColumn.

Troubleshooting

If the function returns an error stating that the field does not exist, first check the spelling and capitalization of the field names in your list. Then, verify that the record actually contains those fields at that specific step in the query. Use the preview window to inspect the record's contents immediately before the Record.SelectFields step.

If you receive a "Type Mismatch" error, you are likely passing a Table or a List instead of a Record. Check the icon next to the step name in the Applied Steps pane. A record icon looks like a small list/table hybrid; a table icon looks like a grid. If it is a table, you must either use Table.SelectColumns or use a function like Table.First to convert the first row of the table into a record before applying Record.SelectFields.

If the query is running slowly, check if you are calling Record.SelectFields inside a custom column for millions of rows. While the function is efficient, repeated calls on very large tables can add overhead. In such cases, check if you can achieve the same result using the UI-based "Choose Columns" feature on the table before expanding the records.

Frequently Asked Questions

What is the difference between Record.SelectFields and Table.SelectColumns?

Record.SelectFields is designed specifically for Record objects (single rows of data), whereas Table.SelectColumns is used for Table objects (multiple rows and columns). They perform similar logic—filtering for specific names—but they operate on different data types.

Can I use this function to remove a field?

Yes, indirectly. Since Record.SelectFields only keeps the fields you list, any field you omit is effectively removed. If you want to explicitly remove a field without listing all the others, Record.RemoveFields is the more direct function to use.

Does Record.SelectFields change the original record?

No. In Power Query, all operations are immutable. The function does not modify the original record but instead returns a new record containing only the selected fields. The original record remains unchanged in the previous step of your query.

Can I pass a variable list to the function?

Yes. Instead of hard-coding {"Field1", "Field2"}, you can create a separate step or a parameter that defines a list of strings and pass that variable as the second argument. This allows for dynamic column selection based on user input or external configuration.

What happens if I include a field name that doesn't exist in the record?

The function will throw an error. It requires every field name listed in the second argument to be present within the record. If there is a possibility that some fields may be missing, you should filter your list of fields using List.Intersect against Record.FieldNames before calling the function.