List.PositionOf 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 List.PositionOf function in Power Query tells you where a value sits in a list. You give it a list and a value, and it returns the position of that value.

It comes in handy more often than you’d expect. You can use it to turn month names into fiscal period numbers, sort data in your own custom order, or find the last time something happened in a log.

Follow along with the example file

List.PositionOf Function Examples.xlsx

Download

List.PositionOf Function Syntax

List.PositionOf(list as list, value as any, optional occurrence as nullable number, optional equationCriteria as any) as any
  • list – the list you want to search. It can be a list you type in curly brackets, like {"Bronze","Silver","Gold"}, or a column from a table that your query has loaded (Example 5 onwards shows how).
  • value – the value you want to find in the list.
  • occurrence (optional) – which match to report when the value appears more than once. Use Occurrence.First (the default), Occurrence.Last, or Occurrence.All to get a list of every position.
  • equationCriteria (optional) – controls how values are compared. You can use a comparer like Comparer.OrdinalIgnoreCase to ignore case, or your own function to define what counts as a match.

What it returns: The position of the value as a number, counting from 0. If the value isn’t in the list, you get -1.

With Occurrence.All, you get a list of positions instead.

The List.PositionOf function is part of the core Power Query M library, so it works wherever Power Query does, including Excel for Microsoft 365, 2024, 2021, 2019, and 2016 (on Windows for 2019 and 2016).

When to Use the List.PositionOf Function

Use this function when you need to:

  • Find where an item sits in a list, such as which tier a customer is in
  • Convert names to numbers based on their place in a list, like month names to fiscal period numbers
  • Sort a table in a custom order (High, Medium, Low) instead of A to Z
  • Find the first, last, or every position where a value appears
  • Find the first value that meets a condition, such as the first month sales crossed a target
  • Get the next item after a value in a sequence, like the next stage in an approval process

Example 1: Find the Position of a Value in a List

Let’s start with the basics. Suppose you have a list of membership tiers, Bronze, Silver, Gold, and Platinum, and you want to know where Gold sits in that list.

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:

= List.PositionOf({"Bronze","Silver","Gold","Platinum"},"Gold")

Result: 2

List.PositionOf returns 2 for Gold, because positions start at 0

Gold is the third item in the list, but the result is 2. That’s because Power Query counts positions from 0, so Bronze is at position 0, Silver at 1, and Gold at 2.

So if you want the “normal” position that starts at 1, just add 1 to the result. You’ll see this in Example 3.

Examples 1 and 2 type their list right into the query, so they don’t need a table on a worksheet. In the example file, you can open them from Data > Queries & Connections.

Important: List.PositionOf only searches lists, so List.PositionOf("Power Query","Q") gives the error “We cannot convert the value “Power Query” to type List.” To find the position of a character inside a text value, use the Text.PositionOf function instead.

Example 2: What Happens When the Value Isn’t in the List

Now let’s look for something that isn’t there. Using the same list of tiers, suppose you search for Diamond.

Create a new blank query and use this formula:

= List.PositionOf({"Bronze","Silver","Gold","Platinum"},"Diamond")

Result: -1

List.PositionOf returns -1 because Diamond is not in the list

Here, Diamond isn’t in the list, so List.PositionOf returns -1 instead of an error. That makes it easy to check, because you can test for -1 in an if statement before using the position.

If you only need a yes or no answer about whether a value is in the list, the List.Contains function is simpler. It returns TRUE or FALSE directly.

Example 3: Convert Month Names to Fiscal Period Numbers

Here’s a practical one. Below is a monthly sales table with month names, and I want to add a Fiscal Period column for a fiscal year that starts in April (so Apr is period 1 and Mar is period 12).

MonthlySales table with Month and Sales columns

The sales data is an Excel Table named MonthlySales. The code below reads it by that name, so for your own data, change “MonthlySales” to the name of your table.

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

let
Source = Excel.CurrentWorkbook(){[Name="MonthlySales"]}[Content],
FiscalMonths = {"Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan","Feb","Mar"},
#"Added Fiscal Period" = Table.AddColumn(Source,"Fiscal Period",each List.PositionOf(FiscalMonths,[Month])+1,Int64.Type)
in
#"Added Fiscal Period"

Result: A new Fiscal Period column with 10, 12, 1, 3, 6, and 9 for Jan, Mar, Apr, Jun, Sep, and Dec

A Fiscal Period column with 10, 12, 1, 3, 6 and 9 for a fiscal year starting in April

Here, the FiscalMonths step is a list of the 12 months in fiscal-year order, starting with Apr. The Table.AddColumn function then adds a new column, and the each keyword runs the formula once for every row.

For each row, List.PositionOf finds that row’s month in the FiscalMonths list. Jan is at position 9, and adding 1 turns it into period 10.

The Int64.Type part sets the new column’s data type to whole number.

This works for any fiscal year. If yours starts in July, just start the FiscalMonths list with “Jul” and end it with “Jun”.

The month names have to match the list exactly. A misspelled or blank month gets -1 from List.PositionOf, so its Fiscal Period shows 0, which makes it easy to spot.

Example 4: Sort Data in a Custom Order

This next one comes up a lot. Below is a support ticket list with a Priority column, and I want to sort the tickets so High comes first, then Medium, then Low.

Tickets table with Ticket ID, Requester and Priority columns

A normal A to Z sort won’t help here, because it would put High, Low, and Medium in alphabetical order. The ticket list is an Excel Table named Tickets (use your own table’s name in the code).

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

let
Source = Excel.CurrentWorkbook(){[Name="Tickets"]}[Content],
PriorityOrder = {"High","Medium","Low"},
#"Sorted Rows" = Table.Sort(Source,{
{each List.PositionOf(PriorityOrder,[Priority]),Order.Ascending},
{"Ticket ID",Order.Ascending}})
in
#"Sorted Rows"

Result: The tickets sorted as High (TKT-402, TKT-405), then Medium (TKT-403, TKT-406), then Low (TKT-401, TKT-404)

Tickets sorted High, then Medium, then Low using List.PositionOf

Here, the PriorityOrder step lists the priorities in the order you want. For each row, List.PositionOf gives the position of that row’s priority in this list, so High becomes 0, Medium 1, and Low 2.

The Table.Sort function then sorts the rows by that number, from smallest to largest. The second sort rule, {"Ticket ID",Order.Ascending}, keeps tickets with the same priority in Ticket ID order.

Important: A priority that isn’t in the PriorityOrder list (say, “Urgent”) gets -1, so it sorts above everything else. If your data might have other values, add them to the list in the order you want.

Example 5: Find the Last Occurrence of a Value

So far, every search has stopped at the first match. Below is a machine service log in the order the jobs were done, and I want to find which technician last serviced machine M-02.

ServiceLog table with Job ID, Machine and Technician columns

The service log is an Excel Table named ServiceLog (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ServiceLog"]}[Content],
LastVisit = List.PositionOf(Source[Machine],"M-02",Occurrence.Last),
Technician = Source[Technician]{LastVisit}
in
Technician

Result: Curtis Campbell

The technician who last serviced machine M-02 is Curtis Campbell

Here, Source[Machine] gives the Machine column as a list. M-02 appears three times, and Occurrence.Last tells List.PositionOf to return the position of the last one, which is 5 (job JOB-106).

The Technician step then uses that number to pick the matching item from the Technician column. Source[Technician]{5} means “the item at position 5”, which is Curtis Campbell.

Without Occurrence.Last, you’d get the first match (position 1), and the result would be Peter Williams. That’s the technician who serviced M-02 first, not last.

If you only need the name, you could also filter the table to M-02 with the Table.SelectRows function and take the last row. Occurrence.Last finds the last match directly, and it also gives you the position itself.

Important: If the machine isn’t in the log, List.PositionOf returns -1, and Source[Technician]{-1} gives the error “The index cannot be negative.” Check for -1 first if the value might be missing.

Follow along with the example file

List.PositionOf Function Examples.xlsx

Download

Example 6: Find All Positions of a Value

Sometimes you need every match, not just one. Below is an order list with a Status column, and I want the positions of all the orders that were returned.

Orders table with Order ID, Customer and Status columns

The order list is an Excel Table named Orders (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
ReturnedRows = List.PositionOf(Source[Status],"Returned",Occurrence.All)
in
ReturnedRows

Result: 1, 4, 5

Occurrence.All returns the positions 1, 4 and 5 for the Returned orders

Here, Occurrence.All makes List.PositionOf return a list with every position where “Returned” appears. Counting from 0, those are the 2nd, 5th, and 6th orders (ORD-6102, ORD-6105, and ORD-6106).

If nothing matches, you get an empty list instead of -1. You can also wrap the formula in the List.Count function to get the number of returned orders, which is 3 here.

And if you want the returned orders themselves rather than their positions, the Table.SelectRows function is the better tool for that.

Example 7: Find a Value Without Worrying About Upper or Lower Case

This one catches a lot of people out. Below is an inventory list where the SKU codes were typed by hand, so some are in lowercase, and I want to find the position of SKU KB-200.

Inventory table with SKU codes in mixed upper and lower case

List.PositionOf is case-sensitive by default. Since the list has “kb-200” in lowercase, a plain List.PositionOf(Source[SKU],"KB-200") returns -1, even though the SKU is right there.

The inventory list is an Excel Table named Inventory (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Inventory"]}[Content],
Position = List.PositionOf(Source[SKU],"KB-200",Occurrence.First,Comparer.OrdinalIgnoreCase)
in
Position

Result: 1

Comparer.OrdinalIgnoreCase finds KB-200 at position 1

Here, Comparer.OrdinalIgnoreCase is the fourth argument, and it tells List.PositionOf to ignore upper and lower case when comparing. So “KB-200” matches “kb-200”, which is at position 1.

To use the fourth argument, you have to fill in the third one before it. That’s why the formula includes Occurrence.First, even though it’s the default.

Example 8: Find the First Value That Meets a Condition

Now let’s use your own matching rule. Below is a table of cumulative sales by month, and I want to find the first month in which cumulative sales reached the 100,000 target.

SalesProgress table with Month and Cumulative Sales columns

The sales data is an Excel Table named SalesProgress (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SalesProgress"]}[Content],
FirstHit = List.PositionOf(Source[Cumulative Sales],100000,Occurrence.First,(x,y)=>x>=y),
MonthReached = Source[Month]{FirstHit}
in
MonthReached

Result: May

May is the first month where cumulative sales reach 100,000

Here, the fourth argument is a small function, (x,y)=>x>=y, that replaces the normal “is equal to” check. It compares each value in the list (x) with the value you’re searching for (y, which is 100,000 here).

So List.PositionOf returns the first position where the cumulative sales are greater than or equal to 100,000. That’s May, at position 4, and the MonthReached step picks May from the Month column.

You could also get the same month by filtering the table with the Table.SelectRows function and taking the first row. Using List.PositionOf here gives you the position in a single step, which you can reuse in later steps.

Important: In the matching function, x is the list item and y is the value you search for, so don’t swap them around. If no month reaches the target, FirstHit is -1 and the next step gives the error “The index cannot be negative.”

Example 9: Get the Next Item in a Sequence

This last one uses the position to look one step ahead. Below is a list of documents with the approval stage each one is at, and I want to add a Next Stage column that shows where each document goes next.

Documents table with Document, Owner and Stage columns

The document list is an Excel Table named Documents (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Documents"]}[Content],
Stages = {"Draft","Review","Approved","Published"},
#"Added Next Stage" = Table.AddColumn(Source,"Next Stage",each
let Pos = List.PositionOf(Stages,[Stage])
in if Pos = -1 then null else Stages{Pos+1}?,type text)
in
#"Added Next Stage"

Result: A new Next Stage column with Review, Approved, Published, null, and Review

A Next Stage column with the next approval stage, and null for Published

Here, the Stages step lists the approval stages in order. For each row, the let part stores the position of the current stage in a variable named Pos, and Stages{Pos+1} picks the stage at the next position.

So for Review (position 1), you get the stage at position 2, which is Approved.

The ? at the end handles the last stage. Published is at position 3, and there’s no position 4, so without the ? you’d get an error; with it, you get null.

The if Pos = -1 check handles a stage that isn’t in the list, like “Archived” or a blank cell. Without it, -1 plus 1 would be 0, and the row would quietly get Draft as its next stage instead of null.

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.