Power Query Table.AddIndexColumn: How to Add a Row Number
In this article
The Table.AddIndexColumn function in Power Query adds a sequential number to each row in a table, starting from 1 or a custom number. This is essential for creating unique identifiers, ordering data, or preparing datasets for joins or analysis. No setup is required beyond having a table loaded into Power Query Editor, which you can do by importing data from Excel, CSV, databases, or other sources.
What You'll Need
- A table in Power Query Editor (loaded from Excel, CSV, database, etc.)
- Microsoft Excel with Power Query enabled (2016 or later) or Microsoft Power BI Desktop
- Basic familiarity with the Power Query Editor interface
Step-by-Step
- Open your data source in Excel or Power BI. In Excel, select your data range and go to Data > From Table/Range. In Power BI, go to Home > Get Data and choose your source. Load the data into the Power Query Editor.
- Once the table appears in the Power Query Editor, locate the Add Column tab on the ribbon at the top of the window.
- Click on the Add Column button, then select Index Column from the dropdown menu. This inserts a new column named "Index" with row numbers starting at 0.
- To start numbering from 1 instead of 0, click on the "Index Column" dropdown again and choose "From 1".
- If you need a custom starting number or increment (e.g., 100, 200, 300), go to the Add Column tab, click on "Custom Column".
- In the Custom Column dialog box, enter a name like "RowNumber" in the New column name field.
- In the Custom column formula field, type:
Table.AddIndexColumn(#"PreviousStepName", "RowNumber", 100, 100). Replace "PreviousStepName" with the exact name of your last step from the Applied Steps pane on the right. For example, if your last step is named "Changed Type", useTable.AddIndexColumn(#"Changed Type", "RowNumber", 100, 100). - Click OK. A new column will appear with your custom starting value and increment.
- To rename the index column, click the column header, type a new name like "ID" or "Sequence", and press Enter.
- Click Close & Load (Excel) or Close & Apply (Power BI) to return the updated table to your worksheet or report.
Common Mistakes
- Using the wrong step name in the custom formula. If you rename a step after writing the formula, the formula breaks. Always use the exact name shown in the Applied Steps pane — case-sensitive and including spaces.
- Forgetting to use the # symbol before the step name. The correct syntax is
#"StepName", not"StepName"or#StepName. - Adding multiple index columns without renaming them, causing confusion. Always rename index columns to reflect their purpose (e.g., "RowID" vs "SortOrder").
- Assuming the index is stable across refreshes. Index columns reset to sequential numbers every time the query refreshes. If you need persistent IDs, combine this with a unique key from your source data.
- Trying to use Table.AddIndexColumn on a non-table source. The function only works on tables. If your data is a list, convert it to a table first using Table.FromList.
Tips
- Use Table.AddIndexColumn with Table.Sort to create a stable sort order when your source data lacks a natural sequence. Sort by a date or name column first, then add the index to preserve that order.
- When joining tables, use the index column to match rows if no other unique key exists — but only if the order is guaranteed to remain consistent after refresh.
- For debugging, right-click the index column and select "Column from Example" to see how the formula was generated. This helps you understand the M language structure.
- Always check the Applied Steps pane after adding an index. If you see multiple Index columns, delete the ones you don’t need to avoid confusion.
- Use the index column to create dynamic headers or labels in reports. For example, you can concatenate "Record " & Text.From([Index]) to create "Record 1", "Record 2", etc.
- If you're working with large datasets, be aware that adding an index column slightly increases memory usage. It’s negligible for most cases, but avoid unnecessary index columns in production queries.
Troubleshooting
- Error: "The name 'StepName' was not recognized." This means the step name in your formula doesn’t match the exact name in the Applied Steps pane. Click each step to confirm the name, including spaces and capitalization. Copy and paste the step name directly from the pane into your formula.
- The index column shows #Error or blank. This often happens if the prior step returned an error (like a failed transformation). Go back through each step in the Applied Steps pane and fix any red error markers before adding the index.
- Index resets after refreshing data. This is normal behavior. Table.AddIndexColumn generates numbers based on the current row order after all transformations. If your source data changes order, the index changes. To create a persistent ID, combine the index with a unique field from your original data (e.g., [ID] & "-" & Text.From([Index])).
- Cannot find "Index Column" in the Add Column menu. Make sure you’re in the Power Query Editor. If you’re in Excel’s main interface, you won’t see Power Query options. Press Alt + A + M to reopen the editor if you closed it.
- Formula syntax error after editing in Advanced Editor. Always use the exact syntax: Table.AddIndexColumn(source, newColumnName, startIndex, stepValue). All parameters must be present. Missing commas or quotes cause syntax errors.
Frequently Asked Questions
Can I add an index column without using the GUI?
Yes. Open the Advanced Editor (View > Advanced Editor), find your last step, and wrap it with Table.AddIndexColumn. For example:Table.AddIndexColumn(#"Changed Type", "ID", 1, 1).