Using Max function result in SumIFS Excel - excel-formula

I have a table like the image given above. There is another sheet which reports some information based on this data. I need the sum of Apples in the last week for a given month.
The answer for apples for 8 th month would be 14 assuming there is another record as Apples - 32 - 10 - 8.
So I would have to find max week number for a given month and then sum the quantity for Apples with max week number
The formula written is
=SUMIFS( C2:C300, A2:A300, H16, B2:B300, MAX(IF(D2:D300=MONTH(TODAY()-1),B2:B300)))
H16 contains the word "Apples"
Column A - Item
Column B - Week
Column C - Quantity
Column D - Month
Column H - Unique Items again..
MAX(IF(D2:D300=MONTH(TODAY()-1),B2:B300)) returns the correct max as 32 for month 8 but the formula returns the value as 0. If I put the max formula in aseperate cell and refer that cell in main formula, it works as expected. I am not sure why the result of max value is not being used for sumif function.

Your formula is good. all you need to do is to convert it into Array Formula. Just type the formula with Ctr + shift + enter.
{=SUMIFS( C2:C300, A2:A300, H16, B2:B300, MAX(IF(D2:D300=MONTH(TODAY()-1),B2:B300)))}
Actually, without array, the part IF(D2:D300 evaluates to #value error as Excel doesn't not know how to compare an array against one value.
Hint: Not sureif you know it already, but anyways, always use Formulas --> Evaluate Formula to pin-point an error in a formula.

Related

Average Offset Index Match formula

I'm trying to average the most recent 6 values of 'x' - however, I'm getting reference errors when trying to average them. The value I'm looking for should be 8,720.33 with the criteria of 'x'. The dates have no bearing in the formula, just a visual reference point.
Formula I'm getting lost on "=AVERAGE(OFFSET(INDEX(C2:C42,MATCH(E2,B2:B42,0),),0,0,-6))"
*Note - the dates are being updated daily to include historical data points
Data Set:
You can try following array formula:
=AVERAGE(INDEX(C1:C25,N(IF(1,AGGREGATE(14,6,ROW(A1:A25)/(B1:B25=E2),ROW(A1:A6)),0))))
or with full ranges:
=AVERAGE(INDEX(C:C,N(IF(1,AGGREGATE(14,6,ROW(A:A)/(B:B=E2),ROW(A1:A6)),0))))
Confirm it with ctrl + shift + enter
You might be able to pull this off with AVERAGEIFS if you add a rank column that ranks Header1 by date.
In my example:
Column D is the Values you are averaging
Column B is Hearder1
Column C is the rank - =COUNTIFS($B:$B,B2,$A:$A,">"&A2)+1
=AVERAGEIFS($D:$D, $B:$B, "x", $C:$C, "<7")

How to calculate moving average 7 days of milk production of a each cow, Dates not sorted. Excel

The daily milk production of each cow is recorded daily. I would like to cancel out the noise. Therefore I would like to use a moving average of seven days ( 3 days before and 3 days after, including current date production) in Excel.
The data is not ordered according to date therefore offset function cannot be used.
Additional Information: "Date" is in Column A, Cow No in Column B and Milk in Column C . Rows are from 1-22.
The expected result would be get get a moving average of milk for 7 days of each cow ( -3 days before <=current date and < days+3 days after current date from each cow.
Similiar function used to calculate rolling average of past year is =AVERAGEIFS(B2:K2,B1:K1,">"&TODAY()-365,B1:K1,"<"&TODAY()). A similiar formula could be used.
Problem when using sumproduct formula: No Data for 7 days should be 0 7D Average. However is given 1.79 .Rolling AVerage with the Sumproduct formula Problem
You can try to use a combination of SUMPRODUCT and AVERAGE functions. Note that this is an array formula, so you need to enter it using Ctrl+Shift+Enter keys:
=SUMPRODUCT(AVERAGE(IF(($B$2:$B$22=B2)*($A$2:$A$22>=(A2-3))*($A$2:$A$22<=(A2+3)),$C$2:$C$22)))
Result:
The average for the first three entries (D2:D4) is exactly the same as the value in column C, as there are no other dates provided for the range between 23-29 January. The remaining values (D5:D22) should properly present the 7-day average values.
This may not be the pretiest solution but it works. We can use countifs in the following way:
1. Create a column that combines Cow no and Date (I called it CowNoDate) with the format CowNo + "-" + date(as number). I put it in column C.
2. Sort list by this new column
3. use the following formula in column E to calculate the running average:
=SUMIFS(D:D,C:C,">="&B2&"-"&(A2-3),C:C,"<="&B2&"-"&(A2+3))

Excel - function to find the highest sum in a table using each row and column only once

I've got a table in excel with 10 rows and 10 columns.
The table contains 100 different values between 1 and 3.
I want to find the highest sum of 10 values using only 1 value from each row and 1 from each column.
Do u guys know a function that finds the highest sum? - I've tried to do i manually, but there are to many combinations!
Hope it makes sense.
Thanks in advance:)
My solution builds on what I wrote in the comment, i.e. you first take the maximum value in the 10x10 array, then the maximum in the 9x9 array (excluding the row/column of the first maximum), etc. My solution tries not to do everything in one formula, but I add a few helper columns, and a bit more helper rows (it is fast and dirty, but it works and is easily audited/understandable). You always can do this on a separate worksheet which you could hide if needed.
The screenshot above goes from cell A1 till Y31.
The key formulas:
3.55 is the result of =MAX(B2:K11)
The first gray cell is =IFNA(MATCH($M12;B2:B11;0);""), and you drag this 9 cells to the left. This tries to find a match with the max result in each column of the table;
The 10 left of the 3.55 is =MATCH(TRUE;INDEX(ISNUMBER(P12:Y12);0);0) , and gives the column number of the max value.
The 2 next to the 10 is =INDEX(P12:Y12;N12) and gives the row number of the max value.
The 1 in cell B12 is =IF(OR(B$1=$N12;$A12=$O12);0;1), and creates a 10x10 matrix with a row and column with zeroes where the previous max value was found.
Then you multiply this with the preceding matrix and create a new 10x10 matrix below (enter {=B2:K11*B12:K21} array formula (ctrl+shift+enter) in B22-K31
You then copy/paste rows 12 till 31 9 times below
The 23.02 is the total sum =SUM($M$12:$M$211) from all 10 maximum values and is the result you are looking for. The 10 is just a check with =COUNT($M$12:$M$211)

Formula returning Column A value for row containing MAX value of a range

Assume I have the following table:
A B C
1 Week 1 Week 2
2 Melissa 114.7 82.8
3 Mike 105.5 122.5
4 Andrew 102.3 87.5
5 Rich 105.3 65.2
The names are in column A, the Week values are in Row 1. (So A1 is blank, B1 = Week 1, and A2 = Melissa.)
I'm trying to build a formula that looks at all the values in a known range (in this example, B2:C5), chooses the highest value of the bunch (here, 122.5) and returns the name of the person from Column A that got that value. If I use this formula, it works for the values in range B2:B5:
=INDEX(A2:A5,MATCH(MAX(B2:B5),B2:B5,0))
That returns Melissa but if I expand the range to include more than just column B's values, I get an #N/A returned:
=INDEX(A2:A5,MATCH(MAX(B2:C5),B2:C5,0))
The weird part (to my simple brain) is that the MATCH portion of the formula works fine, if I just put in this formula, it returns the highest value of 122.5 from C3:
=MAX(B2:C5,B2:C5,0)
So clearly something it going wrong when I'm using either the MATCH or INDEX commands.
Hopefully this makes sense and someone can point out my error?
Try this:
=INDEX(A:A,MAX((B2:C5=MAX(B2:C5))*ROW(B2:C5)))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Note: Match can only search one vector at a time. It can be one row or one column or one array. It cannot be two or more rows or columns or a 2D array.
Do it "twice"? Please try:
=INDEX(A2:A5,IFERROR(MATCH(MAX(B2:C5),B2:B5,0),MATCH(MAX(B2:C5),C2:C5,0)))
If you are going to have up to 52/53 weeks to cope with I'd suggest instead inserting a helper column with the MAX for each row. Make that an new (inserted) ColumnA (say =MAX(C2:BC2) etc.) and a simple VLOOKUP should serve, say:
=VLOOKUP(MAX(A:A),A:B,2,0)

PercentileIF Excel (or rangeif)

A B
1 5
2 10
2 15
3 20
I want to calculate percentile for a column of values B if A is equal say 2. That's I want to get range of B2,B3 and calculate percentile of this.
So basically the question is: how do I select range in one column with the checking with another column?
I.e. it works perfectly with SumIf and CountIf, I just need the same with PercentileIf. Thx!
This will give you the 25th percentile of A1:A6 for all cells where the value in B1:B6 equals 2:
=PERCENTILE.INC(IF(B1:B6=2,A1:A6,""),0.25)
It's an array formula and must be entered with Ctrl-Shift-Enter.

Resources