Unique Rank value for a subgroup within a group

点点圈 提交于 2019-11-26 17:48:43

问题


I am trying to get a unique rank value (e.g. {1, 2, 3, 4} from a subgroup in my data. SUMPRODUCT will produce ties{1, 1, 3, 4}, I am trying to add the COUNTIFS to the end to adjust the duplicate rank away.

                subgroup
col B   col M    rank
LMN       01      1
XYZ       02        
XYZ       02    
ABC       03    
ABC       01    
XYZ       01    
LMN       02      3
ABC       01    
LMN       03      4
LMN       03      4  'should be 5
ABC       02    
XYZ       02    
LMN       01      1  'should be 2   

So far, I've come up with this.

=SUMPRODUCT(($B$2:$B$38705=B2)*(M2>$M$2:$M$38705))+countifs(B2:B38705=B2,M2:M38705=M2)

What have I done wrong here?


回答1:


The good news is that you can throw away the SUMPRODUCT function and replace it with a pair of COUNTIFS functions. The COUNTIFS can use full column references without detriment and is vastly more efficient than the SUMPRODUCT even with the SUMPRODUCT cell ranges limited to the extents of the data.

In N2 as a standard function,

=COUNTIFS(B:B, B2,M:M, "<"&M2)+COUNTIFS(B$2:B2, B2, M$2:M2, M2)

Fill down as necessary.

      

  Filtered Results

        




回答2:


Solution basing on OP

Studying your post demanding to post any alternatives, I got interested in a solution based on your original approach via the SUMPRODUCT function. IMO this could show the right way for the sake of the art:

Applied method

Get

  • a) all current ids with a group value lower or equal to the current value

    MINUS

  • b) the number of current ids with the identical group value starting count from the current row

    PLUS

  • c) the increment of 1

Formula example, e.g. in cell N5:

=SUMPRODUCT(($B$2:$B$38705=$B5)*($M$2:$M$38705<=$M5))-COUNTIFS($B5:$B$38705,$B5,$M5:$M$38705,$M5)+1

P.S.

Of course, I agree with you preferring the above posted solution, too :+)



来源:https://stackoverflow.com/questions/34935673/unique-rank-value-for-a-subgroup-within-a-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!