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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
sbadiger3
Helper I
Helper I

How to rank a row based on a column

Hi everyone, I need help in creating a column in a table in power query. In my table I have a column 'order_line_id'. This column has duplicates. Now, I need to create a column 'rank' which gives rank based on the column 'order_line_id'. I am attaching below a demo table below. 

order_line_idrank
1234561
1234571
1234521
1575931
1234562

 

Here in this case, I'm creating a rank for all the values of order_line_id. In the example table above 123456 is a duplicate order_line_id and the whichever comes first in the table is ranked 1 and later 2. 

It would be great if someone can help me to achieve this in power query. 

Thanks in advance!
 

1 ACCEPTED SOLUTION
mussaenda
Super User
Super User

mussaenda_0-1707378618752.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U4rVgTLNEUwjCNPU3NTSGCEKVBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [order_line_id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"order_line_id", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"order_line_id"}, {{"Count", each _, type table [order_line_id=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Rank", 1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Rank"}, {"Rank"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"Rank", Order.Ascending}})
in
    #"Sorted Rows"

View solution in original post

3 REPLIES 3
mussaenda
Super User
Super User

We grouped them according to order line id 

then created an index column according to it 

then sorted the rank according to your requirement

mussaenda
Super User
Super User

mussaenda_0-1707378618752.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1U4rVgTLNEUwjCNPU3NTSGCEKVBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [order_line_id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"order_line_id", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"order_line_id"}, {{"Count", each _, type table [order_line_id=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Rank", 1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Rank"}, {"Rank"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"Rank", Order.Ascending}})
in
    #"Sorted Rows"

@mussaenda , can you please explain what all steps you performed? 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.