Using SUMPRODUCT and OFFSET on multiple rows in Excel - excel

I have a question on using OFFSET in Excel.
For instance, I have a table with values varying by years.
Then, I have a table with some values varying by year/months.
!!Click here for the tables!!
I would like to write a formula e.g.
=SUMPRODUCT((E2:E37)*OFFSET(A1,C2:C37,1),E2:E37)
but it returns #VALUE!
In short, I would like to use an array of values in C2:C37 i.e. {1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3} to obtain the array {0.03,0.03,0.03,0.03,0.03,0.03,0.03,0.03,0.03,0.03,0.03,0.03,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,}, and this array is then used in the SUMPRODUCT function.
Can someone help me solve the #VALUE! issue?
Thanks in advance!

Apparently (I didn't know this) Offset is unusual because it can return a set of range objects when used with an array as the second argument. Most functions can't handle this if you pass it to them. But if you put it through the N function, you get the right answer.
=SUMPRODUCT((E2:E37)*N(OFFSET(A1,C2:C37,1)))
or
=SUMPRODUCT((E2:E37),N(OFFSET(A1,C2:C37,1)))
Note that these appear to give an array of #Value! errors when you run them through Evaluate Formula but these resolve after passing through the N function.
Of course it would be more common to do this the easy way and use Index or Vlookup with a helper column. My first thought when trying to do it in a single Array formula without using Offset was this:-
=SUM(E2:E37*MMULT(N(C2:C37=TRANSPOSE((ROW(A2:A4)-1))),B2:B4))
I would think that the Offset way is easier and more efficient.
But in this particular case where there are only a small number of categories you could use this array formula which is perhaps the simplest approach:-
=SUM(CHOOSE(C2:C37+1,0,B2,B3,B4)*E2:E37)
The above formulae only work for the special case where the 'key' is the same as the row number in the lookup column. The offset method can easily be adapted to incorporate a lookup:-
=SUM((E2:E37)*N(OFFSET(A1,IFERROR(MATCH(C2:C37,A2:A4,0),0),1)))
See this reference

Related

How can I force the Excel SMALL function to return all results satisfying an IF criterion?

I have a spreadsheet in which I would like to return the text of all cells in MyTable field "Result" in which the corresponding text in MyTable field "Query" matches a string in $C$1, as a list. I want to do this using formulas. I know that my formula will need to have the following form:
=INDEX(MyTable[Result],SMALL(IF(MyTable[Query]=C1,ROW(MyTable[Result]),""),___))
The issue is that I'm not sure what should go in the ___. I want my formula to return strings from all relevant cells, not just a fixed number of cells, because I don't know now many cells match the criteria. As far as I understand, SMALL throws an error if the number provided as the last argument exceeds the number of non-empty strings returned by the IF function. Is there any way around this?
You k argument can be an array. And the upper bounds can probably be calculated using COUNTIF in your case.
Arrays can be constructed in a variety of ways.
One way (not the most efficient) would be something like:
=row(indirect("1:" & countif(MyTable[Query],C1)))
You can also use the INDEX function to construct the array, and, if speed is an issue, this would be better as it is a non-volatile function.

Multiple ranges in RSQ Function formula

I have two data sets to compare using the Excel RSQ Function. The first dataset comprises straightforward sequential cells in a row (A1:D1). The second dataset in the row below takes its final cell from a different reference in the spreadsheet(A2:C2,G12). Can't get the formula with 3 references to work. I seem to have tried all combinations and even read up on array formulas thinking this might help but no luck. Can this even be done?
Obviously the following doesn't work, but for clarity, this is kind of what I'm after:
=RSQ(A1:D1,(A2:C2,G12))
Any help much appreciated.
You don't mention whether this needs to be dynamic or not, so we can use a static array construction:
=RSQ(A1:D1,IF({1,1,1,0},A2:C2,G12))
Note that, if you're not using an English language-version of Excel, the separator within the array constant (here a comma) may require changing.
Regards
Try to replace (A2:C2,G12) with CHOOSE({1;2;3;4},A2,B2,C2,G12).
For completeness, another method. Replace with :
N(INDIRECT({"A2";"B2";"C2";"G12"})).

Nested if/then statement syntax

I've been reading a bunch of answers around nesting if/then statements in Excel but I can't figure out how to fix a "formula parse error" in mine. From what I can tell and in my code editor the formula is correct. Any ideas what I'm missing here?
I have a row of cells that auto-generate a number 1-35 based on other values. I want the cell with the formula to pull data from another row based on what's the in row of numbers 1-35. It works when I test one at a time; it's the nesting that is causing the errors. Thanks!
=IF(E$167=1,C56,IF(E$167=2,D56,IF(E$167=3,E56,IF(E$167=4,F56,IF(E$167=5,G56,IF(E$167=6,H56,IF(E$167=7,I56,IF(E$167=8,J56,IF(E$167=9,K56,IF(E$167=10,L56,IF(E$167=11,M56,IF(E$167=12,N56,IF(E$167=13,O56,IF(E$167=14,P56,IF(E$167=15,Q56,IF(E$167=16,R56,IF(E$167=17,S56,IF(E$167=18,T56,IF(E$167=19,U56,IF(E$167=20,V56,IF(E$167=21,W56,IF(E$167=22,X56,IF(E$167=23,Y56,IF(E$167=24,Z56,IF(E$167=25,AA56,IF(E$167=26,AB56,IF(E$167=27,AC56,IF(E$167=28,AD56,IF(E$167=29,AE56,IF(E$167=30,AF56,IF(E$167=31,AG56,IF(E$167=32,AH56,IF(E$167=33,AI56,IF(E$167=34,AJ56,IF(E$167=35,AK56,””)))))))))))))))))))))))))))))))))))
You might have run into a function nesting limit. Try the choose function to work around it.
How about using
=IF(AND(E167>0,E167<36),OFFSET(B56,0,E167),"")
Note : OFFSET is a volatile function, see this for details.
A simple vlookup or hlookup function can do exactly what you need without righting long nested if statement.
for example you can use:
=vlookup(E$167,$A1:$B35,2,false) where $A1:$B35 is the table array containing column A (1-35) and column B its corresponding value.

Excel - use a range of values without the zeros

I am using a column of values that has a mix of positive numbers and zeros. I want to use various functions like MIN, MAX, AVERAGE, etc that calculates the result of just the nonzero numbers. Without making any changes to the data, is there a way to get functions to calculate this?
Ideally, there would be like a MINIF(range,">0") type of function. But I do not believe those exist. I would like to avoid using VBA as well.
Thanks!
You need to use an array formula like so:
=MIN(IF($A$1:$A$10<>0,$A$1:$A$10))
Hold CTRL+SHIFT+ENTER in the formula bar to make this an array formula.
You can do the same thing with the MAX and AVERAGE functions.
NOTE: Array formulas are inside { } - this is how you know you did it right.
As long as you have Excel 2007 or later you can use AVERAGEIF for averaging without zero, e.g.
=AVERAGEIF(Range,">0")
....but for MIN the normal approach is an array formula as AxGryndr suggests, and that same approach can be used for other functions, although non-array options are available, e.g. for smallest non zero value
=SMALL(Range,COUNTIF(Range,0)+1)
For MAX you can presumably just use a regular MAX function because that will be the same with or without zeroes, i.e.
=MAX(Range)

Using SUMIFS with multiple AND OR conditions

I would like to create a succinct Excel formula that SUMS a column based on a set of AND conditions, plus a set of OR conditions.
My Excel table contains the following data and I used defined names for the columns.
Quote_Value (Worksheet!$A:$A) holds an accounting value.
Days_To_Close (Worksheet!$B:$B) contains a formula that results in a number.
Salesman (Worksheet!$C:$C) contains text and is a name.
Quote_Month (Worksheet!$D:$D) contains a formula (=TEXT(Worksheet!$E:$E,"mmm-yy"))to convert a date/time number from another column into a text based month reference.
I want to SUM Quote_Value if Salesman equals JBloggs and Days_To_Close is equal to or less than 90 and Quote_Month is equal to one of the following (Oct-13, Nov-13, or Dec-13).
At the moment, I've got this to work but it includes a lot of repetition, which I don't think I need.
=SUM(SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,"=Oct-13")+SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,"=Nov-13")+SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,"=Dec-13"))
What I'd like to do is something more like the following but I can't work out the correct syntax:
=SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,OR(Quote_Month="Oct-13",Quote_Month="Nov-13",Quote_Month="Dec-13"))
That formula doesn't error, it just returns a 0 value. Yet if I manually examine the data, that's not correct. I even tried using TRIM(Quote_Month) to make sure that spaces hadn't crept into the data but the fact that my extended SUM formula works indicates that the data is OK and that it's a syntax issue. Can anybody steer me in the right direction?
You can use SUMIFS like this
=SUM(SUMIFS(Quote_Value,Salesman,"JBloggs",Days_To_Close,"<=90",Quote_Month,{"Oct-13","Nov-13","Dec-13"}))
The SUMIFS function will return an "array" of 3 values (one total each for "Oct-13", "Nov-13" and "Dec-13"), so you need SUM to sum that array and give you the final result.
Be careful with this syntax, you can only have at most two criteria within the formula with "OR" conditions...and if there are two then in one you must separate the criteria with commas, in the other with semi-colons.
If you need more you might use SUMPRODUCT with MATCH, e.g. in your case
=SUMPRODUCT(Quote_Value,(Salesman="JBloggs")*(Days_To_Close<=90)*ISNUMBER(MATCH(Quote_Month,{"Oct-13","Nov-13","Dec-13"},0)))
In that version you can add any number of "OR" criteria using ISNUMBER/MATCH
You can use DSUM, which will be more flexible. Like if you want to change the name of Salesman or the Quote Month, you need not change the formula, but only some criteria cells. Please see the link below for details...Even the criteria can be formula to copied from other sheets
http://office.microsoft.com/en-us/excel-help/dsum-function-HP010342460.aspx?CTT=1
You might consider referencing the actual date/time in the source column for Quote_Month, then you could transform your OR into a couple of ANDs, something like (assuing the date's in something I've chosen to call Quote_Date)
=SUMIFS(Quote_Value,"<=90",Quote_Date,">="&DATE(2013,11,1),Quote_Date,"<="&DATE(2013,12,31),Salesman,"=JBloggs",Days_To_Close)
(I moved the interesting conditions to the front).
This approach works here because that "OR" condition is actually specifying a date range - it might not work in other cases.
Quote_Month (Worksheet!$D:$D) contains a formula (=TEXT(Worksheet!$E:$E,"mmm-yy"))to convert a date/time number from another column into a text based month reference.
You can use OR by adding + in Sumproduct. See this
=SUMPRODUCT((Quote_Value)*(Salesman="JBloggs")*(Days_To_Close<=90)*((Quote_Month="Cond1")+(Quote_Month="Cond2")+(Quote_Month="Cond3")))
ScreenShot
Speed
SUMPRODUCT is faster than SUM arrays, i.e. having {} arrays in the SUM function. SUMIFS is 30% faster than SUMPRODUCT.
{SUM(SUMIFS({}))} vs SUMPRODUCT(SUMIFS({})) both works fine, but SUMPRODUCT feels a bit easier to write without the CTRL-SHIFT-ENTER to create the {}.
Preference
I personally prefer writing SUMPRODUCT(--(ISNUMBER(MATCH(...)))) over SUMPRODUCT(SUMIFS({})) for multiple criteria.
However, if you have a drop-down menu where you want to select specific characteristics or all, SUMPRODUCT(SUMIFS()), is the only way to go. (as for selecting "all", the value should enter in "<>" + "Whatever word you want as long as it's not part of the specific characteristics".
In order to get the formula to work place the cursor inside the formula and press ctr+shift+enter and then it will work!
With the following, it is easy to link the Cell address...
=SUM(SUMIFS(FAGLL03!$I$4:$I$1048576,FAGLL03!$A$4:$A$1048576,">="&INDIRECT("A"&ROW()),FAGLL03!$A$4:$A$1048576,"<="&INDIRECT("B"&ROW()),FAGLL03!$Q$4:$Q$1048576,E$2))
Can use address / substitute / Column functions as required to use Cell addresses in full DYNAMIC.

Resources