Table.AddIndexColumn
Table function · By Tahir Nas · Examples run in our simulator · Published · Updated
Adds a running number to each row.
When to use it
Use it to number rows, for example as a line number, a stable sort key or a way to compare a row with the previous one. Sort first: the index follows the current order.
Syntax
Table.AddIndexColumn(table as table, newColumnName as text, optional initialValue as nullable number, optional increment as nullable number, optional columnType as nullable type) as tableParameters
- initialValue
- Starting value (default 0).
Returns
The table with an index column.
Example
Table.AddIndexColumn(#table({"X"}, {{"a"}, {"b"}}), "Line", 1, 1)Result
| X | Line |
|---|---|
| a | 1 |
| b | 2 |
Example: compare each row with the previous
An index starting at 0 lets you look up the row above with {[Index] - 1}.
let T = Table.AddIndexColumn(#table({"Value"}, {{10}, {13}, {12}}), "i", 0, 1) in Table.AddColumn(T, "Change", each if [i] = 0 then null else [Value] - T{[i] - 1}[Value])Result
| Value | i | Change |
|---|---|---|
| 10 | 0 | null |
| 13 | 1 | 3 |
| 12 | 2 | -1 |
Both results are produced by the Power Query Exercises simulator, an independent implementation of a documented subset of M, and checked on every build.
Good to know
- Numbers follow the current row order: sort first.
Common mistakes
- Adding the index before the final sort.
- Treating the index as a permanent ID: it changes when rows change.
The matching command in Excel
- Excel for Windows
- Add Column > Index Column
- What you see
- The default index starts at 0; Custom sets the start and increment.
Paths are for the Power Query editor in Excel for Microsoft 365, checked against Microsoft's documentation on 8 November 2026: Add an index column (Microsoft Learn).
Exercises that use Table.AddIndexColumn
- beginner~5 minPro
Number claims in date order (consultancy)
Sort claims by date and add a Line number starting at 1.
- advanced~12 minPro
Rank each employee's claims by amount (consultancy)
Within each employee, number claims from largest to smallest Amount.
- advanced~12 minPro
Add a running total in date order (consultancy)
Sort claims by date, then add a Running Total of Amount from the first claim to each row.
- advanced~14 minPro
Interview task: month-over-month revenue change (kite shop)
Total revenue per month, then show the change from the previous month (null for the first month), in month order.
Learn more
- Add index columns lesson
Related functions
Official documentation: Table.AddIndexColumn on Microsoft Learn.