Suppose I have the following data in cells A1:A3
A1 = 1
A2 = 3
A3 = 5
I'd like to write a formula that sums the difference between the number in the cell and 2. If the difference is negative, then take 0. I tried the following CSE/array formula:
{=sum(max(A1:A3-2,0))}
Excel evaluates the formula as follows:
sum(max({1-2,3-2,5-2},0)) = sum(max({-1,1,3},0)) = sum(max(-1,1,3,0)) = sum(3) = 3
I'd like to write a formula that is evaluated as follows:
sum({max(-1,0),max(1,0),max(3,0)}) = sum({0,1,3}) = 4
Obviously one approach to this problem is to carry out a second computation in column B such that:
B1 = max(A1-2,0) = max(-1,0) = 0
B2 = max(A2-2,0) = max( 1,0) = 1
B3 = max(A3-2,0) = max( 3,0) = 3
then my target formula is:
= sum(B1:B3) = 4
However, I'd like to know: is there a way to do this in a single cell using array formula?
Thanks!
How about this? Instead of trying to use MAX, just use SUMPRODUCT. If the difference is negative, A1:A3-2>0 evaluates to FALSE, which is equivalent to multiplying the difference by 0.
=SUMPRODUCT((A1:A3-2)*(A1:A3-2>0))
Related
I am trying to write an Excel function in order to compare the values of two columns and write in a third column a specific value, dependent of that comparison. The conditions that need to be simultaneously met are the following:
IF A1 = 0 AND B1 = 1 THEN C1 = 2 IF A1 = 0 AND B1 = 2 THEN C1 = 1 IF A1 = 2 AND B1 = 1 THEN C1 = 3 IF A1 = 2 AND B1 = 2 THEN C1 = 4
Is it possible to achieve this with nested IF's in Excel?
Many thanks
So, based on what you state, this:
=if(and(a1=0,b1=1),2,if(and(a1=0,b1=2),1,if(and(a1=2,b1=1),3,if(and(a1=2,b1=2),4,"check"))))
Proving most cases:
What I would do, is to externalize those conditions. It helps you (and everyone else) whenever
your boss wants to know how your are calculating this specific value --> you simply show her the condition table (no need to look into the formula)
your boss (or the data itself) wants you to add another condition --> you simply add a new row to the conditions table (no need to make the formula longer)
...
The formula I use in Column C of the data table:
=IFERROR(FILTER(tblConditionC[C],(tblConditionC[A]=[#A]) *(tblConditionC[B]=[#B])),"no mapping")
This should do what you want:
=IF(A1=0,IF(B1=1,2,IF(B1=2,1,"Invalid Input")),IF(A1=2,IF(B1=1,3,IF(B1=2,4,"Invalid Input")),"Invalid Input"))
The follow Formula give me the correct Value:
=SUM(SUMIFS('POS Data'!$G:$G,'POS Data'!$B:$B,{"5","10","11"}))
However I require part of the criteria to be taken from a cell value. eg
=SUM(SUMIFS('POS Data'!$G:$G,'POS Data'!$B:$B,E1))
E1 cell value = {"5","10","11"}
However the formula gives a 0 value. What am I missing? Why is it not recognizing that E1 is that value?
The solution used was as follows:
=SUMPRODUCT(SUMIFS('POS Data'!$G:$G,'POS Data'!$B:$B,E1:G1))
E1 = 5
F1 = 10
G1 = 11
That you #scottCraner
Assuming your import cell with your array is in E1, You have to change it from {"5","10","11"} to 5,10,11 in the cell value
=SUM(SUMIFS(G:G,B:B,TRANSPOSE(FILTERXML("<x><y>"&SUBSTITUTE(E1,",","</y><y>")&"</y></x>","//y"))))
I need some help with an excel =if and formula
I have 4 different situations with 4 different outcomes
if cell N2 = 0 and A2 = "apple" then I need to print outcome AB
if cell N2 < 0 and A2 = "apple" then I need to print outcome CD
if cell N2 = 0 and A2 = "pie" then I need to print outcome EF
if cell N2 < 0 and A2 = "pie" then I need to print outcome GH
=IF(AND(N2=0, A2="apple"), "AB", "CD", IF(AND(N2=0, A2="pie"), "EF", "GH"))
according to excel there are to many arguments in this formula
Based on:
"Its just these values: equal to OR lower then zero and "apple" and "pie"
This should do:
=IF(N2=0,IF(A2="apple","AB","EF"),IF(A2="apple","CD","GH"))
IF is of the form
true/false statement, result if true, result if false
Your initial true/false bit is
AND(N2=0, A2="apple")
True bit
AB
False bit
CD
and that's your lot. You need to nest the next IF within the False bit.
This should work. If you have no other options, could probably be shortened. If your list gets any longer better to opt for a look-up table.
=IF(AND(N2=0,A2="apple"),"AB",IF(AND(N2=0,A2="pie"),"EF",IF(AND(N2<0,A2="apple"),"CD",IF(AND(N2<0,A2="pie"),"GH","???"))))
Here's a sample of my matrix:
A B C D E
1 0 0 1 1
0 0 0 0 0
0 0 1 1 0
0 2 1
You can think of each row as a respondent and each column as an item on a questionnaire.
My goal is to take an average of the sum of each row (i.e. total score for each respondent) without creating a new column AND accounting for the fact that some or all of the entries in a given row are empty (e.g., some respondents
missed some items [see row 5] or didn't complete the questionnaire entirely [see row 3]).
The desired solution for this matrix = 1.67, whereby
[1+0+0+1+1 = 3] + [0+0+0+0+0 = 0] + [0+0+1+1+0 = 2]/3 == 5/3 = 1.67
As you can see, we have averaged over three values despite there being five rows because one has missing data.
I am already able to take an average of the sum of rows which are only summed for non-missing entries, e.g.,:
=AVERAGE(IF(AND(A1<>"",B1<>"",C1<>"",D1<>"",E1<>""),SUM(A1:E1)),IF(AND(A2<>"",B2<>"",C2<>"",D2<>"",E2<>""),SUM(A2:E2)),IF(AND(A3<>"",B3<>"",C3<>"",D3<>"",E3<>""),SUM(A3:E3)),IF(AND(A4<>"",B4<>"",C4<>"",D4<>"",E4<>""),SUM(A4:E4)),IF(AND(A5<>"",B5<>"",C5<>"",D5<>"",E5<>""),SUM(A5:E5)))
However, this results in a value of 1 because it treats any row with some or all values values as = 0.
It does the following:
[1+0+0+1+1 = 3] + [0+0+0+0+0 = 0] + [0+0+0+0+0 = 0] + [0+0+1+1+0 = 2] + [0+0+0+0+0 = 0]/4 == 5/5 = 1
Does anyone have any ideas about how to adapt the current code to average over non-missing values or an alternative way of achieving the desired result?
You can do this more concisely with an array formula, but the short answer to fix up your existing formula is, if you have a blank cell in your sheet somewhere (say it's F1) AVERAGE will ignore blank cells so change your formula to
=AVERAGE(IF(AND(A1<>"",B1<>"",C1<>"",D1<>"",E1<>""),SUM(A1:E1),F1),IF(AND(A2<>"",B2<>"",C2<>"",D2<>"",E2<>""),SUM(A2:E2),F1),IF(AND(A3<>"",B3<>"",C3<>"",D3<>"",E3<>""),SUM(A3:E3),F1),IF(AND(A4<>"",B4<>"",C4<>"",D4<>"",E4<>""),SUM(A4:E4),F1),IF(AND(A5<>"",B5<>"",C5<>"",D5<>"",E5<>""),SUM(A5:E5),F1))
This would be one array formula version of your formula - it uses OFFSET to pull out each row of the matrix then SUBTOTAL to see if every cell in that row has a number in it. Then it uses SUBTOTAL again to work out the sum of each row and AVERAGE to get the average of rows.
=AVERAGE(IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))=COLUMNS(A1:E1),SUBTOTAL(9,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1))),""))
Has to be entered as an array formula using CtrlShiftEnter
Note 1 - some people don't like using OFFSET because it is volatile - you can use matrix multiplication instead but it's arguably less easy to understand.
Note 2 - I used "" instead of referring to an empty cell. Interesting that the non-array formula needed an actual blank cell but the array formula needed an empty string.
You can omit the empty string
=AVERAGE(IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))=COLUMNS(A1:E1),SUBTOTAL(9,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))))
Basically, what you're describing here for your desired result is the =AVERAGEA() function
The Microsoft Excel AVERAGEA function returns the average (arithmetic
mean) of the numbers provided. The AVERAGEA function is different from
the AVERAGE function in that it treats TRUE as a value of 1 and FALSE
as a value of 0.
With that in mind, the formula should look like this.
=SUM(AVERAGEA(A1:A4),AVERAGEA(B1:B4),AVERAGE(C1:C4),AVERAGEA(D1:D4),AVERAGEA(E1:E4))
Produces the expected result:
Note, if you want to ROUND() the result to two digits, add the following formula to it:
=ROUND(SUM(AVERAGEA(A1:A4),AVERAGEA(B1:B4),AVERAGE(C1:C4),AVERAGEA(D1:D4),AVERAGEA(E1:E4)), 2)
I would like to know how to specify a cell in a sum function based on a variable.
For instance,
startpoint: a1 = 5
length: a2 = 10
i have data in b1 - b100.
I want a sum function sum(b(a1):b(a1+a2))
which would yield:
sum(b5:b15)
Thank you
Use the non volatile INDEX function:
=SUM(INDEX(B:B,A1):INDEX(B:B,A1+A2))