Table.AddIndexColumn
Numbers the rows in a new column: from 0 by ones, or from a start and by a step you choose.
Group: Table. Power Query M has it too: a query that uses it is written as in Power BI.
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 table
| Parameter | Type | Description |
|---|---|---|
table | table | The table to number. |
newColumnName | text | The name of the new column. |
initialValue | nullable number optional | The first row's number; 0 when left out. |
increment | nullable number optional | What is added from one row to the next; 1 when left out. |
columnType | nullable type optional | The new column's type, such as Int64.Type. |
Returns
table. The table with the numbers as its last column.
Remarks
- The numbers follow the rows' order at this step: sort first when the order matters.
Examples
Line numbers from 1
let
Lines = #table({"Product", "Price", "Units"}, {{"Chair", 50, 4}, {"Desk", 120, 1}, {"Lamp", 30, 6}})
in
Table.AddIndexColumn(Lines, "Line", 1, 1, Int64.Type)
| Product | Price | Units | Line |
|---|---|---|---|
| Chair | 50 | 4 | 1 |
| Desk | 120 | 1 | 2 |
| Lamp | 30 | 6 | 3 |
From 10, by tens
let
Lines = #table({"Product", "Price", "Units"}, {{"Chair", 50, 4}, {"Desk", 120, 1}, {"Lamp", 30, 6}})
in
Table.AddIndexColumn(Lines, "Position", 10, 10)
| Product | Price | Units | Position |
|---|---|---|---|
| Chair | 50 | 4 | 10 |
| Desk | 120 | 1 | 20 |
| Lamp | 30 | 6 | 30 |
Left as it is: from 0
let
Lines = #table({"Product", "Price", "Units"}, {{"Chair", 50, 4}, {"Desk", 120, 1}, {"Lamp", 30, 6}})
in
Table.AddIndexColumn(Lines, "Index")
| Product | Price | Units | Index |
|---|---|---|---|
| Chair | 50 | 4 | 0 |
| Desk | 120 | 1 | 1 |
| Lamp | 30 | 6 | 2 |
Related functions
Table.AddColumn, Table.AddRankColumn, Table.Sort, List.Numbers.