Record.SelectFields Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Record.SelectFields function in Power Query takes a record and gives you back a smaller record with only the fields you ask for. You pass it a list of field names, and it drops everything else.

It gets even more useful inside a table. Each row of a table is a record, so you can pick a set of columns for every row and add them up or average them.

In this article, I’ll show you how it works with practical examples, starting with a single record and moving on to tables where the columns you need can change.

Follow along with the example file

Record.SelectFields Function Examples.xlsx

Download

Record.SelectFields Function Syntax

Record.SelectFields(record as record, fields as any, optional missingField as nullable number) as record
  • record – the record you want to pick fields from. In a table, each row is a record, and its column names are the field names.
  • fields – a list of the field names you want to keep, such as {"Item","Price"}. Field names are case-sensitive, so “price” won’t match a field called Price.
  • missingField (optional) – what to do when a name in your list isn’t in the record. Leave it out and you get an error, use MissingField.Ignore to skip the missing name, or use MissingField.UseNull to add it with a null value.

You can also pass a single field name in double quotes instead of a list. You still get back a record, with just that one field (the Record.Field function returns the value itself instead).

What it returns: A new record that has only the fields you listed. The fields come back in the order of your list, not the order they had in the original record.

The Record.SelectFields function is part of the core Power Query M library, so it works anywhere you can use Power Query, including Excel for Microsoft 365 and Power BI.

When to Use the Record.SelectFields Function

Use this function when you need to:

  • Keep only a few fields from a record and drop the rest
  • Pick fields using a list of names stored in a step, so you can change it in one place
  • Put fields in a specific order
  • Avoid errors when some of the fields you ask for might not be there
  • Add up or average a group of columns in each row of a table
  • Let a table in your workbook decide which columns a query uses

If you want to keep or drop columns in a whole table, use the Table.SelectColumns function instead. Record.SelectFields works on one record at a time, such as a single row.

Example 1: Keep Only Some Fields from a Record

Let’s start with the basics. A record is a set of named values in square brackets, and Record.SelectFields keeps only the ones you name.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= Record.SelectFields([Ticket="TK-3071",Priority="High",Status="Open"],{"Ticket","Status"})

Result: A record with Ticket (TK-3071) and Status (Open)

Record.SelectFields keeps the Ticket, Priority and Status fields of the ticket record

Here, the support ticket record has three fields, and I asked for two of them. So Priority is dropped, and you get a smaller record with just the fields you need.

When you know the field names up front, there’s also a shortcut: put the record in front of double square brackets that list the field names. [Ticket="TK-3071",Priority="High",Status="Open"][[Ticket],[Status]] gives the same record.

The function version earns its place when the names sit in a list you can change, or when some of them might be missing. That’s what the rest of the examples are about.

In the example file, Examples 1 to 4 have no worksheet, because their data is typed right inside the query. To open one, go to Data > Queries & Connections and double-click the query name.

Example 2: Use a List of Field Names Stored in a Step

Here’s where Record.SelectFields starts to pull ahead of the double square bracket shortcut. Suppose you keep the list of field names in its own step, so you can change it in one place.

Create a new blank query, open the Advanced Editor (Home > Advanced Editor), and use this code:

let
OrderRecord = [Order="SO-5512",Customer="Janice Hayes",City="Austin",Amount=240],
Fields = {"Customer","Amount","Order"},
Result = Record.SelectFields(OrderRecord,Fields)
in
Result

Result: A record with Customer (Janice Hayes), Amount (240), and Order (SO-5512), in that order

The result has Customer, Amount and Order, in the order of the Fields list

Here, the Fields step holds a list of three names, and Record.SelectFields reads that list to decide what to keep. City isn’t in the list, so it’s gone.

Also notice the order. In the original record, Order came first, but in the result it comes last, because Record.SelectFields returns the fields in the same order as your list.

You can’t pass a list held in a step to the double square bracket shortcut. It only accepts field names typed right inside the brackets.

Example 3: Skip Fields That Don’t Exist

So far every name we asked for was in the record. Now suppose you want Name, Email, and Phone from an employee record, but this record has no Phone field.

If you leave out the third argument, the query fails with this message: “Expression.Error: The field ‘Phone’ of the record wasn’t found.”

To skip any name that isn’t there, add MissingField.Ignore as the third argument. Create a new blank query, open the Advanced Editor, and use this code:

let
Employee = [Name="Mario Adams",Email="mario.adams@example.com",Department="Finance"],
Result = Record.SelectFields(Employee,{"Name","Email","Phone"},MissingField.Ignore)
in
Result

Result: A record with Name (Mario Adams) and Email (mario.adams@example.com)

MissingField.Ignore skips the missing Phone field and returns Name and Email

Here, Name and Email exist, so they’re kept. Phone doesn’t exist, so it’s quietly left out instead of breaking the query.

There’s no shortcut version of this one. The double square bracket syntax either errors on a missing field or, with a question mark after it, returns it as null.

Example 4: Return Missing Fields as Null

Sometimes you don’t want a missing field to disappear. You want every record to come back with the same fields, with a blank where there’s no value.

That’s what MissingField.UseNull does. Using the same employee record, create a new blank query, open the Advanced Editor, and use this code:

let
Employee = [Name="Mario Adams",Email="mario.adams@example.com",Department="Finance"],
Result = Record.SelectFields(Employee,{"Name","Email","Phone"},MissingField.UseNull)
in
Result

Result: A record with Name (Mario Adams), Email (mario.adams@example.com), and Phone (null)

MissingField.UseNull returns Name, Email, and Phone as null

Here, Phone isn’t in the record, so Record.SelectFields adds it with a null value. The result always has Name, Email, and Phone, which keeps later steps from tripping over a missing field.

When you type the field names directly, the shortcut with a question mark does the same thing. Employee[[Name],[Email],[Phone]]? also returns Phone as null.

Example 5: Average All Columns That Start with the Same Word

Now let’s use Record.SelectFields on a table. Below is a training report with each trainee’s scores in three modules and their attendance, and I want the average module score in an Average Score column.

Training table with Module 1, Module 2, Module 3 and Attendance % columns

The tricky part is that Attendance % is a number too, so you can’t just average every number in the row. And when a Module 4 column gets added later, you want it included without editing the query.

Create a new blank query, open the Advanced Editor, and use this code. It reads the table by name, so you don’t have to load it into Power Query first.

let
Source = Excel.CurrentWorkbook(){[Name="Training"]}[Content],
ModuleCols = List.Select(Table.ColumnNames(Source),each Text.StartsWith(_,"Module")),
#"Added Average" = Table.AddColumn(Source,"Average Score",each List.Average(Record.FieldValues(Record.SelectFields(_,ModuleCols))),type number)
in
#"Added Average"

Result: A new Average Score column (83, 92, 77, 86, 72, and 90)

A new Average Score column: 83, 92, 77, 86, 72, 90

Here, the Table.ColumnNames function gives a list of all the column names, and the List.Select function keeps only the ones that start with “Module”. So ModuleCols is the list Module 1, Module 2, and Module 3.

Then the Table.AddColumn function adds the Average Score column, working out its value one row at a time. The each keyword runs the expression after it once for every row, and the underscore (_) stands for the current row, which is a record.

So Record.SelectFields(_,ModuleCols) keeps only the three module scores from that row. The Record.FieldValues function turns them into a list of numbers, and the List.Average function averages them.

For Kenneth Henderson, that’s 82, 90, and 77, which averages to 83.

Add a Module 4 column to the table and refresh, and it’s part of the average automatically.

Example 6: Total the Columns Listed in a Table

This next one lets your workbook decide which columns to add up. Below is an inventory list with the stock of each item in four locations, and I want a Stock on Hand column that adds up only the locations listed in a separate table.

Inventory table with Main Warehouse, Backroom, Showroom and In Transit columns

The locations to count sit in a one-column table called CountLocations, on its own sheet (Example 6 Locations). Right now it lists Main Warehouse, Backroom, and Showroom, so In Transit is left out.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Inventory"]}[Content],
Locations = Excel.CurrentWorkbook(){[Name="CountLocations"]}[Content][Location],
#"Added Stock" = Table.AddColumn(Source,"Stock on Hand",each List.Sum(Record.FieldValues(Record.SelectFields(_,Locations))),type number)
in
#"Added Stock"

Result: A new Stock on Hand column (144, 294, 99, 170, 72, and 53)

A new Stock on Hand column: 144, 294, 99, 170, 72, 53

Here, the Locations step reads the Location column of the CountLocations table as a list. For each row, Record.SelectFields keeps only those three columns, and the List.Sum function adds them up.

For the Yoga Mat, that’s 120 plus 18 plus 6, which gives 144.

If you also want to count stock that’s in transit, add In Transit to the CountLocations table and refresh the query. You never have to touch the M code.

Important: Each location in the CountLocations table has to match a column name exactly, including capital letters. If someone types “backroom” or adds a trailing space, every row of Stock on Hand shows an error.

Example 7: Total Columns That Might Not Be There Yet

Here’s a situation that comes up with monthly exports. Below is an expense report with each department’s spending by category, and I want a Travel Total column that adds up Airfare, Hotel, Meals, and Taxi.

Expenses table with Airfare, Hotel, Meals and Client Gifts columns and no Taxi column

The catch is that this month’s export has no Taxi column, because nobody took a taxi, but next month it might be there.

Client Gifts isn’t a travel cost, so it should stay out of the total either way.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Expenses"]}[Content],
Categories = {"Airfare","Hotel","Meals","Taxi"},
#"Added Total" = Table.AddColumn(Source,"Travel Total",each List.Sum(Record.FieldValues(Record.SelectFields(_,Categories,MissingField.Ignore))),type number)
in
#"Added Total"

Result: A new Travel Total column (2390, 1240, 395, 830, 750, and 1770)

A new Travel Total column: 2390, 1240, 395, 830, 750, 1770

Here, the Categories step lists all four travel categories. MissingField.Ignore skips Taxi because the column isn’t there, so each row adds up Airfare, Hotel, and Meals.

Without MissingField.Ignore, every row of Travel Total would show an error, because Record.SelectFields can’t find a Taxi field.

Blank cells are fine too. They show up as null in Power Query (HR has no airfare and IT has no meals), and the List.Sum function skips nulls, so HR gets 300 plus 95, which is 395.

When a Taxi column shows up in next month’s export, refresh the query and it’s included in the total on its own.

Important: MissingField.Ignore also skips names you misspell. If you type “Hotels” instead of “Hotel”, there’s no error, and the hotel cost silently drops out of every total.

Other Excel Articles You May Also Like:

  • Number.Abs
  • Table.ExpandRecordColumn – Use Table.ExpandRecordColumn in Power Query M to turn a record column into columns.
  • Table.FromRecords – Use Table.FromRecords in Power Query M to build a table from records.
  • Table.RemoveColumns – Use Table.RemoveColumns in Power Query M to delete columns.
  • Json.Document – Use Json.Document in Power Query M to parse JSON text.
  • List.Skip – Use List.Skip in Power Query M to skip items at the start of a list.
  • List.RemoveNulls – Use List.RemoveNulls in Power Query M to drop null values from a list.
  • Table.SelectRows – Use Table.SelectRows in Power Query M to keep only the rows that meet a condition.

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.