Forum Discussion

SuchCT's avatar
SuchCT
Helper II
3 years ago
Solved

Ranking rows with calculated column

Hello everyone,

I have a database that looks like this:
Person             Week                    Sales

A                      1                           845
B1519
C1304
A2973
B2559
C2187
A3528
B3337
C3406
A4272
B4174
C4159



And in need to get the average sales per person considering only their top 2 weeks. For person C that would be 406 from week 3 plus 304 from week 1 divided by 2. AVG. 355.

However i can´t find a way to do it as when I use rankx to get the top weeks based on sales i always get a circular dependency with a formula along these lines
rankx ( allexcept ( table , person , week ) , Sales)

This is what i need

Person       Week          Sales        Rank

A                 1                  845              2
B15192
C13042
A29731
B25591
C21873
A35283
B33373
C34061
A42724
B41744
C41594

 

  • Nathaniel_C's avatar
    Nathaniel_C
    3 years ago

    Hi SuchCT
    This gives the ranks of each person's weeks.

    Rank col = 
    
    var _person = RankingSales[Person]
    
    var _rank = rankx(FILTER(ALL(RankingSales),RankingSales[Person]=_person),RankingSales[Sales])
    
    return _rank


    Let me know if you have any questions.

    If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
    Nathaniel

9 Replies

  • Nathaniel_C's avatar
    Nathaniel_C
    Community Champion

    Hi SuchCT 

    If I understand you, you are trying to get the average of each person's top two weeks. If that is right, please try this.


    On the left is the original table, on the right is the table with the measure.

    My table name is Ranking Sales

    Average of Top 2 weeks = 
    
    var _person = MAX(RankingSales[Person])
    var _week = MAX(RankingSales[Week])
    
    var _calcMax =CALCULATE(MAX(RankingSales[Sales]),FILTER(ALL(RankingSales),RankingSales[Person]=_person))
    
    var _calcMaxWeek= CALCULATE(MAX(RankingSales[Week]),FILTER(ALL(RankingSales),RankingSales[Sales]=_calcMax &&RankingSales[Person]=_person))
    
    var _calcNextMax = CALCULATE(MAX(RankingSales[Sales]),FILTER(ALL(RankingSales),RankingSales[Person]=_person && RankingSales[Week]<>_calcMaxWeek))
    
    var _aver = DIVIDE(_calcMax+_calcNextMax,2)
    return _aver

     


    Let me know if you have any questions.

    If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
    Nathaniel

    • SuchCT's avatar
      SuchCT
      Helper II

      Thanks, I'll se if it aplplies and let you know. Also i'll check if I can make it scalable and work for the top n weeks too

    • SuchCT's avatar
      SuchCT
      Helper II

      This works great but i'd need it to be a column as to use it on a chart as an x-axis, is that possible?

  • Nathaniel_C's avatar
    Nathaniel_C
    Community Champion

    Hi SuchCT ,
    Sounds good.  I will think about making it scalable. I believe this solves your original query.

    Let me know if you have any questions.

    If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
    Nathaniel

    • Nathaniel_C's avatar
      Nathaniel_C
      Community Champion

      Hi SuchCT
      This gives the ranks of each person's weeks.

      Rank col = 
      
      var _person = RankingSales[Person]
      
      var _rank = rankx(FILTER(ALL(RankingSales),RankingSales[Person]=_person),RankingSales[Sales])
      
      return _rank


      Let me know if you have any questions.

      If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
      Nathaniel

      • SuchCT's avatar
        SuchCT
        Helper II

        thanks, that works great so I´ll mark it as the solution. Just one more thing, I'm trying to resolve ties. In the base some person had the exact same sales for two different weeks, I'd need for one of them to have one rank and the next rank for the other week