Forum Discussion
Countif(s) problem
Hi all, I am currently trying to use the "countif" function on Excel and am running into some problems with cells that have multiple numbers.
To preface this question, the cells at the top of the page with black numbers are the inputs. The cells at the bottom of the page with red numbers are the outputs of the function.
When the inputs only have one number in the cell, the "outputs are fine, as seen in the "Q1" column at the bottom of the page (the equations being used are countif(b5:b8, 1), countif(b5:b8, 2), countif(b5:b8, 3), countif(b5:b8, 4), and countif(b5:b8, 5), respectively, from top to bottom). However, when moving to the "Q6" column, one input cell has "1,2" and it is not being accounted for in the "Q6" outputs. Is there a function that would count both the 1 and 2 individually?
6 Replies
For example:
=SUM(--ISNUMBER(SEARCH(",1,", ","&B5LB8&",")))
=SUM(--ISNUMBER(SEARCH(",2,", ","&B5LB8&",")))
etc.
- sleath01Copper Contributor
You're a hero!! This worked and you saved me from going down a VERY long rabbit hole! Thank you 😀
- PeterBartholomew1Silver Contributor
That looks more straightforward than my solutions! The 'simple' one was
= LET( textMatches, COUNTIFS(column, "*"&seq&"*"), valueMatches, COUNTIFS(column, seq), count, textMatches + valueMatches, count )To get the whole lot as one spill range (requiring nested arrays)
= EVALTHUNKARRλ(BYCOL(data, LAMBDA(column, LET( textMatches, COUNTIFS(column, "*"&seq&"*"), valueMatches, COUNTIFS(column, seq), count, textMatches + valueMatches, LAMBDA(count) ) )))COUNTIFS was a problem when it came to picking up both 1, the number, and "1" in a text string.
- djclementsSilver Contributor
Nice use of thunks, Peter. They are definitely versatile. ;)
Working with your sample file, yet another example could be:
=LET( colϑ, BYCOL(data, LAMBDA(x, LAMBDA(x))), crtϑ, BYROW(HSTACK(seq, "*" & seq & "*"), LAMBDA(x, LAMBDA(x))), MAP(IFNA(colϑ, crtϑ), IFNA(crtϑ, colϑ), LAMBDA(col,crt, SUM(COUNTIFS(col(), crt())))) )While this method doesn't involve stacking or indexing, the total number of iterations is significantly higher than yours (rows * columns instead of just columns). With a larger output array, I believe your example would indeed prove to be more efficient.
Working with the op's layout, and using Hans' concept, GROUPBY is also a possibility:
=LET( data, DROP(TRIMRANGE(B4:U24), 1), qId, SEQUENCE(, COLUMNS(data)), aId, SEQUENCE(, 5), grp, TRANSPOSE(GROUPBY(VSTACK("Answer", TOCOL(IFNA(qId, data))), VSTACK(aId, --(ISNUMBER(SEARCH(aId, TOCOL(data))))), SUM, 3, 1)), hdr, TAKE(grp, 1), VSTACK(IF(ISNUMBER(hdr), "Q" & hdr, hdr), DROP(grp, 1)) )To the best of my knowledge, TRIMRANGE is still only available on the Insider Beta Channel. For testing purposes, I've created my own TRIMRANGEλ custom function in the attached file. Fair warning, though, it is not as robust as what I expect the native function will be, so it should not be used with entire sheet column references.