Forum Discussion

Anthony007's avatar
Anthony007
Helper I
9 years ago
Solved

Build a rating considering the duplicate values

Help please build the rating with DAX.

 

There is a table with three columns.In the value there are duplicates:

WeekCompanyValue
33Company A44
33Company B44
33Company C100
33Company D50
34Company A60
34Company B20
34Company C80
34Company D200
35Company A1123
35Company B1000
35Company C2000
35Company D2000

 

I need to build a rating for each company every week:

WeekCompanyValueRank
33Company A444
33Company B443
33Company C1001
33Company D502
34Company A603
34Company B204
34Company C802
34Company D2001
35Company A11233
35Company B10004
35Company C20002
35Company D20001

 

In Power bi i used 2 formula:

Total value = SUM(Table1[Value])

Rank = RANKX(FILTER(ALLSELECTED(Table1[Company]);NOT(ISBLANK([Total value])));Table1[Total value];;DESC;Dense)

And have next result:

WeekCompanyValueRank
33Company A443
33Company B443
33Company C1001
33Company D502
34Company A603
34Company B204
34Company C802
34Company D2001
35Company A11232
35Company B10003
35Company C20001
35Company D20001

 The problem, that the same values ​​have the same rating.

pbix on OneDrive

Thanks for any help!

  • ImkeF's avatar
    ImkeF
    9 years ago

    Sorry, stupid sorting order. This will do:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjZW0lFyzs8tSMyrVHAEsk1MlGJ10MSdcIg7A9mGBgaYEi5AtilU3ATNAjMs4iALjLCIgyywwCLuAlYPlTBFs8DQ0MgYU8YJ4lYsepwhhmGRcYHLxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Week = _t, Company = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Week", Int64.Type}, {"Company", type text}, {"Value", Int64.Type}}),
        #"Sorted Rows2" = Table.Buffer(Table.Sort(#"Changed Type",{{"Value", Order.Descending}, {"Company", Order.Descending}})),
        #"Grouped Rows" = Table.Group(#"Sorted Rows2", {"Week"}, {{"MyRank", each Table.AddIndexColumn(_, "MyRank",1,1), type table}}),
        #"Expanded MyRank" = Table.ExpandTableColumn(#"Grouped Rows", "MyRank", {"Company", "Value", "MyRank"}, {"Company", "Value", "MyRank.1"}),
        #"Sorted Rows1" = Table.Sort(#"Expanded MyRank",{{"Week", Order.Ascending}, {"MyRank.1", Order.Descending}})
    in
        #"Sorted Rows1"

6 Replies

    • ImkeF's avatar
      ImkeF
      Community Champion

      Hi Anthony,

      I'm not aware of a possiblity where RANK (in DAX) would return different values for the same amount.

       

      But you can achieve the result you've given here as a static table using M in the query editor:

       

      let
          Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjZW0lFyzs8tSMyrVHAEsk1MlGJ10MSdcIg7A9mGBgaYEi5AtilU3ATNAjMs4iALjLCIgyywwCLuAlYPlTBFs8DQ0MgYU8YJ4lYsepwhhmGRcYHLxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Week = _t, Company = _t, Value = _t]),
          #"Changed Type" = Table.TransformColumnTypes(Source,{{"Week", Int64.Type}, {"Company", type text}, {"Value", Int64.Type}}),
          #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Company", Order.Descending}, {"Value", Order.Descending}}),
          #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Week"}, {{"MyRank", each Table.AddIndexColumn(_, "MyRank",1,1), type table}}),
          #"Expanded MyRank" = Table.ExpandTableColumn(#"Grouped Rows", "MyRank", {"Company", "Value", "MyRank"}, {"Company", "Value", "MyRank.1"}),
          #"Sorted Rows1" = Table.Sort(#"Expanded MyRank",{{"Week", Order.Ascending}, {"MyRank.1", Order.Descending}})
      in
          #"Sorted Rows1"

       

      • AlbertoFerrari's avatar
        AlbertoFerrari
        Most Valuable Professional

        RANKX will return the same ranking for the same value, this is by design and it is the correct behavior. If you want the ranking to be different, then you need to write a measure that takes into account other columns to guarantee that the ranking is unique. How do you decide which of the two identical values should be ranked first? It is up to you to take this decision and, if it does not really matter, you can use the company ID to differentiate between them.

        Of course, the result will not be accurate, but if this is what you need, you only need to change the measure to take the company ID into account.