87

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:

  1. Why is ArrayFormula not repeated for all cells in the column?
  2. Why do I get true without ArrayFormula and False with Arrayformula?
0

4 Answers 4

146

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)
Sign up to request clarification or add additional context in comments.

4 Comments

You can also use it the other way around, FALSE, blank and 0 are false, everything else is true. Lets you do shortcuts such as ARRAYFORMULA(IF(LEN(A2:A), <something>, "")) :)
Note that the brackets are required around the condition; otherwise, the multiplication or addition takes precedence over the comparison operator.
Thank you. Also, this behavior of AND within ARRAYFORMULA is a surprise, seems to violate the Principle of Least Astonishment. BTW, NOT within ARRAYFORMULA does not behave this way?
AND is an aggregation function, SUM behaves similarly in that it aggregates the whole range. NOT is not an aggregation so is expected to do element by element. I reckon there may be multiple cases where people have different expectations of what the right behavior should be with different aggregating functions. Always making it apply on the whole range gives a simple rule of thumb that aggregation doesn't change in arrayformulas. (I may be missing some counterexample here)
12
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.

Comments

7

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)) 

Comments

7

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

1 Comment

MAP is to me the most readable solution, and the most compliant with principle of least surprise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.