Forum Discussion

NOVICE02's avatar
NOVICE02
Helper III
5 years ago
Solved

Define Top N by multiple columns

Hi,
Hoping for some direction. Never done this before and in a time crunch

I have a table of names with certain measures like below. there are 100's of names. I need to highlight the Top N names. First by C%, then d% then r%

 

is it possible to do this? and change the ordering depending on the ask. the measures would remain the same,

 

NameC%d%r%
tt20%45%25%
tr10%55%24%
rt5%75%26%
  • Anonymous's avatar
    Anonymous
    5 years ago

    Seems than is enough the only Table.MaxN function to get the expected result, when the values are positive:

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZLLDcQgDER7QdpbDtg4fGqJ0gSify3YbDRhuVhiXmZiY67LteYOx/7Tq5yjcq/30UHtB1JwGhADdThUSabHaRh62Oh10RPkbHTrKALIGKQOkoxEo4SURP2AC4TtSV1J8JBm0kLaPyFI2xJNyxEJgycnu3RBop4yb1EnHWM/HZBnm55eSBdmG/shXNlEwRdEDRFbf68XENh+kh50fwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"C%" = _t, #"d%" = _t, #"r%" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"C%", Percentage.Type}, {"d%", Percentage.Type}, {"r%", Percentage.Type}}),
        besten = Table.MaxN(#"Changed Type",{"C%"},10)
    in
        besten

     

     

     

     

     

3 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello NOVICE02 

     

    I don't know if I get you right. Check out this solution. Depending on the variable "Task" the table is sorted differently. Just change the variable "Task" accordingly to change the result.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKilR0lEyMlAFkiamINIISMbqACWKgBxDsIQpRMIEIlEE0gEWMYeImwHFYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"C%" = _t, #"d%" = _t, #"r%" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"C%", Percentage.Type}, {"d%", Percentage.Type}, {"r%", Percentage.Type}}),
        //Variable for ordering c, d or r allowed
        Task = "d", 
        Top2N = if Task = "c" then 
            Table.FirstN(Table.Sort(#"Changed Type", {{"C%", Order.Descending}}),2) else
            if Task = "d" then 
            Table.FirstN(Table.Sort(#"Changed Type", {{"d%", Order.Descending}}),2) else
            if Task = "r" then 
            Table.FirstN(Table.Sort(#"Changed Type", {{"r%", Order.Descending}}),2) else "wrong task variable"
    in
        Top2N

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • Anonymous's avatar
    Anonymous
    Not applicable

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdFbDoQgDAXQvZDMn5PQ8l6LcROE/UfoVac4/DSxx15e+25aM5th++nVh1G512PrUPsHCQSAB9QxIZ2EfrwGRv/rFlDfkFTSChA15h/JU5bMkM+akOZJKMofXHTemuofOasD0XtRWxDpwCUhMEdNrKdywgt4TZgq15XKkcf5f9sgy7gHmkwSCS94m37C25wt2tpkjF2GKdMxFkqPHSc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"C%" = _t, #"d%" = _t, #"r%" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"C%", Percentage.Type}, {"d%", Percentage.Type}, {"r%", Percentage.Type}}),
        tBestTen=Table.FirstN(Table.Sort(#"Changed Type",{"C%","d%","r%"}),10)
    in
       tBestTen
    • Anonymous's avatar
      Anonymous
      Not applicable

      Seems than is enough the only Table.MaxN function to get the expected result, when the values are positive:

       

       

       

      let
          Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZLLDcQgDER7QdpbDtg4fGqJ0gSify3YbDRhuVhiXmZiY67LteYOx/7Tq5yjcq/30UHtB1JwGhADdThUSabHaRh62Oh10RPkbHTrKALIGKQOkoxEo4SURP2AC4TtSV1J8JBm0kLaPyFI2xJNyxEJgycnu3RBop4yb1EnHWM/HZBnm55eSBdmG/shXNlEwRdEDRFbf68XENh+kh50fwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"C%" = _t, #"d%" = _t, #"r%" = _t]),
          #"Changed Type" = Table.TransformColumnTypes(Source,{{"C%", Percentage.Type}, {"d%", Percentage.Type}, {"r%", Percentage.Type}}),
          besten = Table.MaxN(#"Changed Type",{"C%"},10)
      in
          besten