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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
AuroraNI
Helper III
Helper III

Create Column counting values in ascending order

Hi,

Was hoping people could help.  I would like to add a column in Query editor counting the number of times a value appears in a column in ascending order (see below desired output).  I have tried to combine countrows, filter and earliest but haven't quite figured it out. Thanks!

CountryValue
Algeria1
Belgium1
Belgium2
Belgium3
Canada1
Canada2
Canada3
Canada4
Chile1
1 ACCEPTED SOLUTION
v-diye-msft
Community Support
Community Support

Hi @AuroraNI 

 

Add the index column first, and then add the calculated column:

Column = CALCULATE(DISTINCTCOUNT('Table'[Index]),FILTER('Table',[Country]=EARLIER('Table'[Country])&&[Index]<=EARLIER('Table'[Index])))

 3.PNG

Pbix attached.

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.

View solution in original post

8 REPLIES 8
v-diye-msft
Community Support
Community Support

Hi @AuroraNI 

 

Add the index column first, and then add the calculated column:

Column = CALCULATE(DISTINCTCOUNT('Table'[Index]),FILTER('Table',[Country]=EARLIER('Table'[Country])&&[Index]<=EARLIER('Table'[Index])))

 3.PNG

Pbix attached.

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.

Thanks for this, worked really well!

Ashish_Mathur
Super User
Super User

Hi,

Your question is not clear.  Share 2 seperate tables - input and output.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi, apologies here is the input column

Country

Algeria
Belgium
Belgium
Belgium
Canada
Canada
Canada
Canada
Chile

 

and here is the output I would like

Country

Value

Algeria1
Belgium1
Belgium2
Belgium3
Canada1
Canada2
Canada3
Canada4
Chile1

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}}),
    Partition = Table.Group(#"Changed Type", {"Country"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Index"}, {"Index"})
in
    #"Expanded Partition"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for this answer, I have tried this and works thank you.  I will go with the calculated column option as simpler in my current dashboard

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Vvelarde
Community Champion
Community Champion

@AuroraNI 

 

Hi, maybe there are better ways but this is my first way:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixKT83JTFSK1YlWci7KT0yGsgNSi0rBDGQFrq6hoWCGb2pFZnI+qphTUWJxZg5uzXBBDFNiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Pais = _t]),


    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Pais", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Pais"}, {{"Count", each _, type table [Pais=text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"index",1,1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Pais", "index"}, {"Custom.Pais", "Custom.index"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Pais", "Count"})
in
    #"Removed Columns"

 

Regards

 

Victor




Lima - Peru

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.