Leila Gharani (youtube) has helped me figure out the reason behind using COUNTIF in the k in the above formula (=INDEX($E$5:$E$17,SUMPRODUCT(SMALL(($C$5:$C$7="Josh")*(ROW($C$5:$C$17)-4),2+COUNTIF($C$5:$C$17,"<>Josh")))).
Hi Ufoo ,
It is good that you have understood how the formula works.
I would like you to understand why a simpler formula can be used.
1. The INDEX function , on its own , will display a correct result only if the index value is a scalar value , a single value. If multiple values are going to be used , then the only way it will work correctly is if some other function is wrapped around it. One such function can be the SUM function.
2. The SUMPRODUCT function has been used , which is basically going to sum up all the results that are made available to it ; if the entire formula has to work correctly , it is absolutely essential that the SUMPRODUCT function is passed exactly one value ; if two results such as 3 and 7 are passed to it , what it will return will be 10 , and in this case , such a return value will give a wrong output.
3. The reason for using the COUNTIF function is as you have mentioned , but what your source has not mentioned is that using the COUNTIF is necessitated by the construction of the formula ; if the formula had been constructed differently , the COUNTIF would not be required.
If the formula is revised as follows :
=INDEX($E$5:$E$17,SUM(SMALL(IF($C$5:$C$17="Josh", ROW($C$5:$C$17)-4),2)))
entered as an array formula , using CTRL SHIFT ENTER , then the COUNTIF is eliminated.
The reason for this is that in the first version , we are using the multiplication operator , which will result in zeros ; since we are using the SMALL function , zeros will be taken into consideration , and hence need to be counted.
In the second version which I have posted , we are using the IF function , which will result in numeric values where the condition is satisfied , and FALSE values where the condition is not satisfied. The SMALL function ignores FALSE values , and hence there is no necessity for counting them.
Note that because an IF function is used , even if the SUM function were to be replaced by the SUMPRODUCT function , you would still have to enter the formula as an array formula , using CTRL SHIFT ENTER.
4. The use of the following construct should be avoided at all times.
ROW($C$5:$C$17)-4
The recommended way would be as follows :
=INDEX(NetSales, SMALL(IF(SalesPerson = "Josh", ROW(NetSales) - MIN(ROW(NetSales)) + 1), 2))
If you wish to replace the named ranges by their actual references , the formula would look like this :
=INDEX($E$5:$E$17, SMALL(IF($C$5:$C$17 = "Josh", ROW($E$5:$E$17) - MIN(ROW($E$5:$E$17)) + 1), 2))
The first method would work only if the range starts on row 5 ; if it starts on any other row , the 4 would need to be replaced by the appropriate number.
The second method will work every time , what ever be the range reference.
Note that the formula I have posted avoids the SUMPRODUCT altogether , because , as I mentioned earlier , the use of the SUMPRODUCT function is not warranted when only one index value is to be used. The SUMPRODUCT function has been used only to avoid using CTRL SHIFT ENTER to enter the formula.
Narayan