Summary

To mark or flag data records directly based on logical tests, you can use a formula based on the COUNTIFS function and the IF function. In the example shown, the formula in cell E5 is:

=IF(COUNTIFS(name,B5,drink,"coffee"),"Y","N")

where name (B5:B16) and drink (D5:D16) are named ranges.

Generic formula

=IF(COUNTIFS(range1,criteria1,range2,criteria2),"Y","N")

Explanation 

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:

  1. Have they purchased tea?
  2. Have they purchased coffee?
  3. 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.

Coffee

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

=IF(COUNTIFS(name,B5,drink,"coffee"),"Y","N")

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:

=IF(COUNTIFS(name,B5,drink,"coffee"),"Y","N")
=IF(3,"Y","N")
="Y"

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.

Tea

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:

=IF(COUNTIFS(name,B5,drink,"tea"),"Y","N")

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:

=IF(COUNTIFS(name,B5,drink,"tea"),"Y","N")
=IF(0,"Y","N")
="N"

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:

=IF(AND(E5="Y",F5="Y"),"Y","N")

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:

=IF(AND(COUNTIFS(name,B5,drink,"coffee"),COUNTIFS(name,B5,drink,"tea")),"Y","N")

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:

=IF(AND(COUNTIFS(name,B5,drink,"coffee"),COUNTIFS(name,B5,drink,"tea")),"Y","N")
=IF(AND(3,0),"Y","N")
=IF(FALSE,"Y","N")
="N"

The result is "N" because although Juan has purchased coffee 3 times, he has never purchased tea.

Dave Bruns Profile Picture

AuthorMicrosoft Most Valuable Professional Award

Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.