Smart Systems for Home

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

Power Query List.PositionOf Function Guide and Usage

· 5 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 List.PositionOf function in Power Query (M language) returns the zero-based index of the first occurrence of a specific value within a list. To use it, you provide the list you are searching and the value you are looking for: List.PositionOf(yourList, yourValue). If the value is found, the function returns an integer (0 for the first item, 1 for the second, and so on); if the value is not found, it returns -1.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • The Power Query Editor open (accessible via Data > Get Data or Transform Data).
  • A dataset that can be converted into a List (such as a single column from a table).
  • Basic familiarity with the formula bar or the Advanced Editor.

Step-by-Step

  1. Prepare Your List: Power Query functions like List.PositionOf require a List data type, not a Table . If your data is currently in a table, you must reference the specific column. For example, if your table is named "SalesTable" and your column is "Product", the list reference is .
  2. Open the Formula Bar: Ensure the formula bar is visible. If not, go to the View tab and check the Formula Bar box. This allows you to manually enter the M code instead of relying solely on the ribbon buttons.
  3. Create a Custom Column: To find the position of a value relative to a list in every row, go to the Add Column tab and select Custom Column.
  4. Enter the Basic Syntax: In the Custom Column dialog, enter the function using the following structure: List.PositionOf([ColumnName], "TargetValue"). Replace [ColumnName] with the list you are searching and "TargetValue" with the specific item you want to locate.
  5. Handle Dynamic Values: If you want to find the position of a value that exists in another column of the same row, replace the static "TargetValue" with the column reference: List.PositionOf(SomeOtherList, [CurrentRowValue]).
  6. Apply Occurrences Parameter (Optional): By default, the function finds the first occurrence. If you need the last occurrence, add the optional Occurrence.Last argument: List.PositionOf(yourList, yourValue, Occurrence.Last).
  7. Verify the Result: Click OK. You will now see a new column containing numbers. A 0 indicates the item was the first in the list, and a -1 indicates the item was not present in the list at all.

Common Mistakes

One of the most frequent errors is attempting to pass a Table into the function instead of a List. In Power Query, a table is a collection of rows and columns, whereas a list is a single-column sequence of values. If you write List.PositionOf(Source, "Value") where "Source" is the previous step (a table), you will receive a type mismatch error. You must specify the column, such as List.PositionOf(Source[ColumnName], "Value").

Another common mistake is ignoring case sensitivity. Power Query is case-sensitive. If your list contains "Apple" and you search for "apple", List.PositionOf will return -1 because the strings do not match exactly. To fix this, you may need to transform the list to lowercase using List.Transform before searching.

Users also frequently forget that the index is zero-based. If you are using the resulting position to reference a human-readable row number (like in an Excel sheet), you must add 1 to the result of the function to align with standard row numbering.

Tips

To make your search case-insensitive, wrap your list and your search term in Text.Lower. For example: List.PositionOf(List.Transform(yourList, each Text.Lower(_)), Text.Lower("TargetValue")). This ensures that "APPLE", "Apple", and "apple" are all treated as the same value.

If you need to find the position of a value based on a complex condition (rather than an exact match), List.PositionOf may be too limited. In those cases, consider using List.PositionOfAny if you have a list of multiple possible targets, or use List.Select combined with List.FirstN to isolate the index of an item that meets a specific logical criteria.

When working with very large lists, be mindful of performance. Calling List.PositionOf inside a Custom Column for every single row of a table creates an O(n*m) complexity, which can significantly slow down the refresh time of your report. If possible, perform the lookup once in a separate step or use a Merge operation to bring in the index from a sorted table.

Troubleshooting

If the function returns -1 when you are certain the value exists, first check for leading or trailing spaces in your data. A value like "Apple " (with a space) will not match "Apple". Use the Transform > Trim function on your column before converting it to a list to remove these invisible characters.

If you encounter the error "We cannot convert the value X to type List", check your syntax. This usually happens when you have accidentally referenced a single cell or a table instead of a column. Ensure your reference looks like StepName[ColumnName].

If the function is returning the index of the first match but you need the second or third, remember that List.PositionOf only supports the first or last occurrence. To find the Nth occurrence, you would need to use List.Select to filter the list for all matches, and then use List.PositionOf on the original list using the specific value found at the Nth index of the filtered list.

Frequently Asked Questions

Is List.PositionOf case-sensitive?

Yes, it is strictly case-sensitive. "Value" and "value" are treated as different items. You must normalize the case of both the list and the search term to avoid this.

What does a result of -1 mean?

A result of -1 indicates that the specified value was not found anywhere within the provided list.

How do I find the last occurrence of a value instead of the first?

You can use the optional third argument of the function. By adding Occurrence.Last as the third parameter, Power Query will search from the end of the list backward.

Can I use this function to find a value in a Table?

Not directly. You must first specify which column of the table you want to search by using the bracket notation: TableName[ColumnName].

Does this function work with numbers and dates?

Yes, List.PositionOf works with any data type, including integers, decimals, dates, and booleans, provided the value you are searching for matches the data type of the list items.

How do I convert the 0-based index to a 1-based index?

Simply add 1 to the result of the function. For example: List.PositionOf(yourList, yourValue) + 1.