I've been googling for a while being unable to find my answer.
I'm trying to get a single cell formula that returns with the Highest Sum of a listed item. So for example I have:
Value Name
4 James
5 James
1 Kari
50 (Blank)
7 Kari
3 James
Kari's has 8 days total, James has 12 total, and "Blank" is discarded. I'd like the formula to return just "12". I can get this to work using Max(), and Sumifs(), but that requires additional fields and formulas. Is there a clean way to do this in one in a single cell formula?
Use this as an array formula:
=MAX(SUMIFS($A$2:$A$7,$B$2:$B$7,$B$2:$B$7,$B$2:$B$7,"<>"))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
Related
Suppose you have an ordered, indexed list of positive values. These positive values are interrupted by 0 values. I want to determine if a consecutive sub-array exists which is not interrupted by 0 values and whose sum exceeds a certain threshold.
Simple example:
Index, Value
0 0
1 0
2 3
3 4
4 2
5 6
6 0
7 0
8 0
9 2
10 3
11 0
In the above example, the largest consecutive sub-array not interrupted by 0 is from index 2 to index 5 inclusive, and the sum of this sub-array is 15.
Thus, for the following thresholds 20, 10 and 4, the results should be FALSE, TRUE and TRUE respectively.
Note I don't necessarily have to find the largest sub-array, I only have to know if any uninterrupted sub-array sum exceeds the defined threshold.
I suspect this problem is a variation of Kadane's algorithm, but I can't quite figure out how to adjust it.
The added complication is that I have to perform this analysis in Excel or Google Sheets, and I cannot use scripts to do it - only inbuilt formulas.
I'm not sure if this can even be done, but I would be grateful for any input.
Start with
=B2
in c2
then put
=IF(B3=0,0,B3+C2)
in C3 and copy down.
EDIT 1
If you were looking for a Google sheets solution, try something like this:
=ArrayFormula(max(sumif(A2:A,"<="&A2:A,B2:B)-vlookup(A2:A,{if(B2:B=0,A2:A),sumif(A2:A,"<="&A2:A,B2:B)},2)))
Assumes that numbers in column B start with zero: would need to add Iferror if not. It's basically an array formula implementation of #Gary's student's method.
EDIT 2
Here is the Google Sheets formula translated back into Excel. It gives you an alternative if you don't want to use Offset:
=MAX(SUMIF(A2:A13,"<="&A2:A13,B2:B13)-INDEX(SUMIF(A2:A13,"<="&A2:A13,B2:B13),N(IF({1},MATCH(A2:A13,IF(B2:B13=0,A2:A13))))))
(entered as an array formula).
Comment
Maybe the real challenge is to find a formula that works both in Excel and Google sheets because:
Vlookup doesn't work the same way in Excel
The offset/subtotal combination doesn't work in Google sheets
The index/match combination with n(if{1}... doesn't work in Google sheets.
With data in columns A and B, insure column B end with a 0. Then in C2 enter:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-MAX($C$1:C1),"")
and copy downwards:
Column C lists the sums of consecutive non-zeros. In another cell enter something like:
=MAX(C:C)>19
where 19 is the criteria value.
You can avoid the "helper" column by using a VBA UDF.
EDIT#1:
Use this instead:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-SUM($C$1:C1),"")
Thanks to #Tom Sharpe and #Gary's Student for answering the question.
While I admittedly did not specify this in the question, I would prefer to achieve the solution without a helper column because I have to do this operation on 30+ successive columns. I just didn't think it was possible in Excel.
Full credit goes to user XOR LX on the Excelforum for coming up with this solution. It has blown my mind and took me the better part of an hour to wrap my head around, but it is certainly very creative. There is no way I could have come up with it myself. Re-posting it here for the benefit of everyone who is looking into this.
Copy and paste the table from my initial question into an empty Excel sheet such that the headers appear in (A1:B1) and the values appear in (A2:B13).
Then enter this formula as an array formula (ctrl+shift+enter), which gives the max of the sums of all the uninterrupted sub-arrays:
=MAX(SUBTOTAL(9,OFFSET(B2,A2:A14,,-FREQUENCY(IF(B2:B13,A2:A13),IF(B3:B14=0,A2:A13,0))-1)))
Note the deliberate offset to include one additional row below the end of the dataset.
I have a spreadsheet that looks like follows:
On the lefthand side, column A, I have Bug numbers that corresponds to a specific bug in our Bugzilla.
I am trying to find a way to place, in a single cell, the amount of Bugs that have duplicates, each of them only counted once.
For example, say I have this:
Col A
1
1
2
3
3
3
4
4
5
I would like to have the formula return me 3 because I have 3 numbers that have duplicates, but each of them only counted once.
Use COUNTIF and SUMPRODUCT:
=SUMPRODUCT((COUNTIF($A$1:$A$9,$A$1:$A$9)>1)/COUNTIF($A$1:$A$9,$A$1:$A$9))
To deal with blanks we need to use SUM(IF()) in an array:
=SUM(IF(((COUNTIF($A$1:$A$9,$A$1:$A$9)>1)*($A$1:$A$9<>"")),1/COUNTIFS($A$1:$A$9,$A$1:$A$9,$A$1:$A$9,"<>")))
being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
I need to find the number of times a name appears on this list but I only want to count one instance of the name one time for each row without counting the duplicate name in the row.
For instance: I have the following in a range...
Red Bill Jack Ruby Bill
Blue Ruby Ivan Raul Ted
Green Ted James Rick Ted
Red Ted Phil Ruby Bill
And in this worksheet, I want to count the number of instances of the name Bill and get the answer of 2 because Bill's name shows up in two rows. In the same respect, If I choose to count the name Ted, the answer should be 3 because Ted's name shows up in three rows.
Use OFFSET with SUMPRODUCT. In F7 (per supplied image) as,
=SUMPRODUCT(SIGN(COUNTIF(OFFSET(B$1:E$1, ROW($1:$4)-1, 0), E7)))
The SIGN function turns any positive number to 1 for each row. ROW(1:4) cycles through each of the rows.
Assuming you put your name of choice, e.g. "Ted", in G1, array formula**:
=SUM(0+(MMULT(0+(A1:E4=G1),TRANSPOSE(COLUMN(A1:E4)))>0))
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
A non-vba solution with a helper column in column F.
Column F formula =IF(COUNTIF(B1:E1,$I$1)>0,1,0)
J2 formula =SUM(F1:F4)
change I1 as needed for name.
New to advanced excel concepts, I have a list of numbers such as:
101 02/22/2016
100 02/21/2016
and then another list like so:
101 01/01/2016 Apple
101 02/20/2016 Banana
100 02/21/2016 Apple
100 02/23/2016 Banana
I'm trying to get it where I use a vlookup with the number on the more basic table, check for a match on the advanced table then find the date on the advanced table that is closest to the date on the basic table, then return the value to the right (Banana, Apple).
I've got the vlookup part down, but placing an if statement just returns N/A and breaks every time.
I've also tried using this guide: http://eimagine.com/say-goodbye-to-vlookup-and-hello-to-index-match/
You can use this array formula:
=INDEX($C$1:$C$4,MATCH(MIN(IF($A$1:$A$4=E1,ABS($B$1:$B$4-F1))),IF($A$1:$A$4=E1,ABS($B$1:$B$4-F1)),0))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
In my sheet I have a Column1 with text and a Column2 with random numbers.
I would like to get a Column3 with the five "highest texts" for example:
I know I can get the text if I make an Index/Match looking for the number, but I'm lost at the sorting part.
Place this in cell C1 and drag down:
=LOOKUP(REPT("z",255),IF(LARGE($B$1:$B$10-(ROW($B$1:$B$10)/9^9),ROWS($A$1:A1))=$B$1:$B$10-(ROW($B$1:$B$10)/9^9),$A$1:$A$10))
This needs to be entered with CTRL + SHIFT + ENTER.
Use a 'helper column' to get the top 5 numbers with the LARGE function then use INDEX on column A, returning the appropriate row number with AGGREGATE and COUNTIF function to offset duplicates.
additional variant to already posted:
=INDIRECT("A"&MATCH(LARGE(B:B,ROW(A1)),B:B,0))
test:
The question is: What if there is not a clean break between 5th and 5th place? For example what if f was also a 6? This would then be tied for with the others how does one descriminate?
The following will bring out only the 1st through the 5th but allow for ties that might push the actual number past 5 due to ties.
Put the following in C2:
=IF(OR(ROW(1:1)<=5,COUNTIF($C$1:$C1,INDEX($A$1:$A$10,MATCH(LARGE($B$1:$B$10,ROW(1:1)),$B$1:$B$10,0)))>0),INDEX($A$1:$A$10,MATCH(1,INDEX(($B$1:$B$10=LARGE($B$1:$B$10,ROW(1:1)))*(COUNTIF($C$1:$C1,$A$1:$A$10)=0),),0)),"")
It is an array formula and must be confirmed with Ctrl-Shift-Enter. Then copy it down ten rows(in the picture the formula is copied down ten rows).
Now we change f to be 6 so it is tied for fifth:
And it gets added to the list.
You could just limit the output to five rows. But then it would be excel deciding what is the top five. If this is what you want than any of the other answers will work.