Mark rows with logical tests
In this example, the goal is to mark or flag certain records in a data set based on one or more logical conditions. In each case, the result should be "Y" for "Yes" or "N" for "No". The data represents drinks purchased by five people on different days. Note that most people appear more than once in the data. For each name, we are checking for three separate conditions:
- Have they purchased tea?
- Have they purchased coffee?
- Have they purchased both coffee and tea?
Note these conditions apply to all the data available for each person. To make the formulas easier to set up, we have two named ranges to work with: name (B5:B16) and drink (D5:D16). Also, note that we are using names as a unique identifier for convenience only. Normally, data like this will include some sort of unique ID for each person.
To check if a person has ever purchased coffee, we need to look for a specific name in the same row as "coffee". Essentially, we want to count rows where a given name appears with "coffee". We can do this with the COUNTIFS function configured to with two conditions like this:
COUNTIFS(name,B5,drink,"coffee") // returns 3
We are checking for the name in B5 ("Juan") in names (B5:B16), and "coffee" in drinks (D5:D16). COUNTIFS joins these conditions with AND logic, so the result is a count of all records where the name is "Juan" and the drink is "coffee" (3). To get a result of "Y" for "Yes" or "N" for "No", we nest the COUNTIFS function inside the IF function like this:
Note we are using the COUNTIFS formula as the logical test inside the IF function. COUNTIFS won't return TRUE and FALSE, but rather a number that represents a count. When supplied conditions are met, the result will be a non-zero number. If no records meet supplied conditions, COUNTIFS will return zero. Excel will evaluate any non-zero number as TRUE and zero as FALSE. In cell E5, the formula is solved like this:
Note the result for "Juan" and "Coffee", "Y", will be the same in all rows where the name is "Juan". This is a global test applied with all data considered.
To check if a person has ever purchased tea, we use the same approach. In this case, however, we want to check for rows where a given name appears with "tea". The formula in cell F5 is:
Note the structure is the same as the formula in cell E5: COUNTIFS is used to count records that meet two conditions and delivers a numeric count to the IF function, which returns a final result. In cell F5, the formula is solved like this:
Note in this case the count from COUNTIFS is zero, since Juan never purchased tea, and IF evaluates zero as FALSE and returns "N".
Coffee and Tea
To check if a person has purchased both coffee and tea, we can use the AND function for the logical test. The formula in G5 is:
Here, we are using the AND function to check the already calculated results in columns E and F for convenience and efficiency. For a standalone version of this formula, we could use the COUNTIFS function twice in one formula like this:
As before, the AND function will evaluate any non-zero number as TRUE and zero as FALSE. In cell G5, the formula is evaluated like this:
The result is "N" because although Juan has purchased coffee 3 times, he has never purchased tea.