Add IF-Condition to SUMPRODUCT

寵の児 提交于 2020-07-08 03:36:52

问题


       A           B            C             D           E            F         G       H    
1  Products       Date        Sales     Criteria 1:   Product_B      Result:    200
2  Product_A    2020-04-15     500      Criteria 2:   2020-04-15    
3  Product_B    2020-04-12     600              
4  Product_B    2020-04-12     300              
5  Product_B    2020-04-15     200              
6  Product_B    2020-04-20     400              
7  Product_C    2020-04-15     800              
8  Product_C    2020-04-19     900              
9  Product_C    2020-04-30     300              
10
11

In the table above I have different products and their sales on a certain date.
In Cell G1 I calculate sum of the sales based on the criterias in Cell E1 and E2.

G1 = SUMPRODUCT((($A$2:$A$100=$E$1)*($B$2:$B$100=$E$2)*$C$2:$C$100))

All this works exactly as it should.


Now, I want to include an IF-Condition that says if you enter the words "All Products" into Cell E1 the product condition ($A$2:$A$100=$E$1) in the SUMPRODUCT should not be applied.

Therefore, I tried to go with this:

= SUMPRODUCT((IF(E1="All Products",1,($A$2:$A$100=$E$1))*($B$2:$B$100=($E$2))*$C$2:$C$100))

Unfortunately, this solution only works if I enter "All Products" into Cell E1.
Once I switch back to Product_B it displays 0 instead of 200.

What do I need to change to make it work?


NOTE:

I know one solution could be to split the SUMPRODUCT into two formulas like this:

=IF(E1="All Products",SUMPRODUCT((($B$2:$B$100=$E$2)*$C$2:$C$100)),SUMPRODUCT((($A$2:$A$100=$E$1)*($B$2:$B$100=$E$2)*$C$2:$C$100)))

However, I would prefer a solution with one SUMPRODUCT-Formula.


回答1:


With SUMIFS:

=SUMPRODUCT(SUMIFS($C:$C,$A:$A,IF(E1="All Products","*",$E$1),$B:$B,$E$2))


Combining with your LAST QUESTION

 =SUMPRODUCT(SUMIFS($C:$C,$A:$A,IF(E1="All Products","*",$E$1),$B:$B,$E$2:$E$3))




回答2:


Modify the column A criteria as follows:

=SUMPRODUCT((($A$2:$A$100=$E$1)+($E$1="All Products")*($A$2:$A$100=$A$2:$A$100))*($B$2:$B$100=$E$2)*$C$2:$C$100)

The new factor +($E$1="All Products")*($A$2:$A$100=$A$2:$A$100) will ensure a vector of all 1 with the appropriate input in E1:




回答3:


Array (Control+Shift+Enter) formula in F2

=SUMPRODUCT(IF((E1<>"All Products"),(A2:A9=E1),TRUE)*(B2:B9=E2)*(C2:C9))

Following Array formula is better than the above one. If you enter anything that doesn't match in column A it will consider it as "All Products"

=SUMPRODUCT(IF(COUNTIF(A2:A9,E1)=0,TRUE,(A2:A9=E1))*(B2:B9=E2)*(C2:C9))

OR

=SUMPRODUCT(IF(ISNA(MATCH(E1,A2:A9,0)),TRUE,(A2:A9=E1))*(B2:B9=E2)*(C2:C9))



来源:https://stackoverflow.com/questions/61894421/add-if-condition-to-sumproduct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!