HomeGoogle DocsSpreadsheetOR in Multiple Columns in COUNTIFS in Google Sheets

OR in Multiple Columns in COUNTIFS in Google Sheets

Published on

In this tutorial, let me walk you through how to use the OR criterion in multiple columns in COUNTIFS in Google Sheets.

In conditional count, it’s easy to use multiple OR conditions (this or that) in one column.

I have explained that in one of my earlier Countifs-related tutorials here – Countifs with Multiple Criteria in Same Range in Google Sheets.

For example, we want to count column D if the text in that column is painting or driving.

In that case, we can use the below Array_Formula, Sum, and Countifs combination.

Formula # 1 – Correct ✔

=ArrayFormula(sum(countifs(D:D,{"Driving","Painting"})))

But the same trick may not work in multiple columns.

I know you badly require an explanation for this. Here you go!

Assume you want to extend the above formula to one more column.

That is, count column E for favorite authors Leo Tolstoy and Jane Austen from among a bunch of authors.

The following formula is not going to work!

Formula # 2 – Incorrect ✖

=ArrayFormula(sum(countifs(D:D,{"Driving","Painting"},E:E,{"Leo Tolstoy","Jane Austen"})))

Using OR in multiple columns in Countifs is quite tricky in Google Sheets.

We will use Regexreplace with the above functions (except Sum) for that. In this post, you can learn this trick.

Using OR in Multiple Columns in COUNTIFS

We have seen two OR in multiple column Countifs formulas above – one working and another non-working.

Here is the sample data used in those formulas.

Example to Using OR criterion in multiple columns in COUNTIFS

First of all, we will rewrite formula # 1, which is already returning the correct result, using our new method.

That will help us to rewrite the non-working formula # 2 easily. Before that, let’s understand the logic quickly.

Regexreplace in Countifs (Logic)

The first formula counts the text criteria Driving and Painting in column D.

Here we will substitute both criteria with a unique string, symbol, or character.

It’s up to you to choose the replace text/character/symbol. In my example, I am going to use the character “A.”

We will use the REGEXREPLACE function for the substitution task.

Then using Countifs, we will count the criterion “A” instead of the multiple OR criteria, i.e., Driving and Painting.

Syntax:

COUNTIFS(criteria_range1, criterion1, [criteria_range2, …], [criterion2, …])

Note:- The arguments within the square brackets are optional, and we don’t want them in our formula # 1.

Generic Formula # 1:

ARRAYFORMULA(COUNTIFS(regexreplace_formula_1, "A"))

criteria_range1 – regexreplace_formula_1

criterion1 – “A”

If there is one more column, we will do the substitution in that column also. But, the criteria will be substituted with another value, for example, “B.”

Generic Formula # 2:

ARRAYFORMULA(COUNTIFS(regexreplace_formula_1, "A",regexreplace_formula_2, "B"))

Note:- As already mentioned, the arguments within the square brackets are optional, and we will use them in our formula # 2.

Let’s apply OR in multiple columns in Countifs.

Formula Examples to Using OR in Multiple Columns in COUNTIFS

I have explained everything related to OR criteria in multiple columns in Countifs in Google Sheets.

Here is the replacement of Formula # 1 as per Generic Formula # 1.

One Column:

=ArrayFormula(countifs(regexreplace(D2:D,"(?i)painting|driving","A"),"A"))

Formula # 2 as per Generic Formula # 2 (it’s the replacement of our non-working formula given at the top of this tutorial).

Two Columns:

=ArrayFormula(countifs(regexreplace(D2:D,"(?i)painting|driving","A"),"A",regexreplace(E2:E,"(?i)Leo Tolstoy|Jane Austen","B"),"B"))

What about OR in a third column in Countifs?

I know, now you can do that yourself. I am leaving the generic formula for you to make it easier.

Three Columns:

ARRAYFORMULA(COUNTIFS(regexreplace_formula_1, "A",regexreplace_formula_2, "B",regexreplace_formula_3, "C"))

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

Related 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.

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

More like this

Interactive Random Task Assigner in Google Sheets

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

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

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.