简体   繁体   中英

COUNTIFS column A or B as range

I am struggling hard with the logic here.. COUNTIFS with OR condition for the criteria is fairly simple:

=SUM(COUNTIFS(range,criteria range1,{criteria1,criteria1.1},criteria range2,criteria2))

Though.. what if I need to have an OR condition on the range itself?

I have two columns containing week number for "Attempt 1" and "Attempt 2", then one with status, so my formula looks like this:

=SUM(COUNTIFS(raw[week1],refToWeekNumberCell,raw[status],{"Success","Try again","Fail","Invalid"}))

..so I'm looking to count how many "Invalid" or "Valid" (all the other statuses) I have at any given week. If the status is "Invalid" there will only ever be one attempt. So checking against the column "Week 1" is enough. If I however want to check number of "Valid attempts", I will need to check against both the "Week1" and "Week2" column.. Can this be done somehow? Do I need a helper column, and if so, how do I formulate that one?

Sample data:

Raw table:

Week1 Week2 Status
7 7 Success
7 - Success
7 - Invalid
7 8 Success
7 8 Success
8 8 Success

Table seems to look good in preview, but not when posted, so: 在此处输入图像描述

Expected result: Count of Week 8 that are not Invalid either in column "Week 1" OR "Week2". Since Invalid always only have anything under "Week 1" then that is easy. But on row 5, the logic fails as I can only use one column as the range..

If it doesn't get much more complicated than two columns, you could use:

=SUM(IF((A2:A7=8)+(B2:B7=8)>=1,1,0)*(C2:C7<>"Invalid"))

The "+" is typically used to act as some sort of "Or" validation inside such boolean-structures. However, if you have more columns, the MMULT() variant is much easier to maintain.

=SUM(--(MMULT((A2:B7=8)*(C2:C7<>"Invalid"),{1,1})>0))

As you are using Excel365 then try-

=COUNTA(FILTER(C2:C7,MMULT((A2:B7=8)*(C2:C7<>"Invalid"),SEQUENCE(COLUMNS(A2:B2)))))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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