Power Query List.Positions Function Guide and Usage
In this article
The List.Positions function in Power Query (M language) returns a list of all the index positions for every item contained within a specified list. Essentially, if you have a list of ten items, List.Positions will generate a new list containing the numbers 0 through 9. This is primarily used to create index columns for custom calculations, to facilitate the looping of lists via List.Transform, or to map specific values back to their original sequence in a dataset.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A dataset that contains a list, or a table column that you intend to convert into a list.
- Access to the Power Query Editor (via Data > Get Data or Transform Data).
- Basic familiarity with the Formula Bar in Power Query.
Step-by-Step
- Open the Power Query Editor: Launch Excel or Power BI, go to the Data tab, and select Get Data or Transform Data to enter the Power Query environment.
- Identify Your Source List: Ensure you have a list ready. If you have a table, you can reference a specific column as a list by using the syntax
TableName[ColumnName]in the formula bar. - Create a Custom Step: Click the
fxicon next to the formula bar. This creates a new step in the Applied Steps pane, allowing you to write a custom M function. - Apply the List.Positions Function: In the formula bar, type the function using the following syntax:
= List.Positions(YourListName). ReplaceYourListNamewith the name of the previous step or the specific list variable you are targeting. - Verify the Output: Press Enter. Power Query will now display a list of integers starting from 0. For example, if your source list had 50 items, you will now see a list from 0 to 49.
- Convert to Table (Optional): Since
List.Positionsreturns a List object, you may want to convert it back to a table to merge it with other data. Go to the Transform tab in the ribbon and selectTo Table. - Name the Column: Once converted to a table, double-click the column header (usually named Column1) and rename it to "Index" or "Position" for clarity.
Common Mistakes
One of the most frequent errors is attempting to use List.Positions directly on a Table object rather than a List object. In Power Query, a Table and a List are different data types. If you try to write List.Positions(SourceTable), the engine will return an error because it expects a list. To fix this, you must specify the column: List.Positions(SourceTable[ColumnName]).
Another common mistake is forgetting that Power Query uses zero-based indexing. Users coming from Excel formulas often expect the first item to be at position 1. However, List.Positions always starts at 0. If your business logic requires a 1-based index, you must add a custom transformation step to add 1 to every value in the resulting list.
Users also occasionally confuse List.Positions with List.Generate or List.Numbers. While List.Numbers creates a sequence of numbers based on a start and end point you define, List.Positions is dynamic; it looks at the actual size of an existing list and generates the indices based on that specific count. Using List.Numbers when you should use List.Positions can lead to errors if the source list size changes during a data refresh.
Tips
To make List.Positions truly powerful, combine it with List.Transform. For example, if you want to perform a calculation that depends on the position of a row (like a running total or a weighted average), you can transform the list of positions. Instead of transforming the values of the list, you transform the indices, and then use those indices to reference the original list values via List.ElementAt.
If you are working with very large datasets, be mindful of how many times you call List.Positions. While the function itself is efficient, converting the resulting list into a table and then merging it back into a large original table can create a performance bottleneck. In such cases, consider using the built-in "Add Index Column" feature from the Add Column tab, which achieves a similar result more natively within the table structure.
For those writing complex M code in the Advanced Editor, using List.Positions inside a let...in block allows you to create a stable reference for row numbers that won't shift if you apply filters to the data later in the script. By capturing the positions early, you lock in the original sequence of the data.
Troubleshooting
If you encounter an error stating that the function cannot convert a value of type Table to type List, check your syntax. Ensure you have included the square brackets after the table name to isolate the column. For example, use List.Positions(Source[MyColumn]) instead of List.Positions(Source).
If the list of positions is shorter or longer than expected, check for nulls or empty rows at the end of your source data. List.Positions counts every single entry in the list, including null values. If your source list has trailing empty rows that you didn't notice, the index list will be longer than your actual data rows.
If the Power Query editor becomes unresponsive after applying the function to a massive list (millions of rows), it is likely due to memory exhaustion during the "To Table" conversion. To resolve this, try to perform your calculations within the list format using List.Transform before converting the final result to a table, as this reduces the overhead of managing table metadata.
Frequently Asked Questions
Does List.Positions count null values?
Yes, List.Positions counts every element in the list regardless of its content. If a row is null, it still occupies a position and will be assigned an index number.
What is the difference between List.Positions and adding an Index Column?
Adding an Index Column is a UI-driven action that adds a new column to an existing table. List.Positions is a functional M command that returns a standalone list of numbers. The former is easier for table-based work, while the latter is better for functional programming and custom M scripts.
Can I start the positions at 1 instead of 0?
The function itself always starts at 0. To start at 1, you can wrap the function in List.Transform: List.Transform(List.Positions(MyList), each _ + 1).
Is List.Positions case-sensitive?
The function name List.Positions is case-sensitive because the M language is case-sensitive. You must capitalize the 'L' and the 'P' exactly as shown, or Power Query will not recognize the command.
Can I use this function to find the position of a specific value?
No, List.Positions returns the indices of all items. To find the position of one specific value, you should use List.PositionOf instead.
Does this function slow down my query refresh?
On its own, List.Positions is very fast. However, if you use the resulting list to perform repeated lookups in a large table using List.ElementAt, it can significantly slow down your refresh time due to the way Power Query handles list indexing.