Excel: Formula to count number of unique values in range that match a condition - excel

Consider the hypothetical scenario that you are given a dataset with animals in column A and their weights in column B. Suppose that we want to count the total number of unique weight values within a certain range for a specific animal. In the example below, I'm wanting to count the total number of unique Dolphin weights between 100 and 200. There are three unique weights: 126, 155 and 171. The duplicate weight of 126 is only counted once. The weight of 90 is ignored because it is not within the range. What custom formula can be used to solve this problem?
*This is for Excel 2016

If you are interested to go with SUMPRODUCT() then you can give a try to-
=SUMPRODUCT(((A2:A11=E2)*(B2:B11>=E3)*(B2:B11<=F3))/(COUNTIFS(A2:A11,A2:A11,B2:B11,B2:B11)))

Adapting Scott's second formula:
=SUM(--(FREQUENCY(IF((A2:A11=E2)*(B2:B11>=E3)*(B2:B11<=F3),B2:B11),B2:B11)>0))
where E2 = "Dolphin", E3 = 100, F3 = 200

Related

Median Selling Price Excel Table

I have a spreadsheet with different products, listing units and retail value sold like the example below
Product Units Value
A 10 100
B 15 80
C 30 560
I'd like to compare the Average Selling Price with the Median Selling price, so I am looking for a quick formula to accurately calculate the median.
The median function requires the entire series, so for Product A above I would need 10 instances of 10 etc. How can I calculate the Median quickly considering the condensed form of my data?
Without writing your own VBA function to do this there are a couple of approaches that can be taken.
The first expands the data from its compressed frequency count format to generate the full set of observations. This can be done manually or formulaically. On the assumption the latter is required, it can be achieved using a few columns.
All the blue cells are formulae.
Column Eis simply the cumulative of column B and F is an adjusted version of this. Column H is just the values 1 to 55, the total number of observations given by cell L2. Column I uses the MATCH() with its final argument as 1 to match each observation in H against the adjusted cumulative in F. Column J uses the INDEX() function to generate the value of the observation. (Observations 1-10 have value 100, 11-25 have value 80 and 26-55 have value 560 in this example). The MEDIAN() function is used in cell M2 with column J as its argument.
This approach can be refined to take account of varying numbers of products and data points through the use of the OFFSET function to control the range arguments of the MATCH(), INDEX() and MEDIAN functions. And, of course, adjacent cells in columns I and J could be combined using a single formula - I've shown them separately for ease of explanation.
The second approach involves sorting the data by value (so in this case the data rows would become Product B in row 2, product A in row 3 and product C left as-is in row 4). It is then a case of identifying the middle observation number (if the number of observations is odd) or the middle pair of observation numbers (if the number of observations is even) and then determining the value(s) corresponding to this/these middle observation(s). In this approach the adjusted cumulative in column F is still used but rather than explicitly calculating the values in column I and J for every observation it can now be restricted to just the middle observation(s).
I think there is no way around compromises. Either using big amounts of helper cells or having the table sorted by the values.
Helper cells:
Formula in F4:AS6:
=IF(COLUMN()<COLUMN($F$4)+$B4,$C4,"end")
Formula in D2:
=MEDIAN(F4:AS6)
Sorted:
Formula in F4 downwards:
=SUM($B$3:B3)+1
Formula in D2:
=SUM(LOOKUP(INT(SUM(B4:B6)/2+{0.5,1}),F4:F6,C4:C6))/2

Assigning the same value to different cells according to the rank of a matching cell's group

I have an Excel sheet that has values repeating in different rows of the same column (Obtain Marks). In RANKS column I want a grade assigned.
My problem is assigning the same rank to cells having the same Obtain Marks values.
How do I assign the same rank to the same Obtained Marks using an Excel formula?
Obtain Marks RANKS
212 1
212 1
212 1
211 2
210 3
209 4
209 4
Assuming Obtain Marks is in A1 and is sorted in order, if you want grouped by rank then I suggest in B2 and copied down to suit:
=RANK(A2,A$2:A$7,)
If you post includes under RANK the desired output then I suggest instead 1 in B2 and in B3 and copied down to suit:
=IF(A2=A3,B2,B2+1)
Either way each group should have a distinct value.
You can derive a pseudo-RANK.UNIQUE formula by modifying an old standard method of a SUMPRODUCT-style COUNT.UNIQUE formula.
        
The formula in B2 is,
=SUMPRODUCT((A$2:A$8>=A2)/COUNTIF(A$2:A$8, A$2:A$8&""))
Fill down as necessary.

Ranking in Excel with multiple criteria

For example, I need to create a merit list of few student based on total marks (column C), then higher marks in math (column B) -
A B C D
-------------------------
Student1 80 220 1
Student2 88 180 3
Student3 90 180 2
Expected merit position is given in column D.
I can use RANK function but I can only do that for one column (total number). If total number of multiple student is equal, I could not find any solution of this.
You can try this one in D1
=COUNTIF($C$1:$C$99,">"&C1)+1+SUMPRODUCT(--($C$1:$C$99=C1),--($B$1:$B$99>B1))
and then copy/fill down.
let me know if this helps.
Explanation
Your first criteria sits in column C, and the second criteria sits in Column B.
Basically, first it is counting the number of entries ($C$1:$C$99) that are bigger than the entry itself ($C1). For the first one in the ranking, you will get zero, therefore you need to add 1 to each result (+1).
Until here, you will get duplicate rankings if you have the same value twice. Therefore you need to add another argument to do some extra calculations based on the second criteria:
To resolve the tie situation, you need to sumproduct two array formulas and add the result to the previous argument, the goal is to find the number of entries that are equal to this entry with $C$1:$C$99=C1 and have a bigger value in the second criteria column $B$1:$B$99>B1:
you add -- to convert TRUE and FALSE to 0s and 1s so that you can multiply them:
SUMPRODUCT(--($C$1:$C$99=C1),--($B$1:$B$99>B1))
the first array is to see how many ties you have in the first criteria. And the second array is to find the number of bigger values than the entry itself.
Note you can add as many entries as you like to your columns, but remember to update the ranges in the formula, currently it is set to 99, you can extend it to as many rows as you want.
Sometimes a helper column will provide a quick and calculation-efficient solution. Adding the math marks to the total marks as a decimal should produce a number that will rank according to your criteria. In an unused column to the right, use this formula in row 2,
=C2+B2/1000
Fill down as necessary. You can now use a conventional RANK function on this helper column like =RANK(D2, D$2:D$9) for your ranking ordinals.
Very simple (or, at least, much more simpler that the one provided by the best answer) 'math' solution: do a linear combination with weights.
Do something like
weighted_marks = 10*colC + colB
then sort weighted marks using simple rank function.
It does solve your problem, bulding the ranking you need.
If you don't like to limit the number of rows or the numbers used in the criteria, Jeeped's approach can be extended. You can use the following formulas in cells D2 to L2, assuming that there are three criteria, the first one in column A, the second one in column B, and the third one in column C:
=RANK($A2,$A:$A,1)
=RANK($B2,$B:$B,1)
=D2*2^27+E2
=RANK(F2,F:F,1)
=RANK($C2,$C:$C,1)
=G2*2^27+H2
=RANK(I2,I:I,1)
=J2*2^27-ROW()
=RANK(K2,K:K,0)
The formulas have to be copied down. The result is in column L. Ties are broken using the row number.
If you like to add a fourth criterion, you can do the following after having the formulas above in place:
Add the new criterion between columns C and D.
Insert three new columns between columns I and J.
Copy columns G:I to the new columns J:L.
Copy column G to column M, overwriting its content.
Change the formula in column L to point to the new criterion.
The factor 2^27 used in the formulas balances the precision of 53 bits available in double-precision numbers. This is enough to cover the row limit of current versions of Excel.

Find all values greater or equal than a certain value

Let's say I have the following table.
Team Score
AA 81
BB 67
CC 44
DD 1.5
JJ 279
LL 49
TT 201
GG 158
MM 32
HH 89
I want to get all teams that scored more than 80 in another table. I tried the Index + Match function as follows but it only gives me the smallest value greater than 80.
Here is the code:
=INDEX($A$2:$A$11,MATCH(80,$B$2:$B$11,-1))
Although I put the values in the lookup_array argument in descending order, this function only gives me one answer: The smallest value greater than 80.
I am trying to do this without having to sort the array and with a huge database mind you. Thank you.
Enter this formula to D2 and press CTRL+SHIFT+ENTER to make it an array formula, then drag it down.
=INDEX($A$1:$A$11,SMALL(IF($B$2:$B$11>=80,ROW($B$2:$B$11)),ROW(1:1)))
See How to look up a value in a list and return multiple corresponding values for details.
How about using an AutoFilter? You're then quite flexible in filtering for values in column score.
If you don't want to use vba, in a third column you could have
=if(B2 > 80, A2, "")
and copy that formula down. You could even put this column of formulas in another tab and either delete the rows that have nothing in them manually or write code that will do that for you.
Create a PivotTable (Team for ROWS, Score for Sigma VALUES, then filter the Sum of Score column for Number Filters, Greater Than..., and 80 next to Show rows where: is greater than *, OK.
* or perhaps is greater than or equal to.

Excel function for ranking duplicate values

I have an excel sheet containing two columns of data that I'd like to rank.
Suppose we have the following:
A B
Franz 58
Daniel 92
Markus 37
Jörg 58
I would like a formula to rank the above data based on column B, and where there are duplicate values (Franz and Jörg) to put the alphabetical name first. What I have at the moment is simply duplicating Franz twice:
=INDEX(Name,MATCH(A2,Points,0))
Can someone advise me of formula / code that will rank the data and arrange duplicate values alphabetically?
Thanks
I would add a helper column in next to your data to help out with ties.
so in column C use
=B1+1/COUNTIF($A$1:$A$4,"<="&A1)/10
This will add on a decimal ranking system based on the name. This assumes that your numbers in column B do not have decimal places, if they do then you will need to increase the 10 on the end of the formula to account for it ie: for 2 decimal places use 1000, 3 : 10000 etc
Use this formula to get the first name
=INDEX(name,MATCH(LARGE(points,1),points,0))
adjust the 1 to 2 for the second name etc
EDIT had the sign around the wrong way
This will rank your data and will not repeat duplicates too:
In C2:
=SUM(1*(b2>$b$2:$b$5))+1+IF(ROW(b2)-ROW($b$2)=0,0,SUM(1*(b2=OFFSET($b$2,0,0,INDEX(ROW(b2)-ROW($b$2)+1,1)-1,1))))
CTRL+SHIFT+ENTER to turn it into an array
Drag these down to C5 and it will not duplicate rank where the name is the same, it will rank them alphabetically if they are the same.
Then if you wanted to order them automatically in order of top performer/score you then do this:
Putting this in E2:
=INDEX(A2:A5,MATCH(LARGE(C2:C5,ROW()-1),C2:C5,0))
...and drag down
Then use a vlookup on your data to return the score putting this in F2:
=vlookup(E2,A2:C5,2,false)
...and drag down
This should give you a table of highest scoring people in score order.
Assuming A2 is the first of the ranked points scores try this version
=INDEX(Name,SMALL(IF(A2=Points,ROW(Points)-MIN(ROW(Points))+1),COUNTIF(A$2:A2,A2)))
confirmed with CTRL+SHIFT+ENTER and copied down
Requires the Name list to be sorted because names with duplicate scores will be listed in the order shown

Resources