HomeGoogle DocsSpreadsheetOR in COUNTIFS in Either of the Columns in Google Sheets

OR in COUNTIFS in Either of the Columns in Google Sheets

Published on

This post explains how to use OR criteria in COUNTIFS in either of the columns in Google Sheets.

The best way to solve this is using QUERY. But if you wish to use COUNTIFS, you may want to use it not in the usual way.

I will share both formulas here. Before that, here is the scenario to understand the problem.

Assume five teams compete with each other in a tournament.

So there are two columns (C and D) in a spreadsheet with the following data.

Team (column C) Vs Team (Column D):

OR in Countifs in Either of the Columns - Example

I want to see how many matches have been played by teams A or B so far.

In other words, whether the text/string A or B matches in either of the columns in each row.

As per the above example, there are corresponding data in 7 rows.

One match A & B plaid against each other and six matches with the other teams.

So the output must be 7, not 8.

To get this, we must know how to use OR criteria in COUNTIFS in either of the columns.

You May Like:- How to Use All Google Sheets Count Functions [All 8 Count Functions].

How to Use OR in COUNTIFS in Either of the Columns in Google Sheets

For counting based on more than one condition, the dedicated function is COUNTIFS.

How to use it for our purpose here?

I’ve earlier posted two related tutorials.

  1. COUNTIFS with Multiple Criteria in the Same Range in Google Sheets.
  2. OR in Multiple Columns in COUNTIFS in Google Sheets.

They are slightly different from our new problem, i.e., OR in COUNTIFS in either of the columns.

Formula_1:

=ArrayFormula(SUM(COUNTIFS(if((C2:C="A")+(C2:C="B"),"~","")&if((D2:D="A")+(D2:D="B"),"~",""),{"~","~~"})))

The above is the solution to OR in COUNTIFS in either of the columns in Google Sheets.

How Does This Formula Work?

If you check either of the above tutorials, preferably the first one, you can understand the basics.

There I have used a virtual array formed using Curly Braces to use OR in one column.

Here is a quick example.

We can use the following formula to count the fruits “dragon fruit” or “papaya” in column A.

Generic Formula for OR in COUNTIFS in Either of the Columns: =ArrayFormula(SUM(COUNTIFS(criteria_range,{criterion_1,criterion_2,...})))

=ArrayFormula(SUM(COUNTIFS(A:A,{"dragon fruit","papaya"})))

criteria_range – A:A

criterion_1 – “dragon fruit”

criterion_2 – “papaya”

We have followed this in formula_1 above after virtually modifying the source data.

I’ll explain the steps with the help of a few helper columns.

Insert the following formula in cell F2.

=ArrayFormula(if((C2:C="A")+(C2:C="B"),"~",""))

It matches the teams’ A or B in column C and returns a tilde mark in matching rows.

Insert the below formula in cell G2 to match teams A or B in column D and return a tilde.

=ArrayFormula(if((D2:D="A")+(D2:D="B"),"~",""))
Formula Logic - Criteria_Range Steps

Related: How to Use IF, AND, OR in Array in Google Sheets.

When you combine both, we will get our criteria_range.

=ArrayFormula(if((C2:C="A")+(C2:C="B"),"~","")&if((D2:D="A")+(D2:D="B"),"~",""))

So the criteria will be as follows.

criterion_1 – “~”

criterion_2 – “~~”

I hope it goes without saying.

OR in COUNTIFS in Either of the Columns – Alternative Formula Using Query

We cause a QUERY alternative in Google Sheets to solve the OR in COUNTIFS in either of the columns’ problems.

Here is that formula.

=query(C2:D,"select count(C) where C matches 'A|B' or D matches 'A|B' label Count(C)''")

As you can see, to get the count as per our requirement, I have used the Matches regular expression match, one of the complex comparison operators, in the Where clause.

That’s all. Thanks for the stay. Enjoy!

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Creating Custom Descending Sequence Lists in Excel

You can utilize functions like ROW or SEQUENCE in Excel to create a custom...

SUMIFS with XLOOKUP in Excel and Google Sheets

SUMIFS and SUMIF are infamous for producing an "argument must be a range" error...

Counting XLOOKUP Results with COUNTIFS in Excel and Google Sheets

We can use COUNTIF or COUNTIFS alongside an XLOOKUP formula to conditionally count the...

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

More like this

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.