Second Largest Value with criteria in excel - excel

I'm trying to find the second largest value of a given criteria in excel. So the formula I was trying to use was
=LARGE(IF($B$1:$B$216=1,$F$1:$F$216,""),2).
The problem is that it is returning the second largest overall value, but I want the second largest value where column B is 1. Better Answers would be helpful.

You are using an array (CSE) formula and you did not enter it the key combination for array formuas. Edit the cell again and click Ctrl+Shift+Enter.
Or if you want a normal (non CSE) formula, use this:
=AGGREGATE(14,6,$F$1:$F$216/($B$1:$B$216=1),2)

Related

How to find the maximum value based on another column?

I am trying to find the maximum value based on another column. This is what I have tried.
=MAX(IF(Sheet3!$B$2:$B$5491=Sheet4!A3,Sheet3!$E$2:$E$5491))
How I am reading this code is that if the criteria in sheet 3 in column B is equivalent to the cell in A3 in sheet 4, then return the maximum value that would be in column E from sheet 3. I found this code online and have tried variations but all it is doing is returning 0. Is there another way I could go about finding the max value?
Thanks,
GCC
It's an Array formula (aka CSE formula). After entering or editing it, you should press
CtrlShiftEnter
otherwise it won't work. Always remember this for array formulas.

Using a vlookup or index match inside a sumproduct function

I have two 2-D arrays that relate to engines. The first table classifies an Engine as either a or as b. The second table has a numerical count value for each of the engines.
I want to calculate the number of engines for each label. Here, label a has a count of 40 and label b has a count of 300.
I thought the following formula would work...
=SUMPRODUCT(
--(E3=VLOOKUP(A11:A17,A2:B8,2,0)),
B11:B17
)
...but it does not!
Why can't I use a vlookup inside a sumproduct array formula? Index-match also doesn't work.
I can't append a new column to the second table with a vlookup forumla that references the first table.
Use LOOKUP:
=SUMPRODUCT(($B$2:$B$8=E3)*LOOKUP($A$2:$A$8,$A$11:$A$17,$B$11:$B$17))
To do it with unsorted data use this array formula:
=SUM(SUMIF($A$11:$A$17,IF($B$2:$B$8=E3,$A$2:$A$8),$B$11:$B$17))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.

Count occurrences of values in a specific range (Excel) (no VBA)

I need to count all of the occurrences of a given value from a specific range of cells (containing strings or numbers), depending on a parameter stored in another cell.
I prepared a simple Excel table as an example (see attached image): let's say I want to count all of the occurrences of the VALUE "4" for the BASE "100". The result should be: 2 (C4 + C5).
Attached image
I tried to use COUNTIFS and FIND functions but with no results. The former only considers exact values (so the 4 in cell C5 will be ignored) while I seem to be unable to add another condition - the BASE column - to the latter.
Fact is I need to solve this with formulas only, no programming.
Thanks in advance for your help!
Use the SUMPRODUCT:
=SUMPRODUCT(($B$2:$B$10=100)*(ISNUMBER(SEARCH(4,$C$2:$C$10))))
There's a couple of other approaches, the simpler one is just to add another column which identifies matches for you, then have your count just sum the results of that column.
Solution image
So we put the values we want to find in some reference cells, the BASE match goes in G2, and the VALUE we're looking for goes in G3.
In column D we put a formula in D2:
"=IF(B2=$G$2,IF(ISERR(SEARCH($G$3,C2)),0,1),0)"
Returns 0 if the BASE matches and we can find at least one occurent of VALUE
B2=$G$2 - Does the BASE column match the BASE we're looking for
ISERR(SEARCH($G$3,C2)) - Does searching for the VALUE return an error (if it does, we know that VALUE isn't there)
Copy this formula to all the cells in column D, and then you can just use a simple SUM(D:D) to count the occurences where your conditions are met.
The neater but slightly more complex alternative is to use an array formula to do the match finding and counting all in one formula. This would look like this:
"{=SUM(IF(B:B=$G$2,IF(ISERR(SEARCH($G$3,C:C)),0,1)))}"
Pretty much the same as the formulas in column D, but now we use B:B and C:C in place of B2 / C2 etc. and stick the SUM around the whole thing. If you finish editing with Ctrl+Shift+Enter instead of just Enter, that'll make it an array formula.
Microsoft Array Formula Guidelines
NB: this WILL NOT count multiple occurences of 4 in a single VALUE cell.
p.s. Assuming you would want it to actually return 3 in this case (you missed the 4 in C7)

Counting how many cells have a higher value than the cell below them (COUNTIF and OFFSET)

I am trying to count how many cells in a particular column have a higher value than the cell below them. This is how far I've gotten, but it doesn't work (it always returns 0, even if there is at least one cell bigger than the one below it):
=COUNTIF(B4:B500,">"&OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),1,0))
Background (in case anyone is wondering): I am using this to check if the values in this column are properly sorted (i.e. starting with the lowest value and moving down to the highest). If the formula returns a value greater than 1, I know that the list is not fully sorted.
Use the SUMPRODUCT:
=SUMPRODUCT(1*(B4:B16>B5:B17))
you could also use an array formula.
{=SUM(N(A1:A13<A2:A14))}
which will check your logical condition for each cell in the array, then convert it to either 0 or 1 by using N() function and then sum it up. i prefer this approach because, unlike SUMPRODUCT(), you can use it with other functions than SUM.
please remember that after inserting an array formula into a cell, you must confirm it by pressing CTRL+SHIFT+ENTER.
=COUNTIF(B5:B16,">"&B4) then copy

Microsoft Excel - for each unique value in column A, return max value from column C and the corresponding value from B

using a formula, I'm looking to get the max of column C for each unique value in column A then I'd like to also return the corresponding value from column B to the max of column C. the output should be a summary of the data as in the second screen capture. I've tried pivot tables, conditional formatting and the help in this link with no luck Get the maximum values of column B per each distinct value of column A
original data
desired result
Is this possible with formulas?
So for the Max number use the AGGREGATE() Function:
=AGGREGATE(14,6,$C$2:INDEX(C:C,MATCH(1E+99,C:C))/($A$2:INDEX(A:A,MATCH(1E+99,C:C))=I2),1)
Then to find the Capacity a simple SUMIF():
=SUMIFS(B:B,A:A,I2,C:C,K2)
The AGGREGATE() Function was introduced in Excel 2010.
For other versions, the less robust Array Formula MAX(IF()) will work in place of the AGGREGATE:
=MAX(IF($A$2:INDEX(A:A,MATCH(1E+99,C:C))=I2,$C$2:INDEX(C:C,MATCH(1E+99,C:C))))
Being an Array Formula it must be confirmed with Ctrl-Shift-Enter when exiting Edit mode. When done correctly Excel will automatically put {} around the formula to denote an array formula.
Another, If you have the latest Office 365 or using the online app the following non CSE MAXIFS() will work also:
=MAXIFS($C$2:INDEX(C:C,MATCH(1E+99,C:C)),$A$2:INDEX(A:A,MATCH(1E+99,C:C)),I2)
These two are not as robust as the AGGREGATE() in that they will break if there are errors in the data. AGGREGATE() will ignore them.
Here's another option using Array formula, in a Table. I didn't do this starting at A1 in my sheet, so the image has my cell/row IDs to assist!
Cell M17: =IFNA(INDEX([Host], MATCH(0, COUNTIF($M$16:M16, [Host]), 0)),"Unknown") +Ctrl+Shift+Enter
Cell N17: =MAX(IF([Host]=[#UniqueHost],[Duration])) +Ctrl+Shift+Enter
Cell O17: =IFNA(INDEX([Capacity],MATCH([#UniqueHost]&[#MaxDuration],[Host]&[Duration],0)),0) +Ctrl+Shift+Enter

Resources