Table.AddIndexColumn Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Table.AddIndexColumn function in Power Query adds a column of running numbers (0, 1, 2, and so on) to a table. You can pick the number it starts from and how much it goes up by on each row.

In this article, I’ll show you how to use it with practical examples, from simple row numbers to numbering rows within each group.

Follow along with the example file

Table.AddIndexColumn Function Examples.xlsx

Download

Table.AddIndexColumn Function 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
  • table – the table you want to add the index column to. This is usually the previous step, such as Source.
  • newColumnName – the name of the new column, as text in double quotes, such as "Index".
  • initialValue (optional) – the number the first row gets. If you leave it out, the index starts at 0.
  • increment (optional) – how much the number goes up on each row. If you leave it out, it goes up by 1. A negative number makes it count down.
  • columnType (optional) – the data type of the new column, such as Int64.Type for whole numbers. If you leave it out, the column gets the Decimal Number type (the 1.2 icon in its header).

What it returns – the same table with the new index column added at the end. The rows and the other columns don’t change.

The Table.AddIndexColumn function works in Power Query in Excel for Microsoft 365, 2024, 2021, 2019, 2016, and Excel for Mac (Microsoft 365), as well as in Power BI.

When to Use the Table.AddIndexColumn Function

Use this function when you need to:

  • Number the rows of a table, starting from 0, 1, or any other number
  • Create sequential IDs like 1001, 1002, 1003
  • Number rows in steps, like 10, 20, 30, or count down instead of up
  • Rank rows after sorting them
  • Restart the numbering for each group, like each customer or each driver
  • Compare a row with the row above it, like this week’s number with last week’s
  • Split a list into batches of a fixed size

Example 1: Add a Basic Index Column

Let’s start with the simplest version, where you only give the table and the column name.

Below is an RSVP list with each guest and their meal choice, and I want to add a column that numbers the rows.

RSVP list with Guest and Meal columns

To follow along, select any cell in the table and go to Data > From Table/Range to open it in the Power Query Editor. If Power Query adds a Changed Type step, use #"Changed Type" in place of Source in the formulas below.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddIndexColumn(Source,"Index")

Result: a new Index column with the numbers 0, 1, 2, 3, and 4

A new Index column numbers the guests 0 to 4

Here, the formula adds a column called Index at the end of the table. Since I didn’t give a starting number or an increment, it starts at 0 and goes up by 1 on each row.

The new column gets the Decimal Number type (the 1.2 icon in its header). This column only ever holds whole numbers, so it makes sense to set it to Whole Number, which Example 2 shows.

You can also add an index column without typing a formula. Go to Add Column > Index Column and pick From 0, From 1, or Custom.

The name you give has to be new. If the table already has a column called Index, you get an error that says “The field ‘Index’ already exists in the record,” so use a different name.

Example 2: Start the Index at 1

Starting at 0 isn’t what most people want when they number rows. Here’s how to start at 1 instead.

Below is a list of employees who signed up for a training session, and I want to add a Row No column that numbers them from 1.

Training sign-ups with Employee and Session columns

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddIndexColumn(Source,"Row No",1,1,Int64.Type)

Result: a new Row No column with the numbers 1 to 5

A new whole-number Row No column numbers the rows 1 to 5

Here, the third argument (1) is the starting number, and the fourth argument (1) is the increment. So the first row gets 1, the next gets 2, and so on.

The last argument, Int64.Type, makes it a Whole Number column (the 123 icon). I’ll use it in the rest of the examples, because an index that starts at a whole number and goes up by a whole number is always a whole number.

Example 3: Create IDs That Start from a Custom Number

The starting number can be anything, which makes this handy for creating ID numbers.

Below is a list of new products with their category, and I want to give each one a Product ID starting at 1001.

New products list with Product and Category columns

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddIndexColumn(Source,"Product ID",1001,1,Int64.Type)

Result: a new Product ID column with the numbers 1001 to 1005

A new Product ID column with 1001 to 1005

Here, the index starts at 1001 and goes up by 1, so the Desk Lamp gets 1001 and the Bookshelf gets 1005.

Important: The numbers are assigned again every time the query refreshes, based on the current row order. If a product is added, removed, moved, or filtered out before this step, other products can get a different ID, so don’t use these as permanent IDs.

Example 4: Number Rows in Steps of 10

The increment doesn’t have to be 1. You can make the numbers go up by any amount.

Below is a purchase order with the item and quantity, and I want to add line numbers that go 10, 20, 30, and so on.

Purchase order with Item and Qty columns

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddIndexColumn(Source,"Line No",10,10,Int64.Type)

Result: a new Line No column with the numbers 10, 20, 30, 40, and 50

A new Line No column going 10, 20, 30, 40, 50

Here, both the starting number and the increment are 10. So the first line is 10, and each line after it is 10 more than the one above.

Numbering in tens is common in purchase orders and invoices, because it leaves room to slot in a line later (like 15 between 10 and 20) without renumbering everything.

Example 5: Count Down Instead of Up

A negative increment makes the index count down. This is useful when the last row should get the number 1.

Below is a bake-off results list sorted from the lowest score to the highest, and I want to add a Place column where the top score gets 1st place.

Bake-off results sorted from lowest to highest score

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddIndexColumn(Source,"Place",Table.RowCount(Source),-1,Int64.Type)

Result: a new Place column with the numbers 5, 4, 3, 2, and 1

A new Place column counting down from 5 to 1

Here, the increment is -1, so each row gets one less than the row above it. The Table.RowCount function gives the number of rows in the table (5), which becomes the starting number.

So the first row (the lowest score) gets 5, and the last row (Natalie Palmer with 93) gets 1. I used Table.RowCount instead of typing 5, so the countdown still ends at 1 when a contestant is added or removed.

The places are right only because the list is already sorted from the lowest score to the highest. The index just counts rows, so if two contestants tie, they still get different places.

Follow along with the example file

Table.AddIndexColumn Function Examples.xlsx

Download

Example 6: Rank Rows After Sorting

Table.AddIndexColumn numbers the rows in whatever order they’re in. So if you sort the table first, the index becomes a rank.

Below is an Excel table named SalesReps with each rep’s revenue, and I want to rank them so the highest revenue gets rank 1.

Sales reps with their revenue, in no particular order

Go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor, open the Advanced Editor (Home > Advanced Editor) and use this code.

The Source line reads the table by its name, so if your table has a different name, change SalesReps to that name. The same goes for the table names in the later examples.

let
Source = Excel.CurrentWorkbook(){[Name="SalesReps"]}[Content],
#"Sorted Rows" = Table.Sort(Source,{{"Revenue",Order.Descending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows","Rank",1,1,Int64.Type)
in
#"Added Index"

Result: the reps sorted by revenue, with Shannon Cooper (72300) at rank 1 and Marilyn Warren (39900) at rank 6

Reps sorted by revenue with a Rank column from 1 to 6

Here, the Sorted Rows step uses the Table.Sort function to put the highest revenue at the top. The Added Index step then numbers the sorted rows from 1, so the numbers work as ranks.

The order of the steps matters. If you add the index before sorting, each rep keeps the number from their original position, and the Rank column ends up shuffled.

If two reps have the same revenue, they still get different ranks (like 3 and 4), because the index just counts rows. It doesn’t look at the values.

Example 7: Restart the Index for Each Group

This next one comes up a lot. Instead of one running number for the whole table, you want the numbering to start over for each group.

Below is a delivery schedule (an Excel table named DeliveryStops) with each driver and the customers they visit in order, and I want a Stop No column that starts at 1 for each driver.

Delivery stops with the driver and customer for each stop

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="DeliveryStops"]}[Content],
#"Grouped Rows" = Table.Group(Source,{"Driver"},
{{"Stops",each Table.AddIndexColumn(_,"Stop No",1,1,Int64.Type),type table}}),
#"Combined Stops" = Table.Combine(#"Grouped Rows"[Stops])
in
#"Combined Stops"

Result: a new Stop No column that goes 1 to 3 for Donald Webb, 1 to 2 for David Campbell, and 1 to 4 for Barbara Wright

A Stop No column that restarts at 1 for each driver

Here, the Table.Group function splits the rows into one small table for each driver. For each of those tables (the underscore _ stands for the table), Table.AddIndexColumn adds a Stop No column starting at 1.

The numbered tables go into a new column called Stops, one per driver. The Combined Stops step then takes that Stops column and uses the Table.Combine function to stack the tables back into one.

The preview shows the drivers in the order they first appear in the sample. Table.Group doesn’t guarantee the order of the groups, so sort the final table if you need a particular display order.

This works even when a driver’s rows aren’t next to each other, because Table.Group gathers all of them into that driver’s table.

Example 8: Compare Each Row with the Previous Row

Power Query doesn’t have a direct way to look at the row above, the way an Excel formula can. An index column gives you one.

Below is an Excel table named WeeklySubscribers with the subscriber count for each week, and I want a Change column that shows how much the count went up or down from the week before.

Weekly subscriber counts for six weeks

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="WeeklySubscribers"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source,"Index",0,1,Int64.Type),
Subs = List.Buffer(#"Added Index"[Subscribers]),
#"Added Change" = Table.AddColumn(#"Added Index","Change",
each if [Index] = 0 then null else [Subscribers] - Subs{[Index] - 1},Int64.Type)
in
#"Added Change"

Result: a new Change column with null, 150, -40, 270, 140, and -30

Index and Change columns, with null for week 1 and the week-over-week change after it

Here, the Added Index step numbers the rows from 0. The Subs step then takes the Subscribers column as a list, and the List.Buffer function keeps that list in memory, so Power Query doesn’t rebuild it for every row on bigger tables.

The index starts at 0 on purpose. Power Query counts list positions from 0, so a row’s index is also its position in the Subs list, and Subs{[Index] - 1} gets the count from the row above.

The Added Change step uses the Table.AddColumn function to subtract that count from the current row’s count.

Week 1 has no week before it, so the formula returns null for index 0. Without that check, it would ask for position -1 and give an error.

Example 9: Split Rows into Batches

You can also do math on the index to group rows into batches of a fixed size.

Below is an Excel table named Candidates with a list of job candidates, and I want to put them into interview batches of 3, with the batch number in a Batch column.

List of eight job candidates

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Candidates"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source,"Index",0,1,Int64.Type),
#"Added Batch" = Table.AddColumn(#"Added Index","Batch",
each Number.IntegerDivide([Index],3) + 1,Int64.Type),
#"Removed Index" = Table.RemoveColumns(#"Added Batch",{"Index"})
in
#"Removed Index"

Result: a new Batch column with 1 for the first three candidates, 2 for the next three, and 3 for the last two

A Batch column with 1, 1, 1, 2, 2, 2, 3, 3

Here, the Number.IntegerDivide function divides the index by 3 and drops the decimal part. Index 0, 1, and 2 give 0, index 3, 4, and 5 give 1, and so on, and adding 1 turns that into batch numbers starting at 1.

The index was only a helper here, so the last step removes it. To change the batch size, change the 3 in the formula.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.