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

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

Reply
Nicks612
Helper I
Helper I

How to give dynamic ranking with respect to other columns using DAX or power query

Hello everyone,

 

I want to generate a ranking column in Table A with respect to the Sr.No and DBkey columns.

Ex. for Sr.No = 123456, ranking must be given according to the DBKey column in ascending sorting which means the smallest DBKey value for the given Sr.No is '20230503070939' so the Rank column should display '1' against the DBKey value.

then,

 

the DBKey '20230503084725' is the 2nd largest number so the Rank column should display '2' against the DBKey value.

then,

 

the DBKey '20230503092547' is the 3rd largest number so the Rank column should display '3' against the DBKey value.

 

like that it should display for all other Sr.No.

you can understand the requirements by reviewing the below tables

 

Original Data:

Table A
Sr.NoDBKey
12345620230503092547
12345620230503070939
12345620230503084725
23567820230508100137
13245620230524172714
13245620230527141557
16547820230514120545
16547820230513100529
16547820230514153705
16547820230514194523
16547820230514083357
76543220230507130646
76543220230507131046

 

Result Table:

 

Table A
Sr.NoDBKeyRank
123456202305030925473
123456202305030709391
123456202305030847252
235678202305081001371
132456202305241727141
132456202305271415572
165478202305141205453
165478202305131005291
165478202305141537054
165478202305141945235
165478202305140833572
765432202305071306461
765432202305071310462

 

I want the solution by using DAX and by power query editor.  Kindly, guide me for the mentioned query.

thanks in Advance!

1 REPLY 1
lbendlin
Super User
Super User

Here is the Power Query version. I hope that it is clear the Power Query is not dynamic.  Its results are not impacted by user interaction with filters.

 

Note that the ranking result differs from your expected results.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc+xDcQwDAPAXVynoEjRsmcJsv8a738EyBdOqwMp6TxbUOnejkZQMIRJZ7Xr2Flhar7YyKJ/RrnXeGwEELo7xf8cM4oVubU1D/vO9XXV07mEcHprWvvM+ZKzCvtcxkxTe8OQ7ltqmfj8VyH07G8W+Nr1AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Sr.No = _t, DBKey = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Sr.No"}, {{"Rows", each _, type table [Sr.No=nullable text, DBKey=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddRankColumn([Rows],"Rank",{"DBKey", Order.Ascending},  [RankKind = RankKind.Competition])),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"DBKey", "Rank"}, {"DBKey", "Rank"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Sr.No", "DBKey", "Rank"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

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

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Kudoed Authors