SOLVED

Trying to use UNIQUE inside COUNTIF

%3CLINGO-SUB%20id%3D%22lingo-sub-3339156%22%20slang%3D%22en-US%22%3ETrying%20to%20use%20UNIQUE%20inside%20COUNTIF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3339156%22%20slang%3D%22en-US%22%3E%3CP%3EHi!%3CBR%20%2F%3EIm%20getting%20the%20kind%20of%20error%20you%20get%20when%20there's%20a%20sintax%20error%20in%20a%20formula%20when%20I%20try%20to%20use%20UNIQUE%20inside%20COUNTIF.%3CBR%20%2F%3EWhat%20I%20have%20is%20a%20column%20(S)%20that's%20concatenating%20month%20(column%20A)%20with%20seller's%20name%20(column%20M).%3C%2FP%3E%3CP%3EFor%20example%3A%3C%2FP%3E%3CP%3EJanJon%20Doe%3CBR%20%2F%3EJanJon%20Doe%3CBR%20%2F%3EJanJane%20Doe%3CBR%20%2F%3EFebSomeone%20Else%3CBR%20%2F%3EI%20want%20to%20count%20unique%20values%20in%20column%20S%2C%20with%20an%20specific%20criteria%20(starts%20with%20Jan%2C%20for%20example).%3CBR%20%2F%3EExcel%20seems%20not%20to%20be%20liking%20what%20I'm%20trying%20to%20do.%3CBR%20%2F%3EIf%20I%20do%20COUNTA(UNIQUE(...))%20it%20works.%20Thing%20is%20that%20this%20gives%20me%20the%20grand%20total%20and%20I%20need%20to%20add%20the%20starts%20with...%20criteria%20so%20I%20can%20separate%20by%20month.%3C%2FP%3E%3CP%3EAlso%2C%20if%20I%20do%20UNIQUE(...)%20on%20one%20cell%20(which%20will%20spill%20all%20the%20results)%20and%20then%2C%20on%20another%20cell%20COUNTIF%20using%20the%20cell%20from%20UNIQUE(...)%20and%20it's%20spilled%20values%20as%20a%20range%2C%20it%20also%20works.%20I%20don't%20understand%20why%20I%20can't%20skip%20this%20step%20and%20do%20it%20directly%20with%20UNIQUE%20inside%20COUNTIF.%20Isn't%20the%20return%20value%20fron%20unique%20a%20valid%20range%3F%3CBR%20%2F%3E%3CBR%20%2F%3EI%20hope%20I%20was%20able%20to%20explain%20this%20clearly%20enough.%20Thanks%20in%20advance!%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3339156%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3339391%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20use%20UNIQUE%20inside%20COUNTIF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3339391%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1384026%22%20target%3D%22_blank%22%3E%40AndresVaz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUNIQUE%20returns%20an%20array.%20The%20first%20argument%20of%20COUNTIF%20%3CEM%3Emust%3C%2FEM%3E%20be%20a%20range%2C%20not%20an%20array.%3C%2FP%3E%0A%3CP%3ESee%20%3CA%20href%3D%22https%3A%2F%2Fexceljet.net%2Fformula%2Fcount-unique-values-with-criteria%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ECount%20unique%20values%20with%20criteria%3C%2FA%3E%20for%20a%20way%20to%20do%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3339567%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20use%20UNIQUE%20inside%20COUNTIF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3339567%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1384026%22%20target%3D%22_blank%22%3E%40AndresVaz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3Bpoints%20out%2C%20once%20you%20have%20an%20array%20rather%20than%20a%20range%20reference%2C%20the%20limitation%20of%20the%26nbsp%3B%20xxx%3CSTRONG%3EIFS%3C%2FSTRONG%3E%20formulas%20renders%20them%20unusable.%26nbsp%3B%20Usually%20the%20SUM%20and%20IF%20can%20be%20used%20as%20separate%20functions%2C%20though%20the%20calculation%20is%20less%20efficient.%26nbsp%3B%20With%20365%2C%20one%20can%20simply%20develop%20a%20formula%20step%20by%20step%20testing%20at%20each%20stage.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20%20%20%20distinct%2C%20UNIQUE(data)%2C%0A%20%20%20%20%20matches%3F%2C%20LEFT(distinct%2C3)%3D%22Jan%22%2C%0A%20%20%20%20%20filtered%2C%20FILTER(distinct%2Cmatches%3F)%2C%0A%20%20%20%20%20COUNTA(filtered)%0A%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThen%20again%2C%20why%20the%20concatenation%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20%20%20%20filtered%2C%20FILTER(Array%2C%20month%3D%22Jan%22)%2C%0A%20%20%20%20%20selected%2C%20UNIQUE(filtered)%2C%0A%20%20%20%20%20ROWS(selected)%0A%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EA%20step%20further%20and%20you%20can%20have%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20CountUniqueIf%CE%BB(Array%2C%20month%2C%20%22Jan%22)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3339695%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20use%20UNIQUE%20inside%20COUNTIF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3339695%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20in%20case%20there%20is%20anything%20of%20interest%2C%20here%20is%20the%20file%20showing%20differing%20levels%20of%20complexity.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3340911%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20use%20UNIQUE%20inside%20COUNTIF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3340911%22%20slang%3D%22en-US%22%3E%3CP%3EHans%2C%20thank%20you%20so%20much%20for%20the%20prompt%20response.%20This%20definitely%20solves%20my%20issue.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3340912%22%20slang%3D%22en-US%22%3ERe%3A%20Trying%20to%20use%20UNIQUE%20inside%20COUNTIF%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3340912%22%20slang%3D%22en-US%22%3E%3CP%3EPeter%2C%20thank%20you%20for%20your%20answers.%20I'm%20not%20quite%20there%20yet%20with%20excel's%20formula%20development%2C%20but%20I%20will%20try%20this%20in%20the%20future.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi!
Im getting the kind of error you get when there's a sintax error in a formula when I try to use UNIQUE inside COUNTIF.
What I have is a column (S) that's concatenating month (column A) with seller's name (column M).

For example:

JanJon Doe
JanJon Doe
JanJane Doe
FebSomeone Else
I want to count unique values in column S, with an specific criteria (starts with Jan, for example).
Excel seems not to be liking what I'm trying to do.
If I do COUNTA(UNIQUE(...)) it works. Thing is that this gives me the grand total and I need to add the starts with... criteria so I can separate by month.

Also, if I do UNIQUE(...) on one cell (which will spill all the results) and then, on another cell COUNTIF using the cell from UNIQUE(...) and it's spilled values as a range, it also works. I don't understand why I can't skip this step and do it directly with UNIQUE inside COUNTIF. Isn't the return value fron unique a valid range?

I hope I was able to explain this clearly enough. Thanks in advance!

5 Replies
best response confirmed by AndresVaz (New Contributor)
Solution

@AndresVaz 

UNIQUE returns an array. The first argument of COUNTIF must be a range, not an array.

See Count unique values with criteria for a way to do this.

@AndresVaz 

As @Hans Vogelaar points out, once you have an array rather than a range reference, the limitation of the  xxxIFS formulas renders them unusable.  Usually the SUM and IF can be used as separate functions, though the calculation is less efficient.  With 365, one can simply develop a formula step by step testing at each stage.

= LET(
     distinct, UNIQUE(data),
     matches?, LEFT(distinct,3)="Jan",
     filtered, FILTER(distinct,matches?),
     COUNTA(filtered)
  )

Then again, why the concatenation?

= LET(
     filtered, FILTER(Array, month="Jan"),
     selected, UNIQUE(filtered),
     ROWS(selected)
  )

A step further and you can have

= CountUniqueIfλ(Array, month, "Jan")

Just in case there is anything of interest, here is the file showing differing levels of complexity.

Hans, thank you so much for the prompt response. This definitely solves my issue.

Peter, thank you for your answers. I'm not quite there yet with excel's formula development, but I will try this in the future.