Numbering Grouped Data in Power Query

Mynda Treacy

January 24, 2019

Numbering items within grouped data is easy with an Excel COUNTIF formula, but numbering grouped data in Power Query requires a few more steps. For example, taking the data below, in column C I’ve numbered the Sub Areas within each Area:

Numbering Grouped Data in Power Query

Notice they don’t all have the same number of sub areas.

Download the Workbook

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

Steps for Numbering Grouped Data in Power Query

Step 1:

I started by formatting my source data in an Excel Table, called ‘Table1’:

Table1

Step 2:

Load to Power Query. In Excel 2016 onward go to the Data tab > From Table/Range:

load to Power Query

In earlier versions of Excel go to the Power Query tab > From Table.

This will load the data to Power Query and launch the Power Query Editor window.

Step 3:

In the Power Query editor window Add an Index Column (Note: this step isn't strictly required in this scenario, I just added it out of habit)

add index column

Tip: the index can start at 0 or 1 as it makes no difference.

Step 4:

Group Rows

group rows

This will open the Group By dialog box:

group by

The data is now grouped into tables for each Area. Clicking in the white space beside one of the ‘Count’ column’s tables displays the underlying data in the preview pane at the bottom of the Power Query window, as you can see below:

click white space

Step 5:

Add a Custom Column to index each grouped table:

add a custom column

Use the Power Query Table.AddIndexColumn function to Index the Count column created in the previous Group By step:

index the Count column

In English the formula translates to:

= Table.AddIndexColumn([Count]. Call this new column "Sub Area No.", 1, 1)

Add an Index column to the individual tables in the [Count] column called ‘Sub Area No.’. starting the numbering at 1 and increment by 1

In the Power Query editor window, we now have a new column called ‘Custom’ that contains our individually indexed tables we just added:

individually indexed tables

If you click in the white space beside one of the Tables in the ‘Custom’ column you’ll see a preview showing the new column for Sub Area No.:

preview shows new column

Step 6:

Delete the Count column as we no longer need it. Just select the Count column header and press the Delete key:

delete the Count column

Step 7:

Expand the Tables in the Custom column

expand the tables in the Custom column

Notice that I deselected the ‘Area’ column as we already have this. And I’ve deselected ‘Use original column name as prefix’.

Now I have my tables expanded and the ‘Sub Area No.’ column contains index for each Area:

expand the tables in the Custom column

Step 8:

Delete the ‘Index’ column as we no longer need it.

Step 9:

Set the Data types for each column by clicking on the data type icon in the left of each column header:

set data type

Step 10:

Close & Load. Now you’re ready to close the query editor and load it to a Table in the Excel workbook, or the Data Model a.k.a. Power Pivot, or a PivotTable etc.

close & load

Numbering Grouped Data in Excel

If you don’t need your data numbered in Power Query you can achieve the same results with an Excel COUNTIF formula like so:

same results with an Excel COUNTIF formula

Pay close attention to the use of absolute and relative references:

=COUNTIF($A$5:A5,A5)

Thanks

Thanks to fellow Excel MVP, Ken Puls, for pioneering the Power Query technique here.

Please Share

If you liked this please share it with your friends and colleagues.

30 thoughts on “Numbering Grouped Data in Power Query”

  1. This is excellent material! I saw this method elsewhere at one point, but when I went looking for it again, I came across this and this is much better presented. Thanks!

    Reply
  2. So I did this for a field in my table called Locations. (I’m trying to count the number of open tickets for each corporate location and my main table contains ALL open tickets). How would I finish this off by doing that – example
    albany – 7
    Allentown – 6
    ect.
    Thanks in advance if you have time to answer. I appreciate it.

    Reply
    • Hi Randall, I’d use a PivotTable to do the count by location. If you’re stuck, please post your question on our Excel forum and you can upload a sample Excel file so we can help you further.

      Reply
  3. Hello,

    Much appreciate the step by step process. Just what I needed! To go one step further I had to find the maximum value within the “Subarea” column to thus do a filter. Table.max to the rescue! Could not have done it without you. Thanks indeed!

    Reply
  4. Thank you very much for this super-useful post. I often need to use such a grouping in my data. However, I had an issue where, after expanding the Custom column, data types I had previously set or modified went lost and each column in the table had reverted to data type: Any. Can you help shed some light on this issue?

    Reply
    • Hi Francesca,

      This is common in Power Query, i.e. many processes will remove the data type settings. I recommend using the ‘Detect Data Type’ tool on the Transform tab to quickly set data types again.

      Mynda

      Reply
  5. Important note: If your list is sorted, the sorting and indexing may be unstable or incorrect. To avoid this issue, you can surround your Table.Group code with the Table.Buffer function in the advanced editor, so that the sort order remains stable.

    Here is an example that buffers, groups and adds the index all in one line:

    #”Grouped Rows” = Table.Buffer(Table.Group(#”Sorted Rows”, {“SpecialityID”, “DesignationID”}, {{“GroupedRows”, each Table.AddIndexColumn(_,”Index”,1,1), type table}})),

    Here are some references to this issue:
    https://www.excelando.co.il/en/powerquery-remove-duplicates-bug-workaround/
    https://social.technet.microsoft.com/Forums/WINDOWS/en-US/30e9ab27-23a5-465b-a0c4-36e4e48ce2db/bug-or-feature-when-querying-sqlserver-data?forum=powerquery

    Reply
  6. Hi – I downloaded the Excel workbook from the shortcut in the post and Windows 10 professional reported a Trojan virus in the file: Win32/Spursint.F!cl

    Reply
    • Hi Frans,

      This is an issue with your Windows Defender software. If you search for ‘win32/spursint.f cl false positive’ you’ll see many mentions of this issue.

      If you want to be sure, you can go to https://www.virustotal.com/#/home/url and supply the URL to the workbook and VirusTotal will scan it for you.

      I’m happy to email the file to you if you like?

      Regards

      Phil

      Reply
    • Thanks Phil for the quick reply and my apologies for raising this when it has turned out to be a ‘false positive’ – I thought it best to report it to you in case it was a real virus. Thanks for the offer to email the file – but I have recreated the sample data manually. PS: I always find your site’s training programmes and newsletters very informative. Kind regards, Frans

      Reply
  7. Mil gracias Mynda, entendi la formula de Table.AddIndexColumn :). muchas gracias por los post siempre son geniales y aprendo mucho. Te envio un fuerte abrazo!.

    Reply
    • Good question, Adrian! In this scenario you can get away without the first Index column, but often you’ll find you need it to avoid getting an Expression Error. I just added it out of habit 🙂

      Reply

Leave a Comment

Current ye@r *