In Google Sheets, when using ArrayFormula with AND formula, I don't get the results as it should be.
A
B
6
7
In C1 I put formula as: =and(A1>5,B1>6) then I get True. If in D1 I put formula as: =ArrayFormula(and(A1:A>5,B1:B>6)) I get the results as False.
Here are my two questions:
Why is ArrayFormula not repeated for all cells in the column?
Why do I get true without ArrayFormula and False with Arrayformula?
AND doesn't work that way with Array formulae because it ANDs the whole array together in the top left cell, regardless of number of dimensions.
I.e. it checks if "">"" which is FALSE, ANDed with anything it will return FALSE for the top left cell, that result is carried down.
You can use multiplication of truth values to create ANDing that works with ARRAYFORMULA like this:
=ArrayFormula((A1:A>1)*(B1:B>6) = 1)
The OR equivalent would obviously be
=ArrayFormula((A1:A>1)+(B1:B>6) > 0)
Take this formula for instance:
=ARRAYFORMULA(IF(ISNUMBER(G3:G),IF(AND( G3:G>=7.5,G3:G<=8),"Full Day",IF(AND(G3:G>8,G3:G<24) ,"Full Day+",IF(AND(G3<7.5,G3>=4),"Half Day",IF(G3<4,"Short Leave",)))),))
Since you use the ARRAYFORMULA function you should use * instead of the AND function.
=ARRAYFORMULA(IF(ISNUMBER(A3:A),IF((A3:A>=7.5)*(A3:A<=8),"Full Day",IF((A3:A>8)*(A3:A<24) ,"Full Day+",IF((A3<7.5)*(A3>=4),"Half Day",IF(A3<4,"Short Leave",)))),))
When using the ARRAYFORMULA function you should use * instead of the AND function and + instead of the OR function.
Explanation
I don't recall an official site about this right now. In any case.
Point 1
Within an arrayformula, AND gives a single value. Not an array of TRUE/FALSE.
Point 2
You must also remember that in "math language", TRUE=1 and FALSE=0
Meaning
+----------+--------+
| Formula | Result |
+----------+--------+
| =TRUE+2 | 3 |
| =FALSE+2 | 2 |
+----------+--------+
As you can see one can interchange between boolean TRUE/FALSE and 1/0 numbers.
Point 3
About the AND function
The AND function returns true if all the provided arguments are logically true and false if any of the provided arguments are logically false.
Putting it all together
In an arrayformula, instead of using AND/OR when making comparisons, we take advantage of the above information.
So, the "multiplication" (A3:A>=7.5)*(A3:A<=8) will return 1 (meaning TRUE) only if both sides return TRUE. All other conditions return 0 (meaning FALSE).
This is the exact behaviour of the AND function and does work in an ARRAYFORMULA.
About the OR function
The OR function returns true if any of the provided arguments are logically true and false if all of the provided arguments are logically false.
The same logic is applied within the arrayformula, when using + ("addition") instead of the OR function.
AND doesn't go with an Arrayformula. However, there is an interesting way to use AND and OR operators without really using them.
While using arrayformula, in order to do an AND function you must multiply the 2 conditions, for example
=arrayformula(if((condition1)*(condition2), value if true, value if false))
Similarly using OR functions is identical EXCEPT instead of a "*" Symbol you would ADD the 2 conditions.
=arrayformula(if((condition1)+(condition2), value if true, value if false))
Why is ArrayFormula not repeated for all cells in the column?
As addressed in previous answers,
AND, ANDs the whole array, without regarding dimensions. But, given the recent updates in spreadsheets, using AND respecting dimensions, is now possible. If you want AND, by row, you can use BYROW:
=BYROW(
A2:B4,
LAMBDA(
row,
AND(INDEX(row,0,1)>5,INDEX(row,0,2)>6))
)
Here, INDEX is used to access the first column and second column of every row. If you don't want to use INDEX, Another alternative is MAP , which provides a MAP of every argument before LAMBDA:
=MAP(A2:A4, B2:B4, LAMBDA(a,b,AND(a>5,b>6)))
A
B
MAP
BYROW
6
7
TRUE
TRUE
8
1
FALSE
FALSE
9
7
TRUE
TRUE
Related
In Google Sheets, when using ArrayFormula with AND formula, I don't get the results as it should be.
A
B
6
7
In C1 I put formula as: =and(A1>5,B1>6) then I get True. If in D1 I put formula as: =ArrayFormula(and(A1:A>5,B1:B>6)) I get the results as False.
Here are my two questions:
Why is ArrayFormula not repeated for all cells in the column?
Why do I get true without ArrayFormula and False with Arrayformula?
AND doesn't work that way with Array formulae because it ANDs the whole array together in the top left cell, regardless of number of dimensions.
I.e. it checks if "">"" which is FALSE, ANDed with anything it will return FALSE for the top left cell, that result is carried down.
You can use multiplication of truth values to create ANDing that works with ARRAYFORMULA like this:
=ArrayFormula((A1:A>1)*(B1:B>6) = 1)
The OR equivalent would obviously be
=ArrayFormula((A1:A>1)+(B1:B>6) > 0)
Take this formula for instance:
=ARRAYFORMULA(IF(ISNUMBER(G3:G),IF(AND( G3:G>=7.5,G3:G<=8),"Full Day",IF(AND(G3:G>8,G3:G<24) ,"Full Day+",IF(AND(G3<7.5,G3>=4),"Half Day",IF(G3<4,"Short Leave",)))),))
Since you use the ARRAYFORMULA function you should use * instead of the AND function.
=ARRAYFORMULA(IF(ISNUMBER(A3:A),IF((A3:A>=7.5)*(A3:A<=8),"Full Day",IF((A3:A>8)*(A3:A<24) ,"Full Day+",IF((A3<7.5)*(A3>=4),"Half Day",IF(A3<4,"Short Leave",)))),))
When using the ARRAYFORMULA function you should use * instead of the AND function and + instead of the OR function.
Explanation
I don't recall an official site about this right now. In any case.
Point 1
Within an arrayformula, AND gives a single value. Not an array of TRUE/FALSE.
Point 2
You must also remember that in "math language", TRUE=1 and FALSE=0
Meaning
+----------+--------+
| Formula | Result |
+----------+--------+
| =TRUE+2 | 3 |
| =FALSE+2 | 2 |
+----------+--------+
As you can see one can interchange between boolean TRUE/FALSE and 1/0 numbers.
Point 3
About the AND function
The AND function returns true if all the provided arguments are logically true and false if any of the provided arguments are logically false.
Putting it all together
In an arrayformula, instead of using AND/OR when making comparisons, we take advantage of the above information.
So, the "multiplication" (A3:A>=7.5)*(A3:A<=8) will return 1 (meaning TRUE) only if both sides return TRUE. All other conditions return 0 (meaning FALSE).
This is the exact behaviour of the AND function and does work in an ARRAYFORMULA.
About the OR function
The OR function returns true if any of the provided arguments are logically true and false if all of the provided arguments are logically false.
The same logic is applied within the arrayformula, when using + ("addition") instead of the OR function.
AND doesn't go with an Arrayformula. However, there is an interesting way to use AND and OR operators without really using them.
While using arrayformula, in order to do an AND function you must multiply the 2 conditions, for example
=arrayformula(if((condition1)*(condition2), value if true, value if false))
Similarly using OR functions is identical EXCEPT instead of a "*" Symbol you would ADD the 2 conditions.
=arrayformula(if((condition1)+(condition2), value if true, value if false))
Why is ArrayFormula not repeated for all cells in the column?
As addressed in previous answers,
AND, ANDs the whole array, without regarding dimensions. But, given the recent updates in spreadsheets, using AND respecting dimensions, is now possible. If you want AND, by row, you can use BYROW:
=BYROW(
A2:B4,
LAMBDA(
row,
AND(INDEX(row,0,1)>5,INDEX(row,0,2)>6))
)
Here, INDEX is used to access the first column and second column of every row. If you don't want to use INDEX, Another alternative is MAP , which provides a MAP of every argument before LAMBDA:
=MAP(A2:A4, B2:B4, LAMBDA(a,b,AND(a>5,b>6)))
A
B
MAP
BYROW
6
7
TRUE
TRUE
8
1
FALSE
FALSE
9
7
TRUE
TRUE
i've the following formula the problem i've is, this isn't working as it should be.
=SUMIFS(E9:14,$I$16:$I,FALSE(),$H$16:$H,G8)
E9:E14 is the part which should be summed up when the checkbox in I16:I = FALSE and if the name matches in H16:H from G8. My problem is I am getting this error
The array arguments of "SUMIFS" vary in size
My question would be, how do I get this exact formula to work? Exactly these areas have to be covered and cannot be changed, otherwise everything else is broken.
EDIT: Added example spreadsheet
https://docs.google.com/spreadsheets/d/1DdTSZAfGTpoeun3k2qqkDMG1jnaUaSz6wgSf2heIIDI/edit?usp=sharing
You need to adjust your ranges. Here's how =SUMIFS() works and then you'll see why you need to adjust the function.
=SUMIFS() looks for ranges and then applies the logic. So when you are telling the function to summarise E9:E14 it interprets it as:
SUM(E9,E10,E11,E12,E13,E14)
provided the following conditions. The conditions will tell the function whether to include each of the components (i.e. E9,...,E14).
Whether a condition is met or not is decided using a simple boolean (true/false) array. This could for example be I9:I14=FALSE which is interpreted as the array
{IF(I9=FALSE),IF(I10=FALSE),...,IF(I14=FALSE)}
resulting in an array similar to this:
{TRUE,TRUE,FALSE,FALSE,FALSE,TRUE}
(assuming the conditions I9, I10 and I14 are met but not the other three. The same is done for the second condition (the values in column H being equal to the value in G8, resulting in another array similar to this:
{TRUE,FALSE,FALSE,TRUE,FALSE,TRUE}
(assuming that only the values in H9, H12 and H14 are equal to G8.
When the function compares the two condition arrays and returns an aggregate array similar to this:
{TRUE,FALSE,FALSE,FALSE,FALSE,TRUE}
because only for the first and the last value the conditions are met. Therefore the =SUM function becomes like this:
SUM(E9,FALSE,FALSE,FALSE,FALSE,E14)
where FALSE = 0 so it returns
=SUM(E9,E14)
Here's where you get into trouble
You try to pass the function conditional arrays that are of a different size to the sum array (E9:E14), in effect asking it to compare apples and the age of your neighbour. What you need to do is to create the calculation you have in column E in a new column in rows 24 down and use that as the sum range in =SUMIFS().
My table contains 2 sheets with a different number of columns. I want to add a column that will display true or false (or any other 2 opposite values ) for each row depending on whether this row satisfies 2 criteria which are: sheet1!col1=sheet2!col1 and sheet1!col2=sheet2!col2.
You'll find an illustration below.
I've tried using
ARRAYFORMULA(VLOOKUP(A1&B1, {Sheet1!A1:A4&Sheet1!B1:B4,Sheet1!C1}, 3))
but I get an error message
vlookup evaluates to an out of bound range
So I wanted to try
QUERY({Sheet1!A1:B4,A1:B5}, "Select C where ")
but I couldn't figure out how to write the condition where (sheet1)col1=(sheet2)col1 & (sheet1)col2=(sheet2)col2 and I also don't know if I can work with tables of different dimensions. I finally tried
=MATCH(A1&B1,{Sheet1!A1:A&Sheet1!B1:B})
but it always returns 1.
Any idea please?
Sheet 1
Sheet 2
Your first formula is almost right. You are getting the error message because there is only one column in the curly brackets so you have to change it to
=ArrayFormula(vlookup(A1&B1,{Sheet2!A:A&Sheet2!B:B},1,false))
and add the 'false' to make sure it only does exact matches.
To make the query work you need the right syntax to access cells in the current sheet:
=query(Sheet2!A:B," select A,B where A='"&A1&"' and B='"&B1&"'")
To make the match work, you need to enter it as an array formula and add a zero to specify exact match:
=ArrayFormula(MATCH(A1&B1,{Sheet2!A:A&Sheet2!B:B},0))
However I would take flak from my colleagues if I didn't point out that there is an issue with the vlookup and match as shown above - toto&moto would match with not just toto&moto, but also with tot&omoto etc. The way round this is to add a separator character e.g.
=ArrayFormula(vlookup(A1&"|"&B1,{Sheet2!A:A&"|"&Sheet2!B:B},1,false))
=ArrayFormula(MATCH(A1&"|"&B1,{Sheet2!A:A&"|"&Sheet2!B:B},0))
These still need some tidying up if they are to report Yes and No, and also not to give false positive on blank rows - also the vlookup and match can be written as self-expanding array formulas - but that is the short answer to the question.
I have a bunch of columns that holds scores like 3-1 1-4 1-0 2-2.
I would like to count all columns that are winning, ie. left number is higher than right one.
I have this formula to know if a column is winning : =LEFT(H2; FIND("-"; H2)-1)-RIGHT(H2; FIND("-"; H2)-1)
What I want to do now is to use this formula within COUNTIF, something along the line :
=COUNTIF(A1:A10; "LEFT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)-RIGHT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)")
Is there any way I can do it with a single formula ?
Complex filtering like this can be done with filter, and then the results can be counted with counta. Example:
=counta(filter(A:A, LEFT(A:A, FIND("-", A:A)-1) > RIGHT(A:A, FIND("-", A:A)-1)))
The first argument of filter is the range to be filtered; the second is a formula based on that range (or another range with the same row count) which returns True or False. The rows where the formula evaluates to True are returned.
This seems to be a simple problem that I cannot get around. I have the following formula (works fine).
=MEDIAN(FILTER('Filtered-Data'!$D$4:$D,'Filtered-Data'!$C$4:$C=B3))
What I want is to use ArrayFormula to get a column wise result for the range B3:B instead of just B3
Can this be done?
First idea was to use Google QUERY function, however it does not support standard SQL SELECT ... WHERE... IN (valA,valB...) clause. However thing you want is achievable with ARRAYFORMULA. The trick is to match appropriately two columns, handle errors, and apply appropriate filtering (multiline for readability):
=MEDIAN(FILTER(Filtered-Data'!$D$4:$D,
ARRAYFORMULA(IF(IFERROR(MATCH
(Filtered-Data'!$D$4:$D,'Filtered-Data'!$C$4:$C=B3:B,0),0)>0,TRUE,FALSE))))
Explanation:
ARRAYFORMULA(MATCH(col1,col2,0)) finds in array first row containing value matching criteria, 0/1 determines whether matching starts from top or bottom. For standalone use, MATCH stops after producing first result. Applying ARRAYFORMULA on MATCH gets us close to what we want - it produces an array of results. However, if item is not matched, error is returned, so you get something like below, in a column:
N/A
2
3
1
3
2
N/A
N/A
N/A
1
3
These N/A stuff will do us no good, since we always want some kind of row number. Here comes IFERROR, which replaces all N\A with good ol' "0": IFERROR(MATCH(col1,col2,0),0)
Google Spreadsheet never counts rows from "0" (it starts from "1", only offsets are zero-based), so this is pretty safe unless your data contains "0" - than change it to anything else safe for your case.
Since FILTER expects in filtering_criteria single column array of booleans or expression that evaluates to array of booleans (https://support.google.com/drive/table/25273?hl=en ) we need to make sure ARRAYFORMULA produces such array of booleans. IF comes in handy - always produces either TRUE or FALSE: IF(IFERROR(MATCH(col1,col2,0)>0,TRUE,FALSE)
Now re-apply ARRAYFORMULA on that stuff, to work on array & render an array result instead of single result: ARRAYFORMULA(IF(IFERROR(MATCH(col1,col2,0)>0,TRUE,FALSE)) and put it inside FILTER... voila, it works.
For reference see: http://productforums.google.com/forum/#!topic/docs/PpnZinkLzQs