Let's look at the common logic functions like AND, OR, NOT and XOR that you might think are easy, but I'll show you their limitations and the alternatives.
Â
Plus, I'll show you why you don't need to even use IF a lot of the time and what to do instead, which can vastly improve your
formulas.
Â
Lastly, I'll show you under the hood of how Excel uses logic in arrays, enabling you to perform calculations you can't do with the built-in logic functions.
Â
Along the way I'll drop some tips and tricks that will take your Excel skills to pro level.
Â
How Excel Handles Logic
The logic functions, AND, OR, NOT and XOR all return a single TRUE or FALSE result. These are Boolean values and in Excel they are a data type all their own.
Â
Boolean logic is a form of algebra where all values are reduced to either TRUE or FALSE. In Excel, TRUE can
also be represented with 1 and FALSE with 0.
Â
AND Function
Let’s say you’re hosting an exclusive preview of your new product line for your most loyal customers. Invitations will be sent to those who have spent $100 or more. and have 300 or more points.
Â

Â
In column F of the table below I've used the AND function to find which customers qualify for an invitation:
Â
The AND function takes a series of logical tests and returns TRUE where all tests are TRUE, and FALSE where any test is FALSE.
Â

Â
The limitation is I want it to return a number so that I can add up how many invitations I need to issue.
Â
I can do this with the double unary, which is simply two minus signs. The double unary coerces the TRUE and FALSE values into their numeric equivalents of 1 and 0 like so:
Â

Â
And now I can easily add them up. Another way I can add them up
without coercing them first, is inside the SUM function like so:
Â
=SUM(--F6:F15)
Â
However, I don't even need to use the AND function. I can simply write the logical tests inside parentheses and multiply them by one another:
Â

Â
Notice here I haven't used the double unary, and this is because performing a math operation on Boolean values also coerces them into their numeric
equivalents.
Â
Best Formula: At this point you're probably wondering which is best and the answer is, whichever you prefer.
In this scenario there's no clear winner, but there are some limitations to the AND function that you don't get with the pure logical tests.
Â
Limitations: With all these solutions I still need to calculate the logical tests one row at a time before I can add them up to find out how many invitations I'm sending out.
In this scenario it makes sense to do so because I need to identify which customers to send invitations to vs just
finding out how many to print. Â But that's not always the case and I'll share a solution to this later.
Â
Takeaways:
- The double unary coerces TRUE and FALSE values into their numeric equivalents,
- as do
math operations.
- Multiplying logical tests is the same as using the AND function.
Â
OR Function
Your next promotion is to send a discount coupon to individuals who either have spent more than $100 OR have over 300 points (or both).
Â
Below I've used the OR function to handle the logical test and return the results.
Â
The OR function takes a series of logical tests and returns TRUE where one or more tests are TRUE, and FALSE where all tests are FALSE.
Â

Â
Again, I can coerce the Boolean TRUE and FALSE values to their numeric equivalents with the double unary:
Â

Â
And I can write it without using OR by adding the logical tests like so:
Â

Â
However, notice that when both logical tests are TRUE the
result is 2 because TRUE+TRUE is the same as 1+1.
In some cases this might be the desired result.Â
Â
However, here I only want to give the customers who met either criterion, one briefing. It wouldn't make sense to send out the briefing twice.
Â
A solution to this is to wrap the logical tests in the SIGN function. SIGN converts positive values to 1, zeros to 0 and negative values to -1.
Â

Â
Best Formula: In this scenario there's no benefit to using the SIGN function over the OR function, but later we'll see where SIGN may be useful.
Â
Takeaways:
- Adding logical tests treats them as OR criteria.
- The SIGN function can be used to convert results to 1 where more than one logical test is TRUE.
Â
NOT Function
Next, we’ll target customers who have NOT made a
purchase yet with a special introductory offer to encourage their first purchase.
Â
We can identify customers who haven't made a purchase using the NOT function:
Â

Â
Of course, we can also write this with a simple logical test like so:
Â

Â
Let's say we wanted to identify customers who had made a purchase. It could be written with NOT like so:
Â
=NOT(D6=0)
Â
Or as a logical test:
Â
=D6<>0
Â
Best Formula: Personally, I find the double negative in a NOT function more difficult to write than the simple logical test
above.
Â
XOR Function
The XOR function stands for ‘Exclusive Or' and returns TRUE when the number of TRUE inputs is odd and FALSE when the number of TRUE
inputs is even.
Â
The easiest way to think of XOR is with the light switch scenario. Let's say we have two light switches that control the same light, one at each end of a hall or staircase. The light is only on when the two switches are in different positions.
Â
In the table below,
where the light status is TRUE, the light is on, and FALSE it's off:
Â

Â
For our next promotion we’ll invite customers who have either spent less than $100 or have less than 200 points, but not both, to a preview event for a new product.
Â
We could write it like so:
Â

Â
It could also be written with logical tests nested in ISODD:
Â

Â
Best Formula: In this scenario I'd guess that XOR might be more efficient to calculate that the pure logical
tests inside ISODD, but it probably wouldn't be noticeable on less than 100k rows of data.
Â
If, however you want to return an array of Boolean values, then ISODD is the winner because XOR can only return a single result. More on returning arrays in a moment.
Â
IF Alternative
This time we want to give
each customer who has not made a purchase a super bonus of 2 Invitations to our next product launch, so they can bring a plus 1.
Â

Â
However, when we require a numeric result from a logical test, the IF function is often redundant.
Â
In this case I could simplify this formula like so:
Â

Â
No double unary is required because I'm multiplying the result of the logical test by 2, which coerces it into its numeric
equivalent.
Â
Best Formula: There's no standout winner here, but I'd hazard a guess that the pure logical test will be slightly faster for Excel to calculate, but not significantly on small datasets.
Â
Takeaways: IF is often not required when the outcome of the formula is numeric.
Â
Nested IF/IFS Alternative
Nested IFs or the newer IFS function enable you to perform a
logical test, return a result if the test is TRUE, if not, it will move on to the next logical test and so on until it finds a TRUE outcome.
Â
In this scenario we want to reward customers with a coupon worth 20% of their last purchase where they spent >$200 or 10% of their last purchase where they spend >$100.
Â
We can write this with a Nested IF or the newer IFS function:
Â

Â
Or with the older nested IFs like so:
Â
=IF(D6>200,D6*0.2,IF(D6>100,D6*0.1,0))
Â
Or we could simplify it with pure logical tests:
Â

Â
Note here that both logical tests are performed, hence why both only calculate 10% of the coupon value. Because once combined, customers who spent >$200 will receive 20% in
total.
Â
Best Formula: The nested IF formula will be more efficient for Excel to calculate because they stop calculating at the first logical test that returns TRUE, whereas the pure logical test formula and IFS formula will always calculate all logical tests.
Â
Takeaway: IFS and Nested IF formulas stop calculating at the first logical test to return TRUE. This means it's crucial to get the order of logical tests correct. For more on this, check out my comprehensive tutorial on IF, Nested IF
and IFS formulas.
Â
Arrays
OR, AND, XOR and NOT can only return a single value. i.e., they can't spill results or return arrays of Boolean values when nested inside other functions.
Â
Therefore, when using functions like FILTER, SUM and SUMPRODUCT among others, that can take an array of Boolean TRUE and FALSE values (or their numeric equivalents of 1 and 0), we need to use the pure logical tests with * for AND, and + for OR.
Â
Scenario 1: Let’s say we want to know how many customers spent >$100.
Â
In the image below notice the range being referenced contains all the sales (D6:D15), which are then tested to see if they're > 100. The resulting formula spills the results i.e., it returns an array of Boolean values:
Â

Â
I can also do this in a single cell. The double unary shown in the image below coerces the TRUE and FALSE results from the logical test to their numeric equivalents of 1 and 0 for SUM to add up.
Â

Â
Scenario 2: Lets find the value of purchases where customers spent
more than $100 OR have more than 200 Points.
Â
With pure logical tests I can reference the whole range of purchases (D6:D15) and Points (E6:E15) and perform the logical test on the array, which returns arrays of results.
Remember, with OR criteria I must wrap the logical tests in the SIGN function where there's a
possibility of double counting.
Â
Here I'm performing my logical tests on different columns, which means there is a risk that both criteria could be TRUE which would result in double counting.
Â

Â
The formula above evaluates like so. The logical tests are evaluated to return arrays of Boolean results:
Â

Â
The arrays of Boolean values are then added together:

Â
SIGN then converts the positive values to 1 and zeros remain as is:
Â

Â
The two arrays are then multiplied by one another to leave an array of values for SUM to add up:
Â
=SUM({250;50;0;0;0;150;75;120;0;90})
Â
=735
Â
Scenario 3: We want a list of customers whose last purchase is >$100 OR that have more than 200 Points. I can use the FILTER function for this which is available in Excel 2021
onward.
Â
The FILTER function references the array (cell range) you want filtered, you then provide it with logical tests that tell it which rows you want returned.
Â
It has 3 arguments, the last being optional, which I won't use in this example:
Â
=FILTER(array, logical tests specifying which rows to include, [if empty])
Â

Â
Twist: notice I haven't wrapped the logical tests in the SIGN function! This is because all I need to provide FILTER with are Boolean TRUE and FALSE values or their numeric equivalents.
Â
I'm performing a math operation on my two
logical tests which will convert them to their numeric equivalents of 1 and 0, and when added together it evaluates to this:
Â
=FILTER(B6:E15,{2;1;0;0;0;2;1;2;0;1})
Â
And here's the thing 99.9% of Excel users don't realise:
Excel will treat any positive or negative number as TRUE where it requires a Boolean value.
Â
With FILTER there's no risk of it returning a row twice, it simply wants to know if a row should be included or not. Therefore, I don't need to use the SIGN function to convert the array to 1s and 0s.
Â
Bonus Tip: this also applies anywhere Excel operates on Boolean values, including Conditional Formatting rules.
This is just the tip of the iceberg when it comes to FILTER.Â
Â
It's
one of my favourite new functions in Excel and now that you have logic in Excel mastered, you're ready to maximise the use of FILTER do some amazing things. Go ahead and learn FILTER next:
Â
Excel Filter Function
Comprehensive Tutorial including Hacks
Â
Next Steps
When you understand the fundamentals of Excel functions and formulas it enables you to work more creatively and efficiently.
Â
Build on what you've learned here in my Advanced Excel Formulas course and start standing out from the crowd with skills that will get you noticed and promoted.
Â
