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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
kleetus51
Helper I
Helper I

Power Query - Rank by group with sort direction variable

I have a table of KPIs by customer. I need to add a Rank column by KPI, but I need it to sort each KPI differently depending on another column. Is it possible to get the following values in my Rank column based on the direction and customer?

 

CustomerKPIValueRanking DirectionRank
WFJPYAverage Days to Pay9.44Ascending1
IYKRJAverage Days to Pay13.88Ascending2
IBZTBAverage Days to Pay22.94Ascending3
LSOFUAverage Days to Pay36.96Ascending4
VTTFHAverage Days to Pay37.27Ascending5
GAVOKAverage Days to Pay44.25Ascending6
HQPQOAverage Days to Pay44.27Ascending7
GAMOJAverage Days to Pay46.43Ascending8
LMMEXAverage Days to Pay48.04Ascending9
QFYNFAverage Days to Pay54.61Ascending10
TGQFLAverage Days to Pay73.30Ascending11
OSZLLAverage Days to Pay77.65Ascending12
EZPGIAverage Days to Pay80.84Ascending13
IYKRJSales353,328.04Descending1
HQPQOSales324,707.65Descending2
TGQFLSales300,375.46Descending3
WFJPYSales288,050.65Descending4
OSZLLSales271,172.62Descending5
GAVOKSales269,026.91Descending6
IBZTBSales239,993.21Descending7
LMMEXSales233,063.59Descending8
GAMOJSales199,614.27Descending9
VTTFHSales189,326.97Descending10
LSOFUSales156,850.82Descending11
EZPGISales138,739.33Descending12
QFYNFSales122,803.12Descending13
1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@kleetus51 

Sort it this way:

let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  #"Changed Type" = Table.TransformColumnTypes(
    Source, 
    {{"Customer", type text}, {"KPI", type text}, {"Value", type number}, {"Ranking Direction", type text}}
  ), 
  #"Grouped Rows" = Table.Group(
    #"Changed Type", 
    {"KPI"}, 
    {
      {
        "Detail", 
        each Table.AddRankColumn(
          _, 
          "Rank", 
          if _[Ranking Direction]{0} = "Descending" then {{"Value", Order.Descending}} else {{"Value", Order.Ascending}}
        )
      }
    }
  ), 
  Custom1 = Table.Combine(#"Grouped Rows"[Detail])
in
  Custom1
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

2 REPLIES 2
ThxAlot
Super User
Super User

Isn't a calculated column easy enough?

ThxAlot_0-1731449127406.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



Fowmy
Super User
Super User

@kleetus51 

Sort it this way:

let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  #"Changed Type" = Table.TransformColumnTypes(
    Source, 
    {{"Customer", type text}, {"KPI", type text}, {"Value", type number}, {"Ranking Direction", type text}}
  ), 
  #"Grouped Rows" = Table.Group(
    #"Changed Type", 
    {"KPI"}, 
    {
      {
        "Detail", 
        each Table.AddRankColumn(
          _, 
          "Rank", 
          if _[Ranking Direction]{0} = "Descending" then {{"Value", Order.Descending}} else {{"Value", Order.Ascending}}
        )
      }
    }
  ), 
  Custom1 = Table.Combine(#"Grouped Rows"[Detail])
in
  Custom1
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.