ホーム 暮らし Super Convenient - The Magic to Switch Google Sheets COUNTIF, SUMIFS, and FILTER Functions to (Relatively) Strict Matching

Super Convenient - The Magic to Switch Google Sheets COUNTIF, SUMIFS, and FILTER Functions to (Relatively) Strict Matching

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 55 分
目次

I'm in the middle of covering the UNIQUE function in my advanced function application series, but I'm going to take a one-week break to cover a different topic and will continue with the UNIQUE function next time.

The reason is that the topic I'm introducing this time relates to advanced applications of the UNIQUE function.

It's a rare hack that you don't see often on social media or blogs, and it's a super easy and incredibly convenient technique that feels like magic.

Please, make use of it!

Last time, I wrote about the UNIQUE function and its derivative functions, the COUNTUNIQUE function and the COUNTUNIQUEIFS function.

What is the magic that changes COUNTIF and SUMIF(S) into relatively strict matching?

n17b0658e9e45

Since this is an easy hack you can use right away, let me give you the conclusion first.

Could you tell?

The COUNTIF function is counting how many items match D2 (Frieren: Beyond Journey's End) within the range A:A, but...

Add curly braces to the range.

Just by doing this, the matching criteria for 〇〇IFS functions like COUNTIFS and SUMIFS become (relatively) strict, allowing you to get the correct results.

Why? Even if you ask, even mir doesn't know the reason.

In the image above, there should be 2 cells that match "Frieren: Beyond Journey's End", but if you get the matching count with a normal COUNTIF, you get 8 which is an incorrect result as shown.

This is due to a troublesome specification in Google Sheets where it

"does not distinguish between hiragana, katakana, full-width, half-width, long vowel marks, and vowels"

resulting in loose matching.

However, by making the range A:A an array like A:A for the first argument, you can see that it changes to the correct (strict) judgment result of 2 as shown.

To summarize, it looks like this ↓

Since uppercase and lowercase letters are considered identical,"relatively" is the term I use for strict matching, but the troublesome specification of Google Sheets, "loose matching judgment," is resolved at once with this simple technique.

This is not just for cases where you use it for aggregation with COUNTIF(S) or SUMIF(S),

conditional formatting
extraction of duplicates using the FILTER function

and it can also be utilized in cases like these!

This time, let's take a look at the detailed operation and usage examples of this magic!


Matching in Google Sheets

First, let's look at the criteria for "matching" in Google Sheets, and the differences in behavior for each function.


It's super loose! Hiragana and Katakana matching judgment

Basically, when Google Sheets judges a match, it does not distinguish between full-width/half-width and hiragana/katakana.

You could say it has the generosity to absorb variations in notation, but if anything, there are probably more cases where you are troubled by misjudgments due to this loose matching criterion.

In the table, you can see that quite a few combinations, including "furiiren" and "フリイレン", which are quite far apart, are considered a match.

Furthermore,

it even judges a match in cases of long vowel marks (chōonpu) and vowels with a helpful design lol

"Rii" and "Rii" are treated as a match, but "Rii" and "Riu" are not a match, yet "Kuuru" and "Kuuru" are a match.

It is properly distinguishing the vowel of the character before the long vowel mark.


However, in cases like this where long vowel marks are consecutive, which is usually impossible, or cases where the character before the long vowel mark is a kanji, and also

seem to be distinguished. '~' and '~' are treated as the same.

What's strange is the matching rule for full-width and half-width prolonged sound marks

The half-width kana prolonged sound mark and the full-width prolonged sound mark are judged as a match when alone, but for some reason, if the same character (hiragana, katakana) precedes the prolonged sound mark, they are judged as different.

In the table at the beginning, this is why the half-width 'フリーレン' was not considered a match with the others.

It's a rare case, but if the preceding character is a kanji, it has no effect, and there is also no effect if a hiragana or katakana follows the prolonged sound mark.

It's quite peculiar.


Matching for alphabets, kanji, and numbers

In the case of English (alphabets), full-width and half-width are treated as the same, and uppercase and lowercase letters are also judged as a match without distinction.

Also, regarding the prolonged sound mark (hyphen), perhaps because what precedes it is not hiragana or katakana,

'frie-ren' and 'FRIE-REN'

are treated as a match. By the way, this English prolonged sound mark (hyphen) is different from the one used in katakana conversion.

There seem to be various differences, and there are explanatory sites as well.


Kanji can generally be considered to be distinguished unless it is a very special case.

Numbers are also considered the same regardless of full-width or half-width, including horizontal bars.

If there is a mix of full-width and half-width notation in phone numbers, it might be convenient because it will judge them as a match.

Finally, regarding spaces, while they are distinguished from blank or empty strings, half-width spaces and full-width spaces are treated as the same.


Functions in Google Sheets that use the same matching logic as equals

In fact, many major functions in Google Sheets use the same criteria as this loose = equals matching logic.

These very commonly used functions return results without distinguishing between hiragana/katakana, full-width/half-width characters, or uppercase/lowercase letters.

That is quite frightening, isn't it?

This also applies when extracting data that matches an equals condition using the FILTER function,

or when performing deduplication and sorting with the niche SORTN function

These can also lead to unintended results due to the loose matching logic.


Conditional formatting actually becomes loosely matched when set to "is exactly"

This loose matching logic is actually also used in conditional formatting.

As shown above, even though the condition is set to "Text is exactly" for "Frieren: Beyond Journey's End",

it even marks "Frieren: Beyond Journey's End" written in different scripts or widths as a match and highlights it.

Where exactly is it "exactly"...

However, if you use "Text contains", it actually becomes strictly matched.

Even when using cell references,

it becomes strict like this.

However, when you use text ending with...

this one becomes a loose match lol What on earth is this?

As for the verification results of matching in conditional formatting,

This is how it is. It seems the matching logic is different only when using containing/not containing.

The difference in matching logic above is not limited to conditional formatting; it is the same standard when used in Filter or Filter views under Filter by condition.

It's a troublesome situation.

For now, if you don't need to consider partially matching data,

It seems good to remember that.


Functions in Google Sheets that can determine strict matches

Conversely, what are the functions that can perform strict matching while distinguishing between hiragana/katakana, full-width/half-width, and uppercase/lowercase?

The main functions that can perform strict matching are

  • EXACT function

  • UNIQUE function (including COUNTUNIQUE, COUNTUNIQUEIFS)

  • QUERY function

These three.

Of course, string search and replacement functions like FIND,SUBSTITUTE, and REGEX-based functions also perform strict matching, but since these are basically for searching for strings within cells, they are excluded.


Functions in Google Sheets that can perform relatively strict matching

By the way, although it is a bit niche, DCOUNTA function,DSUM function, and DGET function, which are database-type functions, perform matching relatively strictly.

Although they treat uppercase and lowercase letters as the same, they distinguish between hiragana/katakana and full-width/half-width characters, so the accuracy is much better.

Depending on the target data, they are not a perfect substitute but

In this way, there are cases where you can substitute them to improve the accuracy of searches (matching).


Methods and effects of making matching relatively strict by using arrays

However, many people find database-type functions like DSUM and DCOUNTA a bit tricky to use and dislike them.. They often cannot be used unless the table has a well-defined structure..

Therefore, the magic introduced this time will be effective!


How to make a range an array

Regarding array conversion to use the magic, there are various methods.

To begin with, the difference between cell ranges and arrays... is difficult to write about strictly, so I will keep it simple here.

I think this understanding is fine. (Though experts might nitpick it...)

Now, for changing Range → Array for single-column data, you can use the TOCOL function, or for multiple columns and rows, there are also ways to use the FILTER function or the QUERY function,

but the easiest way is

This is the method of enclosing it in curly braces .


This array creation using curly braces has many uses in Google Sheets; for example, Google Sheets will

=A1:B20

result in an error if you try to output a range with a single formula like this.

=ARRAYFORMULA(A1:B20)

Of course, you can solve it by adding ARRAYFORMULA, but more simply,

=A1:B20

With just this, it becomes an array and is expanded into cells.


And, combining commas and semicolons in curly braces to vertically and horizontally connect cell ranges and arrays is also a very convenient way to use array creation with curly braces.

By the way, in Excel, you cannot use these curly braces to create arrays of cell ranges or to concatenate ranges.

Excel is designed so that only constants can be treated as arrays using curly braces.

Because of this, array operations in Excel have become overwhelmingly more convenient with the introduction of the new VSTACK and HSTACK functions.

For now, just remember that the easiest way to turn a range into an array in Google Sheets is to enclose the range in curly braces (for example, A1:B10 for A1:B10)!


Functions where matching behavior changes with array conversion

The functions where matching behavior becomes stricter when a range is converted into an array are, quite simply, XXIF and XXIFS functions.

This is effective for all of them, and when converted to an array, they return results based on matching that distinguishes between half-width, full-width, hiragana, and katakana characters.

In the image, the value in D2, “Sousou no Furiiren”, is used as the matching condition, but when range A:A is targeted

all of these are judged to match “Sousou no Furiiren”.

Only the half-width hyphen is treated specially in Sousou no FURIREN is not considered a match.

By changing this from A:A to A:A, you can see that only “Sousou no Furiiren” is considered a match, and the results are correct for everything.

It is interesting that it also has an effect on the obscure COUNTUNIQUEIFS function that I introduced last time.


By the way, in the case of SUMIFS and other XXIFS functions, since multiple conditions can be set as arguments, array conversion is required for each range.


The loose matching of COUNTIF and SUMIFS bugs out when combined with wildcards

Furthermore, this loose matching criterion can be very troublesome when performing wildcard-based "contains" checks.

When using standard XXIF or XXIFS functions with a specified range for a wildcard-based "contains" check, if there is a value that is considered a loose match above the value that perfectly matches the search term (D2) within the range, there is a bug where the matching data count becomes 0.

This is an outrageous bug!

As shown, if there is a cell containing a value that strictly matches the keyword earlier (above) in the range, the result of the loose matching check is returned. (This is not the correct result.)

It is a truly strange bug.


However, even when using "contains" with wildcards as a condition, by converting the range into an array using , the issue is resolved and it returns the correct result.

At this point, isn't it fair to say that array conversion is essential when using XXIF and XXIFS functions?


Weaknesses when using strict matching via array conversion in XXIF and XXIFS functions

As mentioned at the beginning, the effect of array conversion is that the matching check becomes "relatively" strict.

This allows for the distinction between full-width/half-width characters and hiragana/katakana, which were previously considered identical.

On the other hand, unfortunately, it cannot distinguish between uppercase and lowercase English letters.

Even if you turn the range into an array, both exact matches and matches containing wildcards will return results that treat uppercase and lowercase letters as identical as a result.

This is the difference between these functions and functions that can perform truly strict matching, such as the EXACT, UNIQUE, and QUERY functions.

That is why I use the expression (relatively) strict.

However, this specification is the same in Excel as well.

It goes without saying, but Excel's equality-based matching and the matching in XXIF and XXIFS functions properly distinguish between half-width and full-width characters, and between hiragana and katakana.

However

Even in Excel, when using equals, XXIF, XXIFS, VLOOKUP, XLOOKUP, etc., uppercase and lowercase letters are judged as identical.


In other words, in the case of Google Sheets, when you turn the range into an array, the XXIF and XXIFS functions have the same matching criteria level as those in Excel

is what that means.

By the way, it goes without saying that if you put an Excel file into Google Drive and open it in a browser, even if it is an xlsx file, it is in Google Sheets compatibility display mode.

Functions like COUNTIF and SUMIFS will also follow Google Sheets behavior.

Be careful, as things that were correct results when opened as Excel might result in incorrect results or errors.


Usage examples of array conversion + XXIF, XXIFS functions

The fact that COUNTIF, COUNTIFS, and SUMIFS matching becomes strict is useful enough on its own, but let's also touch on other usage examples.


Use Array + COUNTIF instead of equal matching

First, by replacing Google Sheets' loose equal matching with Array + COUNTIF, the matching becomes stricter.

Since it returns 1 for a match and 0 for a mismatch, you can use this directly with the IF function to process branches as TRUE or FALSE.

Also, even for conditional formatting where "perfect match" conditions still result in loose matching, you can use a custom formula:

By doing this, you can set formatting and color only for cells that strictly match the value in cell D2 (excluding case sensitivity).

* In this case, =COUNTIF(A2,$D$2) would also work, but since conditional formatting is viewed from the perspective of the cell to which the condition is applied, I think the main point is to check how many items in the fixed range D2 match the conditions A2, A3, A4, etc.

However, honestly, for this kind of processing, you could just use the EXACT function lol. That's easier anyway.

Well, it's a rare case, but I think it's effective when you don't want to distinguish between uppercase and lowercase but want to distinguish everything else.


Using Array + COUNTIF for comparing lists

For simple one-to-one comparisons, you don't need to rely on Array + COUNTIF; you can just use the stricter and clearer EXACT function.

When extracting matching data using the FILTER function, i.e., in many-to-one cases, you can also solve it with EXACT function + array.

Similarly, if you want to filter by strict matching with a many-to-one "contains" condition, it is also acceptable to use the FIND function or REGEXMATCH function in the condition part of the FILTER function.

However, what is difficult to handle with these functions is the many-to-many case.

For example, if you want to extract items from List A (A2:B) where column A overlaps with List B, as shown below,

Usually

You would combine the FILTER function with the COUNTIF function like this, but if the data contains mixed entries where only hiragana, katakana, half-width, or full-width characters differ, it will return incorrect results due to the looseness of the match criteria.

It is quite difficult to process such many-to-many relationships using the EXACT function, but if you use the array conversion + COUNTIF strictness technique, you can


do it without changing the formula much at all

This alone solves it!

Even if you want to highlight data that overlaps with List B using conditional formatting

You can use array conversion like this.

Of course, if you combine it with wildcards, it is also possible to highlight or extract data that contains List B with "relatively strict matching."

Have you realized how convenient this is?

Of course, if you need even stricter matching that distinguishes between uppercase and lowercase English letters, the EXACT function or UNIQUE function will be necessary.

I would like to touch upon this in the UNIQUE function advanced application series scheduled for next week.


Array conversion effect does not work on VLOOKUP or XLOOKUP

We have learned that by using array conversion, XXIF and XXIFS series functions become strictly matched and can be used more conveniently.

However, conversely,unfortunately, this has no effect on other functions! (as far as I have verified)

What is particularly disappointing is that ithad no effect on search-related functions such as VLOOKUP, XLOOKUP, MATCH, and XMATCH.


Verification: Array conversion + VLOOKUP, XLOOKUP, MATCH, XMATCH

Even if you convert the second argument search range into an array like A2:A or A2:B in this way,it has no effect at all, and it will judge "Frieren: Beyond Journey's End" in the first row of the range (cell A2) as a match for "Frieren: Beyond Journey's End (typo)".

If it were Gen from Barefoot Gen, he would go"Gigi-gi...".

It is not impossible to force these VLOOKUP, XLOOKUP, MATCH, and XMATCH functions to process by combining array conversion + COUNTIF.

However, using the original search key in the COUNTIF range

COUNTIF(D2,A2:A)

is a bithard to understand, and since ARRAYFORMULA is required for the array processing in this part, the formula becomes long, which is not ideal.

It seems better to give up and just combine it with the EXACT function.


Method using the DGET function instead of VLOOKUP or XLOOKUP

It has some quirks in how to use it and requires a header row, but if you want to do something like VLOOKUP with strict matching, there is also a method using the DGET function.


As I mentioned in the first half, database functions like DGET, DCOUNT, and DSUM perform relatively strict matching in Google Sheets.

Among the database functions, the one that behaves most similarly to VLOOKUP and XLOOKUP is this DGET function.

However, the way of writing the formula is unique and hard to understand, isn't it?

In particular, the criteria section requires "search column header" ; "search key" , a vertically concatenated array, which is quite hard to get used to.

That said, you just need to fit it into the pattern, so even if there are no header rows or if there is only a search key in the cell,

there is also a way to create an array within the formula like this and add a header row as needed.

However, note that the DGET function returns an error if there are two or more pieces of data that match the search key within the range.

It cannot perform processing like VLOOKUP or XLOOKUP, which return the first result found.


Ultimately, regarding VLOOKUP, XLOOKUP, MATCH, and XMATCH, there is no single best way to make loose matching strict, so depending on the situation, you have to choose between methods like

  • combining VLOOKUP or XLOOKUP with the EXACT function

  • using the DGET function

  • using the QUERY function

and so on.


"It's okay to be loose, it's free after all." Mitsuo

This time, I introduced the looseness of matching in Google Sheets, and a magic technique, "array creation using curly braces," which, while limited to XXIF and XXIFS functions like COUNTIF and SUMIFS, makes the matching relatively strict.

However, as I touched upon at the end, it is a shame that it had no effect on VLOOKUP, XLOOKUP, MATCH, and XMATCH, which are functions used even more frequently than COUNTIF and SUMIFS.

Updated October 2024

↑ Since then, I have compiled a note on the magic to make XLOOKUP relatively strict.


It is certainly a crappy specificationthat might make Excel users say, “This is why Google Sheets sucks,” but please try to look at it positively.

Excel doesn't have functions that treat hiragana/katakana, full-width/half-width, uppercase/lowercase, or even long vowels and vowels like “Frieren” and “Frii-ren” as matches, right?

Furthermore, it is quite difficult to combine sheet functions to create formulas that consider things as matches with such loose criteria, isn't it?

When you think about it that way, doesn't it make you feel like, “Google Sheets can do things Excel can't!” lol

Well, this might be a bit of a stretch, but

I feel like this one phrase solves everything lol

Well, if you get creative, “the way will open,” and it's also nice to enjoy the inconvenience. (It gets exciting when it's improved in an update, after all.)

Next time, I will return to the UNIQUE function series and write about super advanced examples using the UNIQUE function.


同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事