How to COUNTIFS with an array of criteria and match NOT condition? - excel

My data table is like the image above. I can easily count the number of Male participants in group B or C using this array formula:
=SUM(COUNTIFS($B:$B, $E3, $C:$C, $F3:$F4))
The result is 3 as expected. However I'm gonna do the reverse thing, that is count the number of Male participants in NOT group B or C. The result should be 1 but currently I'm stuck at this.
Can anybody show me a way please (preferably not just counting the number of all Male participants and then do a subtraction)? I have even tried to change the values in the Group to something like <>B and <>C but it just doesn't work.

As you only have 2 in the group you can easily use COUNTIFS with 2 separate criteria, i.e.
=COUNTIFS($B:$B,$E3,$C:$C,"<>"&$F3,$C:$C,"<>"&$F4)
but clearly that might not be desirable for a large group, so you could use SUMPRODUCT like this to reference the group once
=SUMPRODUCT(($B:$B=$E3)*ISNA(MATCH($C:$C,$F3:$F4,0)))
ISNA will exclude matching rows - to include use ISNUMBER
You can replace F3:F4 with any single row or column of values
Note: whole columns with SUMPRODUCT will work (post Excel 2003) but is undesirable as Jerry says

Hmm, the thing with the formula right now is that the first COUNTIFS (for F3) will return 2 and the second COUNTIFS (for F4) will return 3, which SUM converts into 5 when you try:
=SUM(COUNTIFS($B:$B, $E3, $C:$C, "<>"&$F3:$F4))
I would suggest using SUMPRODUCT instead:
=SUMPRODUCT(($B:$B=$E3)*($C:$C<>$F3)*($C:$C<>$F4))
And maybe make the range smaller since this can take some time (you don't need to insert this as an array formula).
Otherwise, another option would be to count all the Males, and then subtract the counts for group B and subsequently C:
=COUNTIF($B:$B, $E3)-SUM(COUNTIFS($B:$B, $E3, $C:$C, $F3:$F4))

Related

Excel - How to count unique records with specified condition in a column?

I have a table like following:
And I want to count for each store, how many bills purchased single Item ? How many purchased 2 and full set of items ? The result should look like below
Where in store 'm3', bill 300 bought 2 item C but only count for 1 in the final result. This is where I struggle with, since I tried
COUNTIFS(A:A, "=m1", B:B, "=A") (then add with count for B and C) to get single item for store m1 but unable to figure out how to distinguish with the unique bill numbers.
Please don't mind asking if needed more clarifications, and I do prefer excel in-built functions rather than VBA.
Using Office 365 formula we start with the basic formula to find the singles:
=BYROW(E2:E4,
LAMBDA(z,SUM(--(
BYROW(--(COUNTIFS(A:A,z,C:C,UNIQUE($C$2:$C$8),B:B,{"A","B","C"})>0),
LAMBDA(a,SUM(a)))=1))))
This will give you the single results:
The others are all variations of that formula:
A,B
=BYROW(E2:E4,
LAMBDA(z,SUM(--(
BYROW(--(COUNTIFS(A:A,z,C:C,UNIQUE($C$2:$C$8),B:B,{"A","B"})>0),
LAMBDA(a,SUM(a)))=2))))-J2#
B,C
=BYROW(E2:E4,
LAMBDA(z,SUM(--(
BYROW(--(COUNTIFS(A:A,z,C:C,UNIQUE($C$2:$C$8),B:B,{"C","B"})>0),
LAMBDA(a,SUM(a)))=2))))-J2#
A,C
=BYROW(E2:E4,
LAMBDA(z,SUM(--(
BYROW(--(COUNTIFS(A:A,z,C:C,UNIQUE($C$2:$C$8),B:B,{"C","A"})>0),
LAMBDA(a,SUM(a)))=2))))-J2#
All
=BYROW(E2:E4,
LAMBDA(z,SUM(--(
BYROW(--(COUNTIFS(A:A,z,C:C,UNIQUE($C$2:$C$8),B:B,{"A","B","C"})>0),
LAMBDA(a,SUM(a)))=3))))
We have to add the -J2# to the end of the doubles to remove when it has all three. It will count in both places.
With Older Versions of Excel we need to do some gymnastics with SUMPRODUCT, MMULT, INDEX, MODE.MULT, etc
=SUMPRODUCT(
--(MMULT(
--(COUNTIFS(A:A,E2,C:C,INDEX(C:C,N(IF({1},MODE.MULT(IF(MATCH($C$2:$C$8,C:C,0)=ROW($C$2:$C$8),ROW($C$2:$C$8)*{1,1}))))),B:B,{"A","B","C"})>0),
{1;1;1})=1))
=SUMPRODUCT(
--(MMULT(
--(COUNTIFS(A:A,E2,C:C,INDEX(C:C,N(IF({1},MODE.MULT(IF(MATCH($C$2:$C$8,C:C,0)=ROW($C$2:$C$8),ROW($C$2:$C$8)*{1,1}))))),B:B,{"A","B"})>0),
{1;1})=2))-J2
=SUMPRODUCT(
--(MMULT(
--(COUNTIFS(A:A,E2,C:C,INDEX(C:C,N(IF({1},MODE.MULT(IF(MATCH($C$2:$C$8,C:C,0)=ROW($C$2:$C$8),ROW($C$2:$C$8)*{1,1}))))),B:B,{"B","C"})>0),
{1;1})=2))-J2
=SUMPRODUCT(
--(MMULT(
--(COUNTIFS(A:A,E2,C:C,INDEX(C:C,N(IF({1},MODE.MULT(IF(MATCH($C$2:$C$8,C:C,0)=ROW($C$2:$C$8),ROW($C$2:$C$8)*{1,1}))))),B:B,{"A","C"})>0),
{1;1})=2))-J2
=SUMPRODUCT(
--(MMULT(
--(COUNTIFS(A:A,E2,C:C,INDEX(C:C,N(IF({1},MODE.MULT(IF(MATCH($C$2:$C$8,C:C,0)=ROW($C$2:$C$8),ROW($C$2:$C$8)*{1,1}))))),B:B,{"A","B","C"})>0),
{1;1;1})=3))
Each of these would be placed in the first row of their respective columns and confirmed as array formula by using Ctrl-Shift-Enter instead of Enter when exiting edit mode.
They they would be dragged/copied down the columns.
Here, another alternative that generates the entire output for all cases with one formula. Use the following formula in cell E2:
=LET(ms, UNIQUE(A2:A8), ux, UNIQUE(A2:C8), CALC, LAMBDA(arr,cnt, BYROW(ms,
LAMBDA(m, LET(subset, CHOOSECOLS(FILTER(ux, INDEX(ux,,1)=m),2,3),
C, INDEX(subset,,2), SUM(BYROW(MMULT(IF(TOROW(C)=UNIQUE(C),1,0),
TRANSPOSE(IF(TOROW(INDEX(subset,,1))=arr,1,0))),
LAMBDA(r, N(SUM(r)=cnt)))))))),
HSTACK(ms, CALC({"A";"B";"C"}, 1), CALC({"A";"B"},2), CALC({"B";"C"},2),
CALC({"C";"A"},2), CALC({"A";"B";"C"}, 3)))
Here is the output:
We create a user LAMBDA function CALC with input argument arr (items values) and cnt (count condition to check), so we can generate all possible output changing the input parameters via HSTACK function.
CALC uses ux name, that represents the input removing duplicated row, such as the combination: {m3,C,300}. Now we cannot use RACON functions, because we need to work with an array, therefore in order to do the count for Item and Bill column values, we use MMULT function combined with IF statement as follow:
MMULT(IF(TOROW(C)=UNIQUE(C),1,0),TRANSPOSE(IF(TOROW(INDEX(subset,,1))=arr,1,0))
The output of MMULT, on each row (unique bill numbers) has the occurrences of each arr value. Therefore if we do the sum by row (inner BYROW) and check against the number of counts we are looking for (cnt), we get the expected counts we are looking for.
The rest is just to invoke CALC for all cases and append by column via HSTACK.

Index Match with Multiple Criteria while looking for a certain string

A B C
1 Name Last Name ID
2 Ben Dafflin ID1001
3 Yu Yiin ID1002
5 Max Gray ID1003
6 John Carl Flit ID1004
Situation 1 : Index Match with wildcards "*" (Working Fine!)
Formula : =INDEX($C:$C,MATCH("*John*",$A:$A,0))
Result : ID1004
Situation 2 : Index Match with multiple criteria (Working Fine!)
Formula : =IFERROR(INDEX(D:D,MATCH(1,(B:B="Flit")*(C:C="John Carl"),0)),"")
Result : ID1004
Problem:
In situation 2, if it only looks for cells with "Carl" or "*Carl*" it doesn't work. Any suggestion how will I use the situation 2 having an index match with multiple criteria but can still look for cells that contains such specific string.
To have your 2nd formula return a value, you just need to reference the correct columns: =IFERROR(INDEX(C:C,MATCH(1,(B:B="Flit")*(A:A="John Carl"),0)),"")
If you want to use wild cards with MATCH where you have multiple criteria, you can try:
=INDEX(C:C,MATCH(1,1/(ISNUMBER(SEARCH("*Carl*",A:A))*(B:B="Flit")),0))
If you have Office 365
=FILTER(C:C,ISNUMBER(SEARCH("*Carl*",A:A))*(B:B="Flit"))
Lookup when Matching to Multiple Columns
There's another way to approach this, which is simpler and may well be a better fit:
{=INDEX(C:C,MATCH("*CarlFlit",A:A&B:B,0))}
The above is of course an array formula (hence the opening { and closing })
a) The array matched is the rows of columns A and B concatenated into single strings
b) This specific example is of course looking for (*wild-carded) *CarlFlit
c) Which of course, could be any combination of values and valid MATCH wildcards
If the values being looked for are in cells (rather than hardcoded into the formula) simply refer to those cells:
{=INDEX(C:C,MATCH(E2&F2,A:A&B:B,0))}
To add checking for no-match (using first example above):
{=IF(ISNUMBER(MATCH("*CarlFlit",A:A&B:B,0)),INDEX(C:C,MATCH("*CarlFlit",A:A&B:B,0)),"Not Found")}
Additional information
The formula above can be expanded to accommodate matching across any number of columns.
o Simply & in each additional column (in whatever order suites your purpose).
For performance reasons, it's not a good idea to have lookup functions looking at entire columns.
a) Better to use (e.g.) {=INDEX($C$1:$C$1000,MATCH("*CarlFlit",$A$1:$A$1000&$B$1:$B$1000,0))}.
b) Where 1000 is of course an example last row in your lookup range.

Excel Count Ifs depending on a sum of 2 columns

I work with large data sets that have the same description for cases that need to be broken apart. The only way to consistently distinguish the cases is based on the sum of 2 other columns. I am trying to get a count where the sum matches a value I specify. The length of the columns is variable, so I need to include the entire column.
Example:
I want to count Type A where the sum of columns B & C is 3. I've tried variations of =countifs(A:A,"Type A",sum(B:C),3) with and without the sum function but am pretty sure this is the wrong approach.
Press Ctrl+Shift+Enter to enter this array formula.
=SUM(IF(A:A="Type A", IF(B:B+C:C=3,1,0),0)))

Excel Countif Multiple Criteria based on ID

I am trying to create a formula that will count an ID in one column if it means several criteria in another column. Other formulas I've seen are close to what I want but none consider ID.
My data look like this:
The formula you see in the formula box is the closest thing I could get but it sums up the number of times it gets all the criteria.
My wish list simplified is:
to get a 1 in Column G based on ClientEnrollmentID if Column C has
"Initial" OR "Annual".
same as above except "Initial" AND "Annual".
For example, 1074328692 (the first ClientEnrollmentID--H2) should be 1 for the first wish and 0 for the second wish.
1074331324 (second row--H3-H5) should be 1 because it satisfies the two wishes (it would be okay for 1 to appear in multiple rows for the ClientEnrollmentID).
My third wish is that someone can help me with this. Thanks!
Just math. Maybe this can help:
=MIN(COUNTIFS(H:H, H:H, C:C, "Initial") + COUNTIFS(H:H, H:H, C:C,"Annual"), 1)
=COUNTIFS(H:H, H:H, C:C, "Initial") * COUNTIFS(H:H, H:H, C:C, "Annual")
A COUNTIFS function can be given OR criteria in the form of an array of constants if you wrap it in a SUM function.
        
The formula in G2 is,
    
The second wish can be derived as either a 1 or 0 with an AND function.
    
If they have to be in one cell, you could concatenate them together with something like a division symbol separating them like this.
        
Of course, that renders them completely ineffective for any totaling or mathematical comparison without parsing the string.

Multiple conditions in excel 2002

I am using excel 2002 to create a spreadsheet. What I am trying to do is use the countif function but I have more than one condition. So I have 2 columns with a list of numbers and what I want to say is count the number of occurences where the number x is in one column and the number y is in the other column (in the same row).
E.g.
1 1
1 1
1 2
2 2
2 3
3 3
So if in the above I wanted to count the rows where the first column had the number 1 and the second column had the number 2, the answer should be 1.
I can't use the COUNTIF function because that only allows you to specify one condition and the COUNTIFS isn't available because I am using excel 2002.
Please Help.
This is a job for an array formula. In your case you can do:
=SUM((A1:A6=1)*(B1:B6=2))
entered as an array forumla (Ctrl-Shift-Enter)
The equality tests each return an array of boolean values, and the multiplication does an element-wise multiply (so a logical AND in this case). SUM coerces booleans to numbers when it adds up the resulting array.
EDIT:
In an answer to this similar question:
https://stackoverflow.com/questions/576569/use-2-conditions-in-excel-sumif
Jon Fournier posted a link to:
http://www.cpearson.com/excel/ArrayFormulas.aspx
which has a lot more detail on this sort of thing.
You could create a 3rd column which joins the two other column values with (say) a space or other special character, and then test for the combined value with COUNTIF.
If you don't want to add another column on your worksheet, you could put it on a different worksheet - or even create a dynamic named range.
Personally I love the SUMPRODUCT function but I'm not sure if you have it in Excel2002
Here's a very good resource for multiple conditions:
http://www.ozgrid.com/Excel/sum-if.htm
The DCOUNT function allows for multiple criteria.
The formula would look like this
=DCOUNT(A2:B8,"ColumnA",A11:B12)
if you have your criteria below the data.
Screenshot of data and formula http://img31.imageshack.us/img31/1093/dcount.png

Resources