Beruflich Dokumente
Kultur Dokumente
ablebits.com/office-addins-blog/2017/09/06/excel-rank-functions/
The tutorial explains the specificities of the Excel Rank functions and shows how to do
ranking in Excel based on multiple criteria, rank data by group, calculate percentile rank,
and more.
When you need to determine the relative position of a number in a list of numbers, the
easiest way is to sort the numbers in ascending or descending order. If for some reason
sorting is not possible, a Rank formula in Excel is the perfect tool to do the job.
The RANK function in Excel returns the order (or rank) of a numeric value compared to
other values in the same list. In other words, it tells you which value is the highest, the
second highest, etc.
In a sorted list, the rank of a certain number would be its position. The RANK function in
Excel can determine the rank starting with the largest value (as if sorted in descending
order) or the smallest value (as if sorted in ascending order).
RANK(number,ref,[order])
Where:
Ref (required) - a list of numeric values to rank against. It can be supplied as an array of
numbers or a reference to the list of numbers.
If 0 or omitted, the values are ranked in descending order, i.e. from largest to
smallest.
If 1 or any other non-zero value, the values are ranked in ascending order, i.e. from
smallest to largest.
RANK.EQ(number,ref,[order])
In Excel 2007 and lower versions, you should always use the RANK function. In Excel
2010, Excel 2013, and Excel 2016, you can go with either RANK or RANK.EQ. However,
it'd be wise to use RANK.EQ because RANK can be discontinued at any moment.
1/13
Excel RANK.AVG function
RANK.AVG is another function to find rank in Excel that is available only in Excel 2010,
Excel 2013, Excel 2016 and later.
RANK.AVG(number,ref,[order])
The difference is that if more than one number has the same rank, the average rank is
returned (AVG stands for "average").
All three formulas rank numbers in column B in descending order (the order argument
omitted):
=RANK($B2,$B$2:$B$7)
2/13
=RANK.EQ($B2,$B$2:$B$7)
=RANK.AVG($B2,$B$2:$B$7)
The difference is in how these formulas process duplicate values. As you see, the same
score appears twice, in cells B5 and B6, which affects subsequent ranking:
The RANK and RANK.EQ formulas give a rank of 2 to both duplicate scores. The
next highest score (Daniela) is ranked 4th. A rank of 3 is not given to anyone.
The RANK.AVG formula assigns a different rank to each duplicate behind the
scenes (2 and 3 in this example), and returns the average of those ranks (2.5).
Again, the 3rd rank is not assigned to anyone.
To have number ranked against other numbers sorted in ascending order, put 1 or any
other non-zero value in the optional third argument.
For example, to rank the 100-meter sprint times of the students, you can use either of the
below formulas:
=RANK(B2,$B$2:$B$7,1)
=RANK.EQ(B2,$B$2:$B$7,1)
Please pay attention that we lock the range in the ref argument by using absolute cell
references, so that it won't change when we copy the formula down the column.
=RANK.EQ(B2,$B$2:$B$7)+COUNTIF($B$2:B2,B2)-1
=RANK.EQ(B2,$B$2:$B$7,1)+COUNTIF($B$2:B2,B2)-1
Consequently, for all unique values and 1st occurrences of duplicate values, COUNTIF
returns 1; and you subtract 1 at the end of the formula to restore the original rank.
4/13
For 2nd occurrences of duplicate values, COUNTIF returns 2. By subtracting 1 you
increment the rank by 1 point, thus preventing duplicates. If there happen to be 3
occurrences of the same value, COUNTIF()-1 would add 2 to their ranking, and so on.
The first function determines how many values are greater than or less than the
number to be ranked, depending on whether you are ranking descending or
ascending, respectively.
The second function (with the "expanding range" $B$2:B2 as in the above
example) gets the number of values equal to the number.
For example, to rank numbers uniquely from highest to lowest, you'd use this formula:
=COUNTIF($B$2:$B$7,">"&$B2)+COUNTIF($B$2:B2,B2)
As shown in the screenshot below, the tie-break is successfully resolved, and a unique
rank is assigned to each student:
In our sample dataset, let's add total scores in column C and calculate rank as follows:
To have it done, we will be using a regular RANK/RANK.EQ formula to find rank, and the
COUNTIFS function to break a tie:
=RANK.EQ($B2,$B$2:$B$7)+COUNTIFS($B$2:$B$7,$B2,$C$2:$C$7,">"&$C2)
Compared to the above example, this rank formula is more objective: Timothy is ranked
2nd because his total score is higher than that of Julia:
5/13
How this formula works
The RANK part of the formula is obvious, and the COUNTIFS function does the
following:
Because COUNTIFS works with the AND logic, i.e. counts only cells that meet all of the
specified conditions, it returns 0 for Timothy since no other student with the same Math
score has a higher total score. Hence, Timothy's rank returned by RANK.EQ is
unchanged. For Julia, the COUNTIFS function returns 1 because one student with the
same Math score has a higher total, so her rank number is incremented by 1. If one more
student had the same Math score and a total score lower than that of Timothy and Julia,
his/her rank would be incremented by 2, and so on.
Instead of the RANK or RANK.EQ function, you could use COUNTIF to check the main
criteria, and COUNTIFS or SUMPRODUCT to resolve a tie break:
=COUNTIF($B$2:$B$7,">"&$B2)+COUNTIFS($B$2:$B$7,$B2,$C$2:$C$7,">"&$C2)+1
=COUNTIF($B$2:$B$7,">"&B2)+SUMPRODUCT(--($C$2:$C$7=C2),--($B$2:$B$7>B2))+1
The result of these formulas are exactly the same as shown above.
6/13
To get percentile rank in Excel, use the RANK or RANK.EQ function with a non-zero
order argument to rank numbers from smallest to largest, and then divide the rank by the
count of numbers. So, the generic Excel Percentile Rank formula goes as follows:
RANK.EQ(topmost_cell,range,1)/COUNT(range)
To calculate the percentile rank of our students, the formula takes the following shape:
=RANK.EQ(B2,$B$2:$B$7,1)/COUNT($B$2:$B$7)
To have the results displayed correctly, be sure to set the Percentage format to the
formula cells:
=RANK(B2,($B$2,$B$4,$B$6))
To prevent errors in non-ranked cells, wrap RANK in the IFERROR function, like this:
=IFERROR(RANK(B2,($B$2,$B$4,$B$6)), "")
Please notice that a duplicate number is also given a rank, though cell B5 is not included
in the formula:
=IFERROR(RANK(B2,range), "")
7/13
How to rank in Excel by group
When working with entries organized into some kind of data structure, data may belong
to various groups, and you might want to rank numbers within each group individually.
The Excel RANK function cannot resolve this challenge, so we are going to use a more
complex SUMPRODUCT formula:
=SUMPRODUCT((A2=$A$2:$A$7)*(C2<$C$2:$C$7))+1
=SUMPRODUCT((A2=$A$2:$A$7)*(C2>$C$2:$C$7))+1
Where:
In this example, we use the first formula to rank numbers in each group from largest to
smallest:
First, you check the group (A2=$A$2:$A$7). This part returns an array of TRUE
and FALSE based on whether a range element belongs to the same group as A2.
8/13
Second, you check the score. To rank values from largest to smallest (descending
order), use the condition (C2<$C$2:$C$11), which returns TRUE for the cells
greater than or equal to C2, FALSE otherwise.
Since in Microsoft Excel terms, TRUE = 1 and FALSE = 0, multiplying the two arrays
gives an array of 1's and 0's, where 1 is returned only for the rows in which both
conditions are met.
Then, SUMPRODUCT adds up the elements of the 1's and 0's array, hence returning 0
for the largest number in each group. And you add 1 to the result to start ranking with 1.
The formula that ranks numbers within groups from smallest to largest (ascending
order) works with the same logic. The difference is that SUMPRODUCT returns 0 for the
smallest number in a particular group, since no number in that group meets the 2nd
condition (C2>$C$2:$C$7). Again, you replace zero rank with the 1st rank by adding 1 to
the formula result.
Instead of SUMPRODUCT, you can use the SUM function to add up the array elements.
But this would require using an array formula, completed via Ctrl + Shift + Enter. For
example:
=SUM((A2=$A$2:$A$7)*(C2<$C$2:$C$7))+1
With numbers in cells A2 to A10, use one of the following formulas to get individual
ranking for positive and negative values:
9/13
How these formulas work
To begin with, let's break down the formula that ranks positive numbers in descending
order:
In the logical test of the IF function, you check whether the number is greater than
zero.
If the number is greater than 0, the COUNTIF function returns the count of values
higher than the number being ranked.
In this example, A2 contains the 2nd highest positive number, for which COUNTIF
returns 1, meaning there is just one number greater than it. In order to start our
ranking with 1, not 0, we add 1 to the formula result, so it returns a rank of 2 for A2.
If the number is greater than 0, the formula returns an empty string ("").
The formula that ranks positive numbers in ascending order works a bit differently:
If the number is greater than 0, the first COUNTIF gets the total count of positive
numbers in the data set, and the second COUNTIF finds out how many values are higher
than that number. Then, you subtract the latter from the former, and get the desired rank.
In this example, there are 5 positive values, 1 of which is greater than A2. So, you
subtract 1 from 5, thus getting a rank of 4 for A2.
Note. All of the above formulas ignore zero values because 0 belongs neither to the set
of positive nor to the set of negative numbers. To include zeros in your ranking, replace
>0 and <0 with >=0 and <=0, respectively, where the formula logic requires.
For example, to rank positive numbers and zeros from largest to smallest, use this
formula: =IF($A2>=0,COUNTIF($A$2:$A$10,">"&A2)+1,"")
The best thing about this formula is that it works beautifully both for positive and negative
numbers, leaving zero values out of ranking:
Here's how the Excel RANK IF formula ranks numbers from largest to smallest ignoring
zeros:
The first IF checks whether the number is 0, and if it is, returns an empty string:
IF($B2=0,"", )
If the number is not zero, the second IF checks whether it's greater than 0, and if it
is, a regular RANK / RANK.EQ function calculates its rank:
IF($B2>0,RANK($B2,$B$2:$B$10),)
If the number is less than 0, you adjust ranking by the zero count. In this example,
there are 4 positive numbers and 2 zeros. So, for the largest negative number in
B10, an Excel RANK formula would return 7. But we skip zeros, and therefore we
need to adjust the rank by 2 points. For this, we subtract the number of zeros from
the rank:
11/13
RANK($B2,$B$2:$B$10)-COUNTIF($B$2:$B$10,0))
Yep, it's that easy! The formula to ranks numbers from smallest to largest ignoring zeroes
works in a similar manner, and it could be a good brain exercise to deduce its logic :)
The task can be fulfilled with one of the bellow formulas, at the heart of which is the ABS
function that returns the absolute value of a number:
As the result, negative numbers are ranked as if they were positive numbers:
For example, we can get the top 3 scores of our students with this formula:
=LARGE($B$2:$B$7, $D3)
Additionally, you can retrieve the students' names by using the INDEX MATCH formula
(provided there are no duplicate scores in the top 3):
=INDEX($A$2:$A$7,MATCH(E3,$B$2:$B$7,0))
12/13
Similarly, you can use the SMALL function to pull the bottom 3 values:
=SMALL($B$2:$B$7, $D3)
That's how you do ranking in Excel. To better understand and probably reverse-engineer
the formulas discussed in this tutorial, you are welcome to download our Sample Rank
Excel workbook.
I thank you for reading and hope to see you on our blog next week!
13/13