Dynamic sum column in SUMIFS - excel

I have month columns and criteria for rows (Division, measure) and need a way to sum all divisions for a given measure and only return for the reporting month.
INDEX(MATCH) does not work because I need it to sum all Dec-18 absence values, but there are other measures in the columns as well.
My current iteration (array):
=SUM(OFFSET(D1:D53,,MATCH(Y3,$D$2:$P$2,0)))
But I can't get this to change the summing column based on the month selected.
My last guess is that I need to swap division and month (so division column headers, month rows), but I'd rather not if I'm missing something obvious.
Example:
Department | Measure | Nov-18 | Dec-18
Sales | Absence Hours | 3.5 | 4.6
Manu | Absence Hours | 6.2 | 1.7
Sales | Hours worked | 1000 | 976

An alternative solution that does not use array like calculation nor volatile functions.
=SUMIF($B$2:$B$4,$F2,INDEX($C$2:$D$4,0,MATCH(G$1,$C$1:$D$1,0)))
In the event that either your criteria or date is not found an error will be displayed. You can deal with this by wrapping the whole thing in an IFERROR function and then choose your own error message, display blank or return 0.
=IFERROR(SUMIF($B$2:$B$4,$F2,INDEX($C$2:$D$4,0,MATCH(G$1,$C$1:$D$1,0))),"NOT FOUND")
Also, if you wish to further breakdown your results but "department" and by "measure", then you could us SUMIFS which allows you to set multiple criteria instead of 1

Maybe something like this would work?
=SUMPRODUCT((B3:B5=G3)*OFFSET(B3:B5;0;MATCH(G2;C2:D2;0)))

Related

How can I get an average formula that omits errors

I'm trying to get the average of four data points.
The problem is that one or more data points could be missing.
The average should be the average of the last four mondays or four last tuesdays etc.
Each data point is about 1000 rows apart so my idea was to "list" the dates needed and use vlookup and average.
Generic formula
// I only add two dates, but the same formula is repeated for four dates
=AVERAGE(VLOOKUP(DATE_1;Table;25;FALSE);VLOOKUP(DATE_2;Table;25;FALSE))
The DATE_1 and DATE_2 is dynamic calculations of the previous two, lets say mondays.
This works if all dates are there, but if one monday is missing VLOOKUP returns an error and the error can't be calculated as an average.
I figured I could wrap VLOOKUP with IFERROR, but I can't get that working either
// for simplicity I removed the average and only show one.
IFERROR(VLOOKUP(DATE_1;Table;25;FALSE);"") // returns empty string, can't calculate
IFERROR(VLOOKUP(DATE_1;Table;25;FALSE);0) // Works, but it skews the result with a zero.
I know AVERAGE skips empty cells, but how can I "emulate" a empty cell. "" is empty string and that is not the same.
Is there formula that can handle errors and still give me the average, or a formula that returns "empty cell"?
This is the whole point of the AGGREGATE function.
Instead of AVERAGE(SomeRange), use AGGREGATE(1, 6, SomeRange). Instead of AVERAGE(Value1, Value2), use AGGREGATE(1, 6, Value1, Value2)
The 1 tells AGGREGATE to calculate the AVERAGE, and the 6 tells it "Ignore error values". A full list of the values is at the bottom of this post
=AGGREGATE(1,6,VLOOKUP(DATE_1;Table;25;FALSE);VLOOKUP(DATE_2;Table;25;FALSE))
(As people have pointed out, this doesn't quite work properly without interim calculation cells - when you use a Formula in the function, Excel refuses to accept it in Reference Form)
Reference form: AGGREGATE(function_num, options, ref1, [ref2], …)
Array form: AGGREGATE(function_num, options, array, [k])
Function_num | Function
1 | AVERAGE
2 | COUNT
3 | COUNTA
4 | MAX
5 | MIN
6 | PRODUCT
7 | STDEV.S
8 | STDEV.P
9 | SUM
10 | VAR.S
11 | VAR.P
12 | MEDIAN
13 | MODE.SNGL
14 | LARGE
15 | SMALL
16 | PERCENTILE.INC
17 | QUARTILE.INC
18 | PERCENTILE.EXC
19 | QUARTILE.EXC
Option | Behaviour
0 | Ignore nested SUBTOTAL and AGGREGATE functions
1 | Ignore hidden rows, nested SUBTOTAL and AGGREGATE functions
2 | Ignore error values, nested SUBTOTAL and AGGREGATE functions
3 | Ignore hidden rows, error values, nested SUBTOTAL and AGGREGATE functions
4 | Ignore nothing
5 | Ignore hidden rows
6 | Ignore error values
7 | Ignore hidden rows and error values
Just giving you a different approach even though it is not exactly the same way as your question is going, I just thought to share how i've solved a similar issue.
No lookup table in this one, I personnally try to avoid these in these situations, as you always have to update them given some conditions.
{=AVERAGE(IF((WEEKDAY(A1:A276,2)=1)*((L1:L276)>0)*((A1:A276)>((TODAY())-29)),L1:L276,""))}
array formula, so ctrl+shift+enter
(WEEKDAY(A1:A276,2)=1) tests if it's a monday
(L1:L276)>0) is where I have values and therefore want to ignore zeros
((A1:A276)>((TODAY())-29)) added this one for you to check if it's less than 4 weeks old
if these conditions are fulfilled the respective value in L:L is take for the average (A:A being the date in this example)
Here's one more solution for you to try (array formula - Ctrl+Shift+Enter):
=AVERAGE(IF(ISNUMBER(MATCH($A$1:$A$10,CHOOSE({1,2,3,4},DATE_1,DATE_2,DATE_3,DATE_4),0)),$B$1:$B$10))
Result:
INDEX & MATCH doesn't produce the same problem, try this:
=AVERAGE(INDEX(YourColumnRange;MATCH(DATE_1;Table));INDEX(YourColumnRange;MATCH(DATE_2;Table)))
EDIT: To match your dataset you can manually calculate an average like this:
=SUM(IFERROR(INDEX(Y:Y,MATCH(Date_1,A:A,0)),0), IFERROR(INDEX(Y:Y,MATCH(Date_2,A:A,0)),0))/
COUNT(INDEX(Y:Y,MATCH(Date_1,A:A,0)), INDEX(Y:Y,MATCH(Date_2,A:A,0)))
This will allow you to skip empty rows. The formula is simplified for a generic case to make it easier to read.

Excel - Rather complex SUM IF criteria

I have roughly the following setup:
Values:
MONTH | DURATION | VALUE |
5 | 3 | 120 |
6 | 1 | 100 |
Expected outcome for totals:
MONTH | TOTAL
5 | 120
6 | 220
7 | 120
What I would like to do, is to be able to sum in another table the total values for each month. The logic would be to SUM every value where the total table's month is equal or higher than that of the values, but lower than the value's month + duration.
Does that make any sense? Is that possible? I'm cracking my head and I can't seem to find a way to solve it.
Thank you very much.
The easiest solution is probably to make another column with the end month. And then use SUMIFS to check if month is >= starting month and <= ending month.
=SUMIFS(<Range of Values>,<Range of starting>,"<="& "Target month",<Range of ending>,">="& <Target month>)
DSUM is the best candidate for this, I believe. See Microsft's documentation and this site for help understanding function, and what it is doing. I have made very complex calculations possible in Excel by using the "database" methods (DSUM, DCOUNT, etc).

How to select/filter rows in MS Excel based on cell value

I'm working on an excel sheet which has a simple structure like this.
Date | Order
-----------------------
08-15-2014 | 84
08-16-2014 | 50
08-17-2014 | 68
08-17-2014 | 78
08-18-2014 | 23
Here, in a separate column I'd like to calculate the sum of Orders by week days(Sunday, Monday and so on). Meaning, I want to see how many Orders received on Sunday, Monday etc.
So far I have come up with this non working formula.
=SUM(LOOKUP(1, WEEKDAY(date_column), order_column))
This was example for summing all order received on Sunday. And, yes Date column is in DateTime Data type so that shouldn't be a problem.
Thanks.
Consider adding a new column after the dates, representing the day values. So in column B you might have something like =TEXT(WEEKDAY(A2),"ddd").
Then if you wanted to sum up order numbers by day, you could use an equation like =SUMIF(B1:B5,"Sun",C1:C5) where column B has the day values and column C has the order numbers.
To show an example (with headers, etc):

using SMALL function to retrieve lowest value in range, trying to combine with OFFSET to get value to the left

I am using the SMALL formula in H2 to find the lowest price in the row, which works great.
=IF(ISERROR(SMALL(A2:F2,COUNTIF(A2:F2,0)+1)),"",SMALL(A2:F2,COUNTIF(A2:F2,0)+1))
I am having trouble retrieving the value to the left of it (its corresponding item#) with OFFSET.
A | B | C | D | E | F | G | H
item# | price | item# | price | item# | price | lowest value item# | lowest value
123 | 70 | 456 | 80 | 789 | 67.89 | ? | 67.89
Also,I do not know which column will have the lowest value, A-F can change.
I've spent several hours searching and tried using the original formula as the Reference part for the OFFSET:
=OFFSET(IF(ISERROR(SMALL(A2:F2,COUNTIF(A2:F2,0)+1)),"",SMALL(A2:F2,COUNTIF(A2:F2,0)+1)),0,-1,1,1) and variations of it. This returns #Value!
Am I on the right track? Is OFFSET the correct way to do this? Thanks
I would also remove the dependency on H2 by replacing it thus, simply adding a MIN function
=INDEX(A2:F2;MATCH(MIN(A2:F2);A2:F2;0)-1)
however, you always need to distinguish between item number and price. Sometimes the item value could be lower than price and then your formula wouldn't work. It would return the item value as the lowest value and then will return the price for the preceding item. It would be a mess.
So to resolve this, you need to add two MATCH conditions to look for the exact match:
here is the formula the LOWEST VALUE ITEM#:
=INDEX(A2:F2;(MATCH(MIN(IF(A1:F1="Price";A2:F2));A2:F2;0)-1)*(MATCH("Item#";A1:F1;0)))
and the formula to find the LOEWST VALUE PRICE
=MIN(IF(A1:F1="Price";A2:F2))
For example in the following:
Value in C2 is the lowest, however it is not the lowest price value, which is itself in F2 That is why you need to add these match conditions to find the value above which is item# or above which is Price.
So for Price I used MIN(IF and for Item# i used a MATCH condition.
here is the excel sheet example downloadable from dropbox
P.S. do NOT forget to adjust the formuals to your regional settings, by replacing the ";" with ","
tell me if it works.
Is OFFSET the correct way to do this? - No
OFFSET expects as its first parameter a range reference, not a value.
If your prices are unique, use this (if they are not, I'm not sure what result you would expect)
=INDEX(A2:F2,MATCH(H2,A2:F2,0)-1)
That said, your SMALL formula looks suspect. It looks like you want to return "" if all the prices are 0. But your formula will return the smallest item# in that case. Can you confirm or explain?

Custom Formula for Grand Total column

I have a frequent problem where the formula I want to use in the Values area in my Pivot-Table is different than the formula I want to use for the Grand Total column of that row. I typically want to Sum the Values but I want to average the Sums. Here is what I normally would get if I pivoted the dates on the Column Labels, Meat Type on the Row Labels, and Sum Orders in the Values.
Row Lables | Day 1 | Day 2 | Day 3 | Grand Total
________________________________________________
Beef | 100 | 105 | 102 | 307
Chicken | 200 | 201 | 202 | 603
I get sums by day and a sum of all of the days in the Grand Total column. Here is what I want to have:
Row Lables | Day 1 | Day 2 | Day 3 | Grand Total (Avg of Day Totals)
________________________________________________
Beef | 100 | 105 | 102 | 102.3
Chicken | 200 | 201 | 202 | 201.0
In this case the Orders are still summed by day but the Grand Total is now an average of the sums. What I do now is copy and paste the Pivot data onto a seperate sheet then calculate the averages. If there was a way to do this with a custom Grand Total column it would be incredible. This is one of the biggest shortcomings of Pivot Tables for me but I'm hoping it is due to my ignorance, which it often is. Thanks for the help!
You can write a measure that checks the number of 'rows' in a particular filter context and nest that in an IF() to determine which to use.
If using PowerPivot V2 then it's:
=IF(HASONEVALUE(Calendar[Day]), SUM(AMOUNT), AVERAGE(AMOUNT))
If using PowerPivot V1 it's:
=IF(COUNTROWS(Calendar[Day])=1, SUM(AMOUNT), AVERAGE(AMOUNT))
Both do the same thing in that they assess the number of rows in the table in the given context and when the Meat Type is 'Beef' then the temporarily filtered table has one row. If it doesn't have one row then its going down the path of the AVERAGE()
This assumes your column headers 'Days' are in a table called Calendar (if you aren't using a separate Calendar table then you are missing the most powerful functionality of PowerPivot IMO).
Jacob
I can't think of a "good" way, but here's one option. Add the Amount field to the data area a second time and change the operation to Average. Then use conditional formatting to hide the averages in the data area and hide the sums in the total area.
You might be better off just using some array formulas in a do-it-yourelf pivot table. You lose the pivot table benefits, but get more flexibility with the data.

Resources