Apply a function to an argument inside a formula - excel

How can I apply a function to an argument of a SUMIF or COUNTIF formula?
For example:
=SUMIF(YEAR(B1:B10),"2017", A1:A10)
Where B1:B10 contains an array of dates. For example:
1 A B
2 200 01/01/2017
3 300 01/01/2017
4 420 01/01/2016
5 250 01/01/2016
When I try:
=SUMPRODUCT(A:A*(YEAR(B:B)=2017))
or
=SUMPRODUCT(A1:A5*(YEAR(B1:B5)=2017))
I get #REF! however if I define the ranges like:
=SUMPRODUCT(A2:A5*(YEAR(B2:B5)=2017))
I get the result I expect.

Use SUMPRODUCT with arrays. Multiplying by a boolean array converts it to 0 or 1 array:
=SUMPRODUCT(A1:A10*(YEAR(B1:B10)=2017))
p.s.: this is a valid normal formula, no need for CSE.
On the other hand, SUMIF and SUMIFS want their range arguments to be pure ranges, not arrays.
So it appears that your data is heterogeneous and some cells are not numbers or not dates. To deal with this, try this array formula:
=SUM(IF(ISNUMBER(A1:A5), A1:A5, 0)*(YEAR(IF(ISNUMBER(B1:B5),B1:B5,0))=2017))
Ctrl+Shift+Enter

If you really like the SUMIF then you will need to use SUMIFS and bracket the date:
=SUMIFS(A:A,B:B,">=1/1/2017",B:B,"<=12/31/2017")
You can do the same with COUNTIFS.

No need to use CTRL + SHIFT + ENTER ( CSE )
=SUMPRODUCT(--(TEXT(B7:B10,"YYYY")="2017")*A1:A10)

Related

Excel: apply formula to each cell before SUM operation

I am trying to do a SUM on a range of cells but instead of using the value in the cell as IS I need to extract a piece from each cell to use in the sum. In the example below I need to use the value before the - sign.
A
1 1-3
2 2-60
3 5-3
4 =SUM(A1:A3)
SUM should be the addition of the first piece (before the -) so 1 + 2 + 5
I found a way to extract the data by using
=LEFT(A1, SEARCH("-",A1,1)-1) = 1
=LEFT(A2, SEARCH("-",A2,1)-1) = 2
=LEFT(A3, SEARCH("-",A3,1)-1) = 5
But how can I SUM the value of these formulas without having to use any more additional cells?
Use an array formula: =SUM(VALUE(LEFT(A1:A3, SEARCH(A1:A3, "-")-1))). Then press Shift-Ctrl-Enter to tell Excel this is an array formula.
sumproduct alternative
=SUMPRODUCT(--(LEFT(A1:A3,FIND(A1:A3,"-")-1)))
This is a none CSE formula that peforms array like operations. As such you should avoid full column references within the sumproduct. Also note, things may go a little "WONKY" if there is no - in the cell. To avoid that you could add a - to the very end. Formula would look something like this:
=SUMPRODUCT(--(LEFT(A1:A3&"-",FIND(A1:A3&"-","-")-1)))
That will work when you just have integers and no formula

Convert SUMIFS to SUMPRODUCT

This is my SUMIFS which SUM the number in D12:D1000 if the number in E12:E1000 is within 21 to 30.
SUMIFS(D$12:$D$1000,E$12:$E$1000,">20",E$12:$E$1000,"<31")
I tried to convert it to SUMPRODUCT:
SUMPRODUCT(D12:D1000*(E12:E1000=">20")*(E12:E1000="<31"))
But I get 0 as the result. The formula doesn't seem to work.
Your SUMPRODUCT formula should written like this,
=SUMPRODUCT (-- (A2 :A11 >=20),--(A2 :A20<=30),C2 :C20)
Please change data range as you need.

Passing multiple values to SUMIFS function in Excel

I have Twocolumns A, B ..I am trying to get the sum of A like below.
=SUMIFS(
sheet1!$A:$A,
sheet1!$B:$B, ("AB", "BC", "CD")
)
But this formula is not working.
Please suggest me.
Try to use following formula:
=SUMPRODUCT((sheet1!$B:$B={"AB","BC","CD"})*(sheet1!$A:$A))
or alternatively you can use an array formula:
=SUM(IF(sheet1!$B:$B={"AB","BC","CD"},sheet1!$A:$A,0))
enter formula in the formula bar and press CTRL+SHIFT+ENTER to evaluate it......
If I'm guessing your intention right, you should have:
=SUMIFS(sheet1!$A:$A,Sheet1!$B:$B,"AB")+SUMIFS(sheet1!$A:$A,Sheet1!$B:$B,"BC")+SUMIFS(sheet1!$A:$A,Sheet1!$B:$B,"CD")
Add two helper columns: in D:D you have the list of valid values. In C:C you have a formula like this (change ; to ,). In F1 you have your sum like this:
=SUMIFS($A:$A,$C:$C,FALSE)
Now you can add any number of valid criteria in column D:D.
You can use SUMIFS to return an array (one for each criterion) and then SUM to sum those, i.e.
=SUM(SUMIFS(sheet1!$A:$A,sheet1!$B:$B,{"AB","BC","CD"}))
This way retains the speed and efficiency of SUMIFS without needing repetition
If you have your criteria values in a range of cells you can simply reference the range, but use SUMPRODUCT to avoid "array entry"
=SUMPRODUCT(SUMIFS(sheet1!$A:$A,sheet1!$B:$B,Z2:Z4))
where Z2:Z4 contains the criteria
Note: in both of these SUMIFS does all the "heavy lifting" - SUM/SUMPRODUCT is used simply to sum the resulting array

SUMPRODUCT( SUMIF() ) - How does this work?

Part 1:
I was able to construct a formula that does exactly what I want (from some examples), but yet, I'm unable to figure out how exactly it works. I have, starting with cell A1:
Price $
table 20
chair 10
Invoice Quantity
table 17
chair 1
chair 2
table 3
What I want is the final total (430) for the invoice which is computed as Quantity*Price for each item (17*20 + 1*10 + 2*10 + 3*20). the following formula correctly does this:
=SUMPRODUCT(B6:B9,SUMIF(A2:A3,A6:A9,B2:B3))
I understand the basics of SUMPRODUCT and SUMIF. But here, my argument for SUMIF's range is A2:A3, which makes me think the SUMIF would iterate through A2 and A3, and not through A8:A11 (which is the criteria). What gives?
Edit: the unclear part is, what exactly does SUMIF do (what is its iteration pattern) when the first two arguments are of different dimensions (here, the range is 2 cells while the criteria is 4 cells). Also, what is the "output" of SUMIF? An array? Of what dimensions?
Part 2:
In addition, if I ignored the quantity and simply wanted to add 20 whenever I saw a table and 10 whenever I saw a chair, I figured I would do:
=SUMIF(A2:A3,A6:A9,B2:B3)
But that doesn't work, and I have to enclose it with a SUMPRODUCT() for it to work and correctly evaluate to 60. Enclosing it within a SUM doesn't work either (probably because the SUMIF doesn't return an array?) Why?
I've read a bunch of tutorials and still can't understand this, and would be most grateful for a clear, intuitive explanation for both these cases. Thank you.
SUMIF can produce an array of results. If you take my formula
=SUMIF(A6:A9,A2:A3,B6:B9)
it says
For the criteria in A2 (ie table)
- look at A6:A9
- where table is matched, sum the corresponding value in B6:B9
- returns 20 (ie 17 +0 +0 +3)
- this is stored in the first position of the array
Then for the criteria in A3 (ie chair)
- look at A6:A9
- where table is matched, sum the corresponding value in B6:B9
- returns 3 (ie 0 +1 +2 +0)
- this is stored in the second position of the array
So the end array from the SUMIF is {20:3}
You can see the array result by highlighting the SUMIF formula in Excel's formula bar and then pressing F9
Then use SUMPRODUCT to multiple the count in the SUMIF by the $ values in B2:B3 to get total dollars
={20;3}*{20:10}
=20*20 + 3*10
= 430
Part 1
Rather than
SUMIF(A2:A3,A6:A9,B2:B3)
which produces a four element array of
={20;10;10;20}
(corresponding to table;chair;chair;table)
You should use
SUMIF(A6:A9,A2:A3,B6:B9)
which sums the values in B6:B9 against your two criteria in A2:A3 giving the desired result
={20;3}
(corresponding to table;chair)
and then use SUMPRODUCT to weight your array, ie
=SUMPRODUCT(SUMIF(A6:A9,A2:A3,B6:B9),B2:B3)
={20;3}*{20:10}
=430
Part 2
Use COUNTIF to return an array of the number of chairs and tables and then multiply by the vales using SUMPRODUCT
=SUMPRODUCT(B2:B3,COUNTIF(A6:A9,A2:A3))
={20;10} * {2;2}
=60
Well you only have one minor mistake:
probably because the SUMIF doesn't return an array?
SUMIF can work with arrays, thats why you formula SUMPRODUCT( SUMIF() ) works in first place, to SUMIF show an array you have to select a group of cells (like C6:C9) input the formula and use CTRL+SHIFT+ENTER instead of ENTER only. this generate an "array fomula", identified by curly brackets {} (those can only be entered with CTRL+SHIFT+ENTER, no manualy) and show the array formula and results

MS Excel create virtual column to use in formula

I want to do something similar to summing 1/x from 1 to 100 without creating an extra column to help with the calculation. I want my first column to be the numbers 1 through 100. And I want a cell to show the sum of 1/x where x is each cell in the first column. Currently the only way I can think to do this is to create a second column to do 1/x for each individual cell then sum the second column. Is there any solution to doing this without having to create the second column?
Thanks!
You can use this "array formula"
=SUM(1/A1:A100)
confirmed with CTRL+SHIFT+ENTER
....or avoid array entry by using SUMPRODUCT
=SUMPRODUCT(1/A1:A100)
both versions assume you don't have zeroes (or blanks) in A1:A100
if you might have zeroes or blanks then use this array formula
=SUM(IF(A1:A100,1/A1:A100))
if you mean 1 + 1/2 + 1/3 + ... + 1/100, use following array formula (entered by Ctrl+Shift+Enter instead of just Enter):
=sum(1/row(A1:A100))
You need to use an array formula as below:
{=SUM(1/A1:A100)}
where A1:A100 contains 1 to 100
You create the array formula bu typing the formula as =SUM(1/A1:A100) and then pressing Control-Shift-Enter.
If you do it correctly the formula then shows up with the curly brackets {} but you don't enter the curly brackets yourself.

Resources