≡ Menu

Excel’s Missing RANKIF Function

Excel Function

Excel offers a number of conditional IF functions such as COUNTIF and SUMIF which allow you to count or sum a range based on the values in another column. If you have Excel 2007 or later you’ll have the really useful COUNTIFS and SUMIFS functions which allow you to perform multi-condition actions based on the values in multiple columns. If you are looking around for a RANKIF function you are going to be disappointed – Microsoft appear to have missed a trick.

I wanted a RANKIF function the other day when I was reviewing the latest Reference Cost release to enable me to rank all the available hospitals according to their cost (reference cost) for a specific HRG.

If you are using a version of Excel prior to 2007 you will need to use the SUMPRODUCT function to achieve the ranking but if you are using a later version of Excel you have the additional option of using the COUNTIFS function to assist. They both do the same job, but COUNTIFS is slightly easier to understand.

RANKIF using SUMPRODUCT and COUNTIFS

Performing a RANKIF using the SUMPRODUCT function

The formula I used to achieve the above ranking was:

=1+SUMPRODUCT(($B$2:$B$12=B2)*($C$2:$C$12>C2))

Copied down the column it will return the number of organisations for a given HRG which have costs higher than the selected row. Adding 1 to the result just ensures that the highest cost organisation starts with a rank of 1 rather than 0.

With SUMPRODUCT the “*” symbol between conditions represents an AND a “+” symbol would represent an OR.

It’s a little difficult to explain this formula in real terms but the bracketed areas are returning true or false (1 or 0) and the sumproduct multiplies the two arrays together and then adds the results. So in our example of 8 rows, the calculation in cell D2 would be:

SUMPRODUCT illustration

And just to reiterate the process, the calculation D3 would be like this:

SUMPRODUCT illustration

Performing a RANKIF using the COUNTIFS function

The formula I used to achieve the above ranking was:

= 1+COUNTIFS($B$2:$B$9,B2,$C$2:$C$9,“>”&C2)

This outputs exactly the same result but it is much easier to understand what the formula is doing:

Count the number of entries in column C where the HRG is the same and where the cost is higher than in the current row. Add 1 just to ensure that the highest cost returns 1 rather than 0.

Link to excel 2007 and 97–2003 versions of the RANKIF workbooks (note that the earlier version does not support COUNTIFS).

Doctor Moxie

Written by -

NHS Accountant with geeky tendencies - serial blogger on subjects varying from Excel, Raspberry Pi, productivity, allotment gardening and running. The NHSExcel blog is reserved for Excel topics.

Comments on this entry are closed.

  • GEOFF

    This is great information, and still I am having challenges excluding blanks.
    Say, for instance, I have a column of performance reviews (T2:T169), some of which are blank because we have data on new employees. I tried the following, hoping to get the top scorer to be ranked “1”:
    =IF(T2″”,1+COUNTIF($T$2:$T$169,”>”&T2),COUNTIF($T$2:$T$169,”>”&T2))

    Would anyone be kind enough to provide advice? Thank you in advance!

  • http://www.doctormoxie.com Doctor Moxie

    I used a similar IF function.
    If the cell with performance scores is blank, then count how many records are greater than 0
    If the cell has a value calculate as normal, ie how many rows have a value greater than the current performance value:
    =IF(C16=””,1+COUNTIFS($B$16:$B$23,B16,$C$16:$C$23,”>”&0),1+COUNTIFS($B$16:$B$23,B16,$C$16:$C$23,”>”&C16))

    • Jonny

      Tried the sumproduct version on a larger dataset and the rankings seem to break jumping from 1 to 98 to 468 :-s