I am trying to get a sum of column C if A is abc or cba and B is def:
=SUMIFS(C2:C51;A2:A51;{"abc","cba"};B2:B51;"def")
But the formula is not valid, not sure where is my mistake since this was proposed in a quick google search.
Thank you for your suggestions.
The formula is valid for me, but this might be an issue with your delimiter. Depending on your excel, windows or location settings you might need to use a comma , as a delimiter, instead of a semicolon ;.
As for your formula, for completion I've done the same google search and ended up with this reference. It seems your logic in the formula is correct apart from one crucial step, the SUM( wrapping around your formula. This means if your formula works, it will only take the first hit into account, but with the sum, it will count every entry where your logic is True. Syntax:
=SUM(SUMIFS(C2:C51,A2:A51,{"abc","cba"},B2:B51,"def"))
Or semicolon delimited:
=SUM(SUMIFS(C2:C51;A2:A51;{"abc";"cba"};B2:B51;"def"))
Since the {array} option does not seem to be working for you, I propose a workaround as follows:
=SUMIFS(C1:C15;A1:A15;"abc";B1:B15;"def")+SUMIFS(C1:C15;A1:A15;"cba";B1:B15;"def")
This is a more clunky function, but reaches the same result by splitting up the data in two SUMIFS( functions and adding the results together.
Probably I would use #Plutian answer (actually I upvoted), but in case it might work for you, you can use SUMPRODUCT combined with DOUBLE UNARY to get exactly what you want.
DOUBLE UNARY
SUMPRODUCT
I made a fake dataset like this:
As you can see, only the highlighted values meet your requirements ( if A=abc OR cba AND B=def)
My formula in E10 is:
=SUMPRODUCT(--($A$2:$A$7="abc")+--($A$2:$A$7="cba");--($B$2:$B$7="def");$C$2:$C$7)
This is how it works:
($A$2:$A$7="abc") will return an array of True/False values if condition is met.
That array, because it's inside a double unary operator --( your range ), will convert all True/False values into 1 or 0 values. Let's say it works like if you would have selected a range of cells that contains only 1 or 0. So this will return an array like {1,0,1,0,1,0} in this case
--($A$2:$A$7="cba") will do exactly the same than steps 1 or 2 again, but with your second option. It will return another array of values, in this case, {0,1,0,1,0,1}
--($A$2:$A$7="abc")+--($A$2:$A$7="cba") we are just summing up both arrays, so {1,0,1,0,1,0}+{0,1,0,1,0,1}={1,1,1,1,1,1}
--($B$2:$B$7="def") will do like steps 1 and 2 again with your third condition, and will return another array, now it will be {1,0,1,0,0,1}
The array obtained in step 5 then it's multiplied to array obtained in step 4, so we are doing {1,1,1,1,1,1} * {1,0,1,0,0,1}={1,0,1,0,0,1}
Now, that final array obtained in step 7 then it's multiplied by the values of cells $C$2:$C$7, so in this case is {1,0,1,0,0,1} * {10,1,10,1,1,10} = {10,0,10,0,0,10}
And final step, we sum up all values inside array obtained in last step, so we do 10+0+10+0+0+10=30
I've explained every step to make sure everybody can understand, because SUMPRODUCT it's really an useful function if you know how to hanlde (I'm a noob, but I've seen real heroes here on SO using this function).
The advantage of using SUMPRODUCT instead of SUMIFS is that you can easily add more conditions to apply same range (case --($A$2:$A$7="abc")+--($A$2:$A$7="cba") or single condition to additional ranges (case --($B$2:$B$7="def")).
With normal SUMIFS probably you would have to add 1 extra complete SUMIF for each condition applied in same range.
Hope this helps
Related
We use this format in our casino to know where we have to send our employees to certain tables or games. We recently changed the way we do this and we now need to have some checks to make sure we didn't forget certain tables.
Every hour/half hour/20mins we assign a table to a person, everyone else moves one up. We know exactly which tables are open at which times. We fill this in at the top. When we fill in the upcoming timeslot we would like to have some check so we don't forget a table and maybe miss out a employee.
Example:
In the example supplied you can see that we accidentally have two number 6's but no 7 I highlighted the number 7 in the top row but it would be nice if this is doable automatically
I used VLOOKUP and INDEX/MATCH in the formula for Conditional formatting but that does not seem to create the correct outcome.
Here is an example of how it can be done:
The formula used has the array {1;2;3;4;5;6;7} hard written into it, assuming that the number of tables does not vary. The output is 0 when no table is missing, otherwise it returns a list of missing tables separated by commas.
Note: That of course means, the comma separated list is not a numeric value but a string value and cannot be used for further calculations. If further calculations on this output are required, the 'solution' has to be changed accordingly.
Formula
=IFERROR(CONCAT(FILTER({1;2;3;4;5;6;7},ISERROR(MATCH({1;2;3;4;5;6;7},H5:H24,0)))&", "),0)
Explanation
The MATCH() function checks which of the numbers 1 to 7 are present in the given range (here H5:H24) and returns the cell index of where it is found. When a number does not appear in the range, the MATCH() function will generate an #N/A error for that number.
Then, the ISERROR() function will output a FALSE value for all numbers found by MATCH() and a TRUE value for those numbers where the MATCH() function lead to an error.
The FILTER() function filters and thereby reduces the number array {1;2;3;4;5;6;7} to only those numbers where the ISERROR() function is TRUE.
The CONCAT() function concatenates the resulting array from the FILTER() function (in case more than 1 number is missing) to a single string of numbers separated by commas.
However, when there are no open tables, i.e. the MATCH() function finds all numbers 1 to 7 in the given range, then the ISERROR() function will only return FALSE values and the thus the FILTER() function returns an 'empty' array, which is not allowed in excel and leads to an #CALC error in excel. This case is captured by the IFERROR() function encapsulating the whole calculation, and instead of showing the error, returning 0.
What about this formula:
=AND(COUNTIF(A$2:A$10,1)=1,COUNTIF(A$2:A$10,2)=1,COUNTIF(A$2:A$10,3)=1,COUNTIF(A$2:A$10,4)=1,COUNTIF(A$2:A$10,5)=1,COUNTIF(A$2:A$10,6)=1,COUNTIF(A$2:A$10,7)=1)
A bit clearer:
=AND(COUNTIF(A$2:A$10,1)=1,
COUNTIF(A$2:A$10,2)=1,
COUNTIF(A$2:A$10,3)=1,
COUNTIF(A$2:A$10,4)=1,
COUNTIF(A$2:A$10,5)=1,
COUNTIF(A$2:A$10,6)=1,
COUNTIF(A$2:A$10,7)=1)
... which means that the number of ones need to be 1, the number of twos need to be 1, ..., up to the number of sevens.
Hereby a screenshot of an Excel sheet, which contains that formula:
In order to understand how this works, you might work with formula auditing, more especially formula evaluating, hereby an extra screenshot, showing formula evaluating after some steps:
Have fun :-)
I am using this array formula which works fine
=IF(ROWS(K$62:K62)>COUNTIF(accounts_table[§],"<>J"),"",INDEX(accounts_table[Account Name],SMALL(IF(accounts_table[§]<>"J",ROW(accounts_table[§])-ROW(Ledger!$H$17)+1),ROWS($K$62:K62))))
However, I need to extend this for multiple COUNTIF Criteria:
accounts_table[§],"<>J"
accounts_table[§],"<>T"
accounts_table[§],"<>P"
I haven't been successful in doing this. I have tried this but doesn't work:
=IF(OR(ROWS(K$62:K62)>COUNTIF(accounts_table[§],"<>J"),ROWS(K$62:K62)>COUNTIF(accounts_table[§],"<>T")),"",INDEX(accounts_table[Account Name],SMALL(IF(OR(accounts_table[§]<>"J",accounts_table[§]<>"T"),ROW(accounts_table[§])-ROW(Ledger!$H$17)+1),ROWS($K$62:K62))))
You have to make two adjustments to the formula:
(1) I was talking total rubbish previously, it is correct that the easiest thing to do is to use Countifs. You could subtract the two separate counts of J and T from the total, but it's longer.
(2) You can't use AND or OR in array formulas - they only give you one result for the entire array instead of iterating over the cells like you want them to. Instead you have to use multiply (*) or add (+). Here you are trying to include cells which are both not equal to J and not equal to T, so again you need AND logic, therefore you want to multiply.
=IF(ROWS(K$62:K62)>COUNTIFS(accounts_table[§],"<>J",accounts_table[§],"<>T"),"",INDEX(accounts_table[Account Name],SMALL(IF((accounts_table[§]<>"J")*(accounts_table[§]<>"T"),ROW(accounts_table[§])-ROW(Ledger!$H$2)+1),ROWS(K$62:$K62))))
Extending it to more variables is left as an exercise for the reader...unless you have a real lot of values to exclude in which case another approach might be needed.
For example =SUMPRODUCT(A2:A10-10) does, but =SUM(A2:A10-10) does not. Specifically in my situation I wish =MEDIAN(ABS(A2:A10-MEDIAN(A2:A10))) did; however sadly it does not, and if I knew which built in functions convert the ranges into arrays before they evaluate then I might be able to reformulate something that gives me what I'm looking for...
there are some natural array type formulas like SUMPRODUCT and some of the options in AGGREGATE, as well as some financial functions.
Many can be "forced" into array mode by simply using Ctrl-Shift-Enter instead of Enter when exiting edit mode.
For example your formula:
=MEDIAN(ABS(A2:A10-MEDIAN(A2:A10)))
If it is confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode will "Force" the Array. If done correctly the Excel will put {} around the formula.
The INDEX function has an Array Form.
=SUM(INDEX((A2:A10)-10, , ))
In the above, INDEX is returning an array of values but a wrapping SUM is required to collect a total. The blank parameters represent all rows and all columns in the range (e.g. A2:A10) specified. All rows and all columns in the range can also be represented with zeroes in place of the blank parameters.
With 2 to 10 in A2:A10, your MEDIAN example would return 14 from,
=MEDIAN(INDEX(ABS((A2:A10)-MEDIAN(A2-A10)), , ))
(without CSE)
Use the Evaluate Formula command to see more of the inner workings of this style of INDEX use.
I am adding the values of several cells from two sheets in excel.
I am using the formulas below to calculate this.
My question is why to two formulas aren't giving the same result? As far as I can tell, the only difference is that the 2nd formula has an extra bracket, which shouldn't change anything?
=SUM(SUMIFS('Ark1'!F15:F75;'Ark1'!E15:E75;{"adgangsareal bolig";"fællesrum bolig"}))+SUM(SUMIFS('Ark2'!F11:F126;'Ark2'!E11:E126;"bolig"))
=SUM((SUMIFS('Ark1'!F15:F75;'Ark1'!E15:E75;{"adgangsareal bolig";"fællesrum bolig"}))+SUM(SUMIFS('Ark2'!F11:F126;'Ark2'!E11:E126;"bolig")))
The latter formula has been working perfectly until now through my Work, but for this specific value i needed to remove the extra bracket.
Jacob is right that its the array bit that is causing the problem, but really the problem is caused by the + sign in an expression containing an array which causes the expression to be evaluated as an array formula. You can fix this by changing the + to ; (or whatever the argument separator character is in your locale).
A simpler example (my locale uses , rather than ;):
=SUM({5,10}+20)
results in 55 (the expression evaluater creates (5+20)+(10+20) using the array expansion rules and then passes {25,30} to SUM) but
=SUM({5,10},20)
results in 35
In your first SUMIFS() you have the criteria listed as an array. When using SUM() around the first SUMIFS() alone, it is returning the sum of the range that meets the first criteria being true in the range, then repeats for the second critera, and then it adds the second SUMIF().
When you add the parentheses, you SUM() the first SUMIF() total for the first array value AND the second SUMIF(), and then you are repeating for the second array. So you are getting that second SUMIF() total added twice essentially.
I believe you want something like this:
=SUM(SUMIF('Ark1'!E15:E75;"adgangsareal bolig";'Ark1'!F15:F75);SUMIF('Ark1'!E15:E75;'Ark1'!F15:F75;"fællesrum bolig");SUMIF('Ark2'!E11:E126;"bolig";'Ark2'!F11:F126))
Try this:
=(87,35+464,71-87,35-464,71)
=87,35+464,71-87,35-464,71
2nd formula results correctly in ZERO, while
1st one results in very small number(-0,0000000000000568434)
Add more decimal places to see it.
I think it is a BUG and has something to do with different type of numbers (decimal, floating,...).
I'm currently using this expression to check if a cell contains at least one of a set of strings:
A B
1 abcd =ISNUMBER(SEARCH({"a","x"},A1))
B1 will return true here. However if I change the order of the array as follows it returns false:
A B
1 abcd =ISNUMBER(SEARCH({"x","a"},A1))
Why is that? Is there a better way to do this that's more elegant than using a bunch of OR()'s?
Say we have text in A1 and we also have a list of sub-strings in column C. These can be either single or multiple character substrings. We assign the Name - KeyStrings to the list of cells in column C
We want to know if any of the substrings is present in A1.
Enter the following in B1:
=NOT(LEN(TRIM(SUBSTITUTE(A1,INDEX(KeyStrings,SUMPRODUCT(ROW(KeyStrings)*ISNUMBER(SEARCH(KeyStrings,A1)))),"")))=LEN(A1))
The formula will report True if any of the substrings in column C can be found in A1
Just for completeness, the answer to the original question "Why is that?" i.e. why does the result seem to be sensitive to the order of the strings in curly brackets is that the formula
=ISNUMBER(SEARCH({"a","x"},A1))
only looks at at the first string "a" because there is nothing to make it operate like an array formula and step through all the values "a","x" etc.
The usual procedure is to wrap the function in an aggregate function like SUM. If you do this you also have to add minus signs to make the TRUE/FALSE values from ISNUMBER behave as ones and zeroes and further wrap in SUMPRODUCT or enter it as an array formula to make it work:-
=SUMPRODUCT(SUM(--ISNUMBER(SEARCH({"a","x"},A1))))
Even then it gives a numeric result which is not what you actually want.
# Dirk Reichel's idea is therefore much better giving you:-
=OR(ISNUMBER(SEARCH({"a","x"},A1)))
which works very nicely and incidentally doesn't need to be entered as an array formula.
I think since you have put a curly bracket there, it indicates an array search. (I am not an expert with arrays, so following is an educated guess).
What Excel is doing is in the first example, it is finding "a" in position no 1, so it is returning the no 1 to ISNUMBER function, which is TRUE. (Basically, compares "a" to a in abcd which is position 1, and doesn't find/look for[?] "x". Returns 1 for the "a", which is TRUE for ISNUMBER function)
In the 2nd code, "x" is compared to a from abcd, which returns a non-numeric value (and doesn't find/look for[?] "a"), hence it is FALSE.
PS:
what I mean by Excel doesn't find/look for[?] the second or any element there on, seems to be based on how arrays and formulae are coded in. In the curly brackets, commas separate "columns" and semi-colons separate "rows". So you are entering a 2 column/1row array inside a search function. While there is nothing wrong with that, the SEARCH function itself may not be coded to look for any element in the array apart from the one in position [1,1]. Someone better informed with the inner workings of SEARCH function should confirm this answer. (If it is a completely wrong educated guess, I wouldn't mind taking it off)