Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
arpost
Post Prodigy
Post Prodigy

How would you auto-number items in a group in Power Query?

I have a table that I need to group together so I can number off the values within each group. I know how to use group by, but the problem is I need the index to restart with each group.

 

For example, let's say I had a table with the following:

ProductDateValue
Food1/1/212000
Food2/1/211000
Food2/1/21500
Shoes1/1/211000
Shoes2/1/21500

 

I want the final product to look like this

ProductDateValueID
Food1/1/2120001
Food2/1/2110002
Food2/1/215002
Shoes1/1/2110001
Shoes2/1/215002

 

Anyone have any ideas?

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @arpost 

 

I am not sure how you will write your DAX, but based on your sample, here is one way to add the column you want in M

Vera_33_0-1626922694837.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvPT1HSUTIw1AciIwMjQyDHyMDAQClWB0XSCCZpiE/SFFPOGEkjupwJHjlTouRMMeTM8OgzxyNngUfOErecoQEeOUM8ckZ47APrM8IuZ4QuF5yRn1qMHoXwWEKWRY2mWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    MinYear = Date.Year( List.Min( #"Changed Type"[Date])),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ID", each Date.Month([Date])+(Date.Year([Date])-MinYear)*12)
in
    #"Added Custom"

 

View solution in original post

9 REPLIES 9
Vera_33
Resident Rockstar
Resident Rockstar

Hi @arpost 

 

ID is your index column starts with 1 for each Product? and it is 1,2,3 for Food, not 1,2,2 right?

 

Vera_33_0-1626834079473.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvPT1HSUTIw1AciIwMjQyDHyMDAQClWByFphCRpiE/SFCoXnJGfWoxuLFwnXBZDaywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Date", type date}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Product"}, {{"allrows", each _, type table }}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([allrows],"ID",1,1)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Product", "Date", "Value", "ID"}, {"Product", "Date", "Value", "ID"})
in
    #"Expanded Custom"

Thanks for the fast reply, @Vera_33. Actually, it is correct that the numbering is 1,2,2 for the "Food" category because I'm needing to create a numbered sequence for all months (0-6, 7-12, 13-24, 25+) for each "group" to track values to targets. In my example with Food, there are 2 actual months while there are 3 Food entries. This will enable me to then display this data in a visual like below:

 

ProductMonthsValue
Food23500
Shoes11500
Vera_33
Resident Rockstar
Resident Rockstar

Hi @arpost 

 

So you did not want an Index column, just a column with month number? The visual you want to do is like counting Months? If you have a Calendar table, you can use DAX measure to count it directly based on my understanding

 

Vera_33_0-1626916931057.png

 

@Vera_33, thanks for the response. Perhaps this will help clarify my need a little more. Let's say I have two years of values from 1/1/2019 to 1/1/2021. I'm needing to programatically create a list that numbers off the "month number" but in sequence like an index, so rather than starting over at 12, it would continue to 13, 14, 15, etc. This also needs to restart when changing from one group to another. I've created this list below to illustrate.

 

Hope this helps.

 

ProductDateValueID
Food1/1/2120001
Food2/1/2110002
Food2/1/215002
Food3/1/211003
Food4/1/211004
Food5/1/211005
Food5/12/211505
Food6/1/211006
Food7/1/211007
Food8/1/211008
Food9/1/211009
Food10/1/2110010
Food11/1/2110011
Food12/1/2110012
Food1/1/2210013
Food2/1/2110014
Shoes1/1/2110001
Shoes2/1/215002

Hi @arpost 

 

I am not sure how you will write your DAX, but based on your sample, here is one way to add the column you want in M

Vera_33_0-1626922694837.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvPT1HSUTIw1AciIwMjQyDHyMDAQClWB0XSCCZpiE/SFFPOGEkjupwJHjlTouRMMeTM8OgzxyNngUfOErecoQEeOUM8ckZ47APrM8IuZ4QuF5yRn1qMHoXwWEKWRY2mWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    MinYear = Date.Year( List.Min( #"Changed Type"[Date])),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ID", each Date.Month([Date])+(Date.Year([Date])-MinYear)*12)
in
    #"Added Custom"

 

Vera_33
Resident Rockstar
Resident Rockstar

Hi @arpost 

 

I am not sure how you will write your DAX, but based on your sample, here is one way to add the column you want in M

Vera_33_0-1626922694837.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvPT1HSUTIw1AciIwMjQyDHyMDAQClWB0XSCCZpiE/SFFPOGEkjupwJHjlTouRMMeTM8OgzxyNngUfOErecoQEeOUM8ckZ47APrM8IuZ4QuF5yRn1qMHoXwWEKWRY2mWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    MinYear = Date.Year( List.Min( #"Changed Type"[Date])),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ID", each Date.Month([Date])+(Date.Year([Date])-MinYear)*12)
in
    #"Added Custom"

 

@Vera_33, thanks for sharing that! This is really close to the solution and may in fact be what I need.

 

However, I did a little testing with the M code you provided and noticed that the numbering is solely based on month number rather than on the month's number "in data sequence" by which I basically mean that if there are no entries for 2 months, the numbers jump over those rather than continuing.

 

In the example below, there were no Food entries for the 7th month, so the auto-number skips 7 and goes ahead with 8, which is a problem when needing to create a continuous numbered sequence.

 

arpost_0-1626956944627.png

 

Is it possible to correct this so the auto-number is based on the month's occurence in the list rather than on the literal month number?

 

Here's the modified M code:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdE9CoAwDAXgu3Qu5Me26gW8gGPppuDWwfuDXSxtaETIkveRJS9Gs+V8GGuQoAwjU1kYEU2yHfKL9IVeGANOzaE0p9kE6H+Z780BBu2u2KyYhxVGeVDyRcmJNGCgsdTH8/hFLG2/8nnLxmoprfatpAc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    MinYear = Date.Year( List.Min( #"Changed Type"[Date])),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ID", each Date.Month([Date])+(Date.Year([Date])-MinYear)*12)
in
    #"Added Custom"

 

 

 

Vera_33
Resident Rockstar
Resident Rockstar

Hi @arpost 

 

I see, here is one way, although I really doubt you can do DAX measure to count YYYYMM directly

Vera_33_0-1626999341365.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdE9CoAwDAXgu3Qu5Me26gW8gGPppuDWwfuDXSxtaETIkveRJS9Gs+V8GGuQoAwjU1kYEU2yHfKL9IVeGANOzaE0p9kE6H+Z780BBu2u2KyYhxVGeVDyRcmJNGCgsdTH8/hFLG2/8nnLxmoprfatpAc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "YYMM", each Date.ToText([Date],"yyyyMM")),
    #"Removed Other Columns" = Table.Distinct( Table.SelectColumns(#"Added Custom",{"YYMM", "Product"})),
    #"Grouped Rows" = Table.Group(#"Removed Other Columns", {"Product"}, {{"allrows", each _, type table }}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([allrows],"ID",1,1)),
    #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom1",{"Custom"}),
    indexTable = Table.ExpandTableColumn(#"Removed Other Columns1", "Custom", {"YYMM", "Product", "ID"}, {"YYMM", "Product", "ID"}),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Product", "YYMM"}, indexTable, {"Product", "YYMM"}, "indexTable", JoinKind.LeftOuter),
    #"Expanded indexTable" = Table.ExpandTableColumn(#"Merged Queries", "indexTable", {"ID"}, {"ID"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded indexTable",{"YYMM"})
in
    #"Removed Columns"

 

@Vera_33, both of those solutions worked quite well! I did have to make some M modifications for my scenario but was able to use them. Thanks for giving two stellar solutions!

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.