首页 > 解决方案 > 带有包含字符和 OR 逻辑的文本的 COUNTIF/COUNTIFS 无法按预期工作

问题描述

我在谷歌表格中有以下列示例,我试图查看有多少人(在 BI 栏中有名字) -如果我选择某些日子,将被排除在外:

     A
-----------
s, m
m, w
s, m

所以基本上如果我选择一天f(星期五)或t(星期二) - 所有人都无法参加 如果我选择一天fw- 只有一个人能够参加。

遵循文档中的以下示例,括号{}应该这样做OR- 但结果根本没有意义

这是怎么回事 ?

查看https://www.got-it.ai/solutions/excel-chat/excel-tutorial/countifs/countifs-with-multiple-criteria-and-or-logic

它说count items which have the month January or March(或{"January", "March"}

Count Items with Multiple Criteria and Or Logic
In our example, we want to count items which have the month January or March and Product ID 1001 or 1002.

The formula looks like:

=SUM(COUNTIFS(B3:B13, {"1001", "1002"}, C3:C13, {"January", "March"}))

The parameter criteria_range1 is B3:B13 and the criteria1 is the array  {“1001”, “1002”}. The parameter criteria_range2 is C3:C13 and the criteria2 is {“January”, “March”}. The result of this COUNTIFS function is 2 numbers, which are number parameters of the SUM function.

标签: google-sheetsgoogle-sheets-formula

解决方案


这一点也不奇怪。与{}您一起创建一个值数组。由于您可能没有 Excel O365,您会看到返回数组的隐式交集,或者换句话说,第一个值。

在您的示例=COUNTIF(A1:A3, {"*w*", "*s*"})中,生成的数组{1,2}因为*w*只存在一次和*s*两次。所以这个数组的隐式交集显示你只是1. 如果您显然交换标准,您会看到2.

在这种情况下,您通常会使用SUM(COUNTIF(A1:A3, {"*w*", "*s*"}))return 3。但请注意,这样做会返回5for SUM(COUNTIF(A1:A3, {"*m*", "*s*"})),因为它的结果数组将为{3,2}. 因此,在逗号分隔值的情况下,我会选择一些不同的方法。

如果您使用的是 Google 电子表格,可能类似于以下内容:

=IFERROR(QUERY(A:A,"Select Count(A) where A like '%s%' or A like '%x%' label Count(A) ''"),0)

做你想做的事?


推荐阅读