简体   繁体   English

计算excel表中的行,其中列满足多个条件(模拟多重过滤条件)

[英]Count rows in an excel table where columns satisfy multiple criteria (simulating multi-filter criteria)

I am wondering if there is a simple way to count the number of rows after filtering a subset of columns by value considering multiple selections for a given column in an excel table object. 我想知道是否有一种简单的方法可以通过考虑对excel表对象中给定列的多个选择来通过值过滤列的子集后计算行数。

Let's say I have the following excel table: 假设我有以下excel表:

A   B   C
a1  b1  c1
a2  b2  c1
a1  b2  c2
a2  b1  c2
a1  b3  c3
a3  b1  c3

saved in an excel table under the name: Table1 and I would like to find all rows that the column A has the value a1 or a2 and column B has the value: b1 . 保存在excel表中,名称为: Table1 ,我想查找列A的值为a1a2且列B的值为b1所有行。 The result should be 2. 结果应为2。

I am able to do it using SUMPRODUCT function and converting the logical value into [0,1] using the -- operator: 我可以使用SUMPRODUCT函数并使用--运算符将逻辑值转换为[0,1]来做到这一点:

= SUMPRODUCT(--(Table1[A]="a1"),--(Table1[B]="b1")) 
   + SUMPRODUCT(--(Table1[A]="a2"),--(Table1[B]="b1"))

In my real example, I have more than three columns and at least one of them can satisfy multiple criteria so I am wondering if there is a way to do it with a less verbose syntax. 在我的实际示例中,我有多于三列,并且其中至少一列可以满足多个条件,所以我想知道是否有一种方法可以使用不太冗长的语法来做到这一点。 For example, I was trying something like this, but it does not work: 例如,我正在尝试类似的方法,但是它不起作用:

= SUMPRODUCT(--(Table1[A]="a1|ab2"),--(Table1[B]="b1"))

or 要么

=SUMPRODUCT(--(Table1[A]=OR("a1", "a2")), --(Table1[B]="b1"))

the OR function does not help, because it does not return an array result, and I cannot use the array formula in my real example because I would need to apply just for one column with more than one selection, but for the rest of the columns I am selecting it is just a single value. OR函数无济于事,因为它不返回数组结果,并且在我的实际示例中我无法使用数组公式,因为我只需要对具有多个选择的一列进行申请,而对其余列进行申请我选择的只是一个值。

It seems to be a good trick for representing in an excel formula a multiple filter criteria action, but the excel formula is very verbose when within a column it has to satisfy more than one condition, like in the above example. 在一个excel公式中表示多个过滤条件动作似乎是一个好技巧,但是当excel公式必须在一列中满足多个条件时,excel公式就非常冗长,如上例所示。

Under my solution, it would something like this for counting rows in a Table where for each column we filter by only one value except for the first column A that we filter by two possible values: 在我的解决方案下,对表中的行进行计数将是这样的,其中对于第一列,我们仅过滤一个值,但对第一列A进行过滤时,我们会过滤两个可能的值:

= SUMPRODUCT(--(Table1[A]="a1"),--(Table1[B]="b1"), 
    --(Table1[C]="c1"), ...Table1[Z]="z1"))
  + SUMPRODUCT(--(Table1[A]="a2"),--(Table1[B]="b1"), 
    --(Table1[C]="c1"), ...Table1[Z]="z1"))

Try: 尝试:

=SUMPRODUCT((Table1[A]="a1")+(Table1[A]="a2"),--(Table1[B]="b1"))

Since any given cell's cannot both be "a1" and "a2", the sum will be 1 if either is true and 0 if neither is true 由于任何给定的像元都不能同时为“ a1” “ a2”,因此如果两个为真,则总和为1如果两个都不为真,则总和为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM