Skip to main content

Power Query: Create row number for each group in Excel

Author Xiaoyang Last modified

If you've ever needed to create sequential row numbers for each group of data in Excel, you know that while a formula can solve the task, there is a more efficient way using Power Query. In this article, we'll show you how to use Power Query to easily add row numbers within each group, streamlining your workflow and saving time when working with grouped data in Excel.

A screenshot showing data with groupings, with a column for row numbers for each product


Power Query: Create row number for each group in Excel

To generate the index numbers for each group of the data, the Power Query can do you a favor. Please do with the following steps:

1. Select the data range that you want to insert the row number. In Excel 365, click "Data" > "From Table/Range", see screenshot:

A screenshot showing the 'From Table/Range' option in Excel 365

Note: In Excel 2016 and newer versions, click "Data" > "From Table", see screenshot:

A screenshot showing the 'From Table' option in Excel 2016 and newer versions

2. In the "Create Table" dialog box, click "OK" button to create a table, see screenshot:

A screenshot of the 'Create Table' dialog box in Excel

3. And then, in the opened "Power Query Editor" window, select the column that you want to use to group the table, then right click it. And then, choose "Group By" from the context menu, see screenshot:

A screenshot showing the right-click context menu with the 'Group By' option in Power Query

4. In the "Group By" dialog box:

  • Select the column name that you want to insert the row number based on from the drop-down list;
  • Select" All Rows" option from the "Operation" drop-down list;
  • Click "OK" button.

A screenshot of the Group By dialog box in Power Query

5. Now, return to the "Power Query Editor" window, click "Add Column" > "Custom Column", see screenshot:

A screenshot of the Group By dialog box in Power QueryA screenshot showing the 'Add Column' and 'Custom Column' options in Power Query

6. In the "Custom Column" dialog box, using the "Table.AddIndexColumn" function as below screenshot show:

  • Type a new column name into the" New column name" text box;
  • Then, enter the following formula into the "Custom column formula" box;
    =Table.AddIndexColumn([Count],"Index",1)
  • At last, click "OK" button.

Note: In the above formula, "[Count]" is the name of the column you have created in step 4, please change it to your need.

A screenshot showing the Custom Column dialog box with the Table.AddIndexColumn formula

7. Now, return to the "Power Query Editor" window, you should remove all other columns and only keep the last column you have created. Right-click the name of the last column you created, select "Remove Other Columns", see screenshot:

A screenshot showing the context menu to remove other columns in Power Query

8. Then, expand this column, click the "Expand" icon next to the column title. In the expanded box, check "Expand" option, and then check all the column names from the list box, and uncheck the Use original column name as prefix option. And finally, click "OK" button. See screenshot:

A screenshot showing the expand icon in Power Query to expand grouped data

9. And now, you can see the row index numbers are displayed as below screenshot shown:

A screenshot showing the resulting data with row numbers created for each group in Power Query

10. Then, please click "Home" > "Close & Load" > "Close & Load" to load the data into a new worksheet, see screenshots:

 

A screenshot of the 'Close & Load' option in Power Query Arrow A screenshot of the final data loaded into a new worksheet in Excel

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!