Record.Field 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.Field function in Power Query returns the value of one field from a record. You give it the field name as text, so you can type the name or pull it from a step, a cell, or another column.

Each row of a table is also a record, with the column names as its field names. So Record.Field lets you pick a column when you don’t know its name until the query runs.

In this article, I’ll show you how it works with practical examples, from a single record to picking a different column for every row of a table.

Follow along with the example file

Record.Field Function Examples.xlsx

Download

Record.Field Function Syntax

Record.Field(record as record, field as text) as any
  • record – the record you want to get a value from. In a table, each row is a record, and its column names are the field names.
  • field – the name of the field you want, as text (in double quotes, or a step or column that holds the name). Field names are case-sensitive.

What it returns: The value stored in that field, which can be any type: a number, text, a date, or even another record or table. If the record has no field with that name, you get an error.

The Record.Field function is part of the core Power Query M library, so it works wherever Power Query does, including Excel 365, 2024, 2021, 2019, 2016, and Power BI.

When to Use the Record.Field Function

Use this function when you need to:

  • Get a value from a record when the field name is stored in a step or a variable
  • Let a cell in your workbook decide which column a query reads
  • Pick a different column for each row, based on a value in that row
  • Work with the newest columns in a report that gains a new column every month
  • Use a record as a small lookup list, such as a rate card

Example 1: Get a Value from a Record

Let’s start with the basics. A record is a set of named values in square brackets, and Record.Field pulls out one of them by 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.Field([Item="Office Chair",Price=185,Stock=42],"Price")

Result: 185

Record.Field returns 185 from the Price field of the record

Here, the record has three fields (Item, Price, and Stock), and I asked for the one named “Price”. So Record.Field returns 185.

When you already know the field name, you can also write it with the shorter square bracket syntax. [Item="Office Chair",Price=185,Stock=42][Price] gives the same 185.

You need Record.Field when the field name isn’t fixed, and that’s what the rest of the examples are about.

In the example file, Examples 1 and 2 have no sheet because they don’t use any data. You can open them from Data > Queries & Connections.

Example 2: Use a Field Name Stored in a Step

Here’s where Record.Field starts to make sense. Suppose the name of the field you want is stored 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
Product = [Item="Office Chair",Price=185,Stock=42],
FieldName = "Stock",
Result = Record.Field(Product,FieldName)
in
Result

Result: 42

Record.Field reads the field name from the FieldName step and returns 42

Here, the FieldName step holds the text “Stock”, and Record.Field reads that text to decide which field to return. So you get 42, the stock count.

You can’t do this with the square bracket syntax. Product[FieldName] looks for a field literally called FieldName, and since there’s no such field, you get an error.

Example 3: Pick a Price Tier Based on a Cell Value

This next one is a very practical use case. Below is an order list with the quantity of each product and its Retail, Wholesale, and Distributor prices, and I want an Order Total column that uses whichever price tier I type in a separate cell.

Products table with Qty, Retail, Wholesale and Distributor columns

The tier name sits in a one-cell table called PriceTier, on its own sheet (Example 3 Tier). Right now it says Wholesale.

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

let
Source = Excel.CurrentWorkbook(){[Name="Products"]}[Content],
Tier = Excel.CurrentWorkbook(){[Name="PriceTier"]}[Content]{0}[Price Tier],
#"Added Total" = Table.AddColumn(Source,"Order Total",each [Qty]*Record.Field(_,Tier),type number)
in
#"Added Total"

Result: A new Order Total column with the quantity times the Wholesale price (190, 144, 288, 192, 180, and 220)

A new Order Total column with quantity times the Wholesale price: 190, 144, 288, 192, 180, 220

Here, the Tier step reads the text from the first row of the PriceTier table, which is “Wholesale”. Then the Table.AddColumn function adds the Order Total column, working out its value one row at a time.

The each keyword runs the expression after it once for every row. It’s the same shorthand you may have seen with the Table.SelectRows function.

Inside each, the underscore (_) stands for the current row, which is a record. So Record.Field(_,Tier) returns that row’s Wholesale price, and [Qty]* multiplies it by the quantity.

Change the cell to Retail or Distributor and refresh the query, and the totals switch to those prices. You never have to edit the M code.

Important: The text in the PriceTier cell has to match a column name exactly, including capital letters. If someone types “wholesale” or adds a trailing space, every row of the Order Total column shows an error.

Example 4: Compare the Latest Month with the Month Before

Here’s a problem that comes up with monthly reports. Below is a website visits report with one column per month, and I want a Change vs Previous Month column that shows how much each page grew in the newest month.

Page visits table with one column per month from Jul to Oct

The catch is that a new month column gets added every month. If you hard-code [Oct]-[Sep], the query keeps comparing October with September long after November shows up.

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

let
Source = Excel.CurrentWorkbook(){[Name="PageVisits"]}[Content],
Cols = Table.ColumnNames(Source),
LatestMonth = List.Last(Cols),
PrevMonth = Cols{List.Count(Cols)-2},
#"Added Change" = Table.AddColumn(Source,"Change vs Previous Month",each Record.Field(_,LatestMonth)-Record.Field(_,PrevMonth),type number)
in
#"Added Change"

Result: A new Change vs Previous Month column (530, 135, 185, 45, -20, and 45)

A new Change vs Previous Month column: 530, 135, 185, 45, -20, 45

Here, the Table.ColumnNames function gives a list of the five column names (Page, Jul, Aug, Sep, and Oct), and the List.Last function picks the last one, “Oct”.

List positions start at 0, so these five names sit at positions 0 to 4. List.Count(Cols)-2 is 5 minus 2, which is 3, and position 3 holds “Sep”.

Record.Field then reads both values from each row and subtracts them. For the Home page, that’s 4910 minus 4380, which gives 530.

When you add a Nov column at the end of the table and refresh, the query compares November with October on its own.

This only works while the two newest months are the last two columns. If you add something like a Total column after them, the query picks the wrong columns.

Follow along with the example file

Record.Field Function Examples.xlsx

Download

Example 5: Pick a Different Column for Each Row

This next one is something plain square brackets can’t do at all. Below is a list of office supplies with quotes from three suppliers, plus the supplier I picked for each item, and I want the chosen supplier’s price in a Price column.

Supplier quotes table with a Chosen Supplier column and Northline, Bayside and Crestway price columns

The column to read changes from row to row. For Printer Paper it’s Bayside, and for Toner Cartridge it’s Northline.

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

let
Source = Excel.CurrentWorkbook(){[Name="SupplierQuotes"]}[Content],
#"Added Price" = Table.AddColumn(Source,"Price",each Record.Field(_,[Chosen Supplier]),type number)
in
#"Added Price"

Result: A new Price column with the chosen supplier’s price for each item (39, 68, 12, 16, 24, and 11)

A new Price column with the chosen supplier's price: 39, 68, 12, 16, 24, 11

Here, [Chosen Supplier] gives the supplier name in the current row, such as “Bayside”. Record.Field uses that name as the field to read, so it returns the Bayside price for that row.

There’s no way to do this with plain square brackets, because the column name you’d type inside them has to be fixed. Record.Field lets the data itself decide which column to read.

The supplier name still has to match a column name exactly. If a row says “bayside” or a supplier with no column, that row shows an error, and Example 7 shows how to handle it.

Example 6: Use a Record as a Rate Card

A record also works nicely as a small lookup list. Below is a list of orders with the shipping method each customer chose, and I want to add a Shipping Cost column.

Orders table with a Customer and a Shipping Method column

The shipping rates are $5 for Standard, $12 for Express, and $25 for Overnight. Instead of keeping them in a separate table, you can store them as a record right inside the query.

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

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
Rates = [Standard=5,Express=12,Overnight=25],
#"Added Cost" = Table.AddColumn(Source,"Shipping Cost",each Record.Field(Rates,[Shipping Method]),type number)
in
#"Added Cost"

Result: A new Shipping Cost column (12, 5, 25, 5, 12, and 5)

A new Shipping Cost column: 12, 5, 25, 5, 12, 5

Here, the Rates step is a record with one field per shipping method. For each order, Record.Field looks up the field whose name matches the Shipping Method, so Express returns 12.

This works well for a short list of rates that rarely changes. If the rates live in a table in your workbook or change often, merging the two tables is the better option, because then nobody has to edit the M code.

Example 7: Handle a Field That Doesn’t Exist

So far every name we asked for existed in the record. Below is a list of drivers with their vehicle type, and I want to add a Daily Fee column, but one driver has a Truck and there’s no rate for trucks.

Parking passes table with a Driver and a Vehicle Type column, including one Truck

If you use Record.Field here, the Truck row shows Error instead of a fee. Click it and you’ll see this message: “Expression.Error: The field ‘Truck’ of the record wasn’t found.”

The same error shows up when the case doesn’t match, because field names are case-sensitive. Asking for “car” when the field is “Car” fails too.

When a missing name is possible, use the Record.FieldOrDefault function instead. It works just like Record.Field, but returns null (or a default value you give it) when the field isn’t there.

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

let
Source = Excel.CurrentWorkbook(){[Name="ParkingPasses"]}[Content],
Rates = [Car=12,Motorcycle=5,Van=18],
#"Added Fee" = Table.AddColumn(Source,"Daily Fee",each Record.FieldOrDefault(Rates,[Vehicle Type]),type number)
in
#"Added Fee"

Result: A new Daily Fee column (12, 5, 18, null, 12, and 5)

A new Daily Fee column: 12, 5, 18, null for the Truck, 12, 5

Here, Car, Motorcycle, and Van all have a rate, so they get 12, 5, and 18. Truck isn’t in the Rates record, so that row gets null instead of an error, and the rest of the query keeps working.

If you’d rather show a number, add it as the third argument. For example, Record.FieldOrDefault(Rates,[Vehicle Type],0) returns 0 for the Truck row.

Other Excel Articles You May Also Like:

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.