Count unique values in range if matches found in set of criteria ranges - google-sheets

Given many ranges containing text values, how would you count the unique values in one range if matches are found in a set of criteria ranges. So a formula should return 4 in the example below:
Count unique values in A3:A12 if matches found in B3:B12 or B17:B26
count_unique_range
criteria_range_1
criteria_range_2
Srijeet
Lua
Mia
Jahaan
Atakan
Caroline
Bogdan
Atakan
Caroline
Skanda
Harrison
Annika
Liam
Carter
Skanda
Liam
Liam
Carter
Francois
Francois
Chelsea
Makayla
Francois
Daniella
Malakai
Jaswant
Sowmiya
Sowmiya
Abrielle
Sowmiya
If this can be done with only COUNTUNIQUEIFS(), how would you set the criterion to match values in the count_unique_range with criteria_range1 or criteria_range2:
COUNTUNIQUEIFS(A3:A12, B3:B12, [criterion1], B17:B26, [criterion2])

Instead of using COUNTUNIQUEIFS, you could try using COUNTUNIQUE but with the values filtered according to if they match or not your ranges.
=COUNTUNIQUE(FILTER(A3:A12,IFNA(XMATCH(A3:A12,{B3:B12;B17:26}),0)))
If you have more ranges add them inside the curly brackets with semicolons

Related

How to find if string contains a specific word in Google Sheets

I have one column with comma separeted strings and a several with words that can be in these strings. I have to find if a a word from a column name is in a string. I used a regexmatch formula, but it doesn't distinguisch TV from TV remote. How can I fix this?
Mu formula:
arrayformula(if(L1<>"";if(REGEXMATCH($K$2:$K;L$1)=TRUE;"Wybrano";"");""))
My data
Or if it will be simplier I have to count how many times the certain word occured in the column with strings.
Can you try:
=MAKEARRAY(ROWS(K2:INDEX(K2:K;ROW(LOOKUP("ZZZ";K:K))));COUNTA(L1:1);LAMBDA(r;c;IF(LEN(INDEX(K2:K;r));IF(REGEXMATCH(INDEX(K2:K;r);INDEX(L1:1;;c)&"(?:,|$)");"Wybrano";);)))
Adjust the column range here COUNTA(L1:1) to something like COUNTA(L1:P1) or so depending on your dataset.
try:
=IFNA(BYCOL(L1:P1, LAMBDA(y, BYROW(K2:K, LAMBDA(x,
IF(MATCH("*"&y&"*", x, ), "Wybrano"))))))

How to force sumif() to use string matching (instead of numerical matching)?

How can I force Google Sheets sumif() formula to use string matching? I'm using a string criteria on string data and it is undesirably using numerical matching.
Example data:
1.1 1
1.1 2
1.10 4
1.11 5
Doing sumif on the above data yields the value 7, instead of 3 as the "1.10" row is being matched:
SUMIF(A1:A,"1.1",B1:B)
I can achieve the desired result using query()
query(A1:B, "select sum(B) where A matches '1.1' label sum(B) ''")
but in my complex real-world use-case I do not find it as intuitive and would prefer to use sumif() if possible.
Online example:
https://docs.google.com/spreadsheets/d/1s41B3pIWixkiAh7DFvGmcJzrr6gjmQlcO3yYrvuFYzc/edit?usp=sharing
Use "'1.1"
=SUMIF(A1:A,"'1.1",B1:B)
to force text match.
Try adding a non-numerical character to both the range array and the search:
=ArrayFormula(SUMIF(A1:A&"x","1.1x",B1:B))
You may try filter function, I also feel surprise sumif that will confuse over 1.1 vs 1.10"
=sum(FILTER(B:B,A:A=E7))
The TO_TEXT function is useful here:
=arrayformula(sumif(to_text(A1:A),"1.1",B1:B))
It needs to be put in arrayformula to add up matching values in the columns.

Excel Combine two columns in call to function?

My spreadsheet looks like this:
I have two lists of dates, I want to get all unique dates between Dates1 and Dates2 and put them in column C.
I can get unique dates in column A with UNIQUE(A2:A4), but I want all unique dates between both columnms. When I try to do =UNIQUE(A2:A4,B2:B5) I get the error:
Wrong number of arguments to UNIQUE. Expected 1 arguments, but got 2 arguments.
How can I get all unique dates between both? I've also tried concatenate, but that just gives me a huge number, not a row of dates.
Thanks!
unique formula can only work with single column. Try this formula:
=UNIQUE({A2:A4;B2:B5})
{ } will convert 2 columns into one: {A2:A4;B2:B5}
See more info here:
https://support.google.com/docs/answer/6208276?hl=en
Put all cells on the argument ie UNIQUE(A2:B5)
For seperate columns, create a dummy column next to the one you are comparing to, set its contents to the actual column, then use Unique
Use iferror to pass processing to a second version of a 'unique list' formula with different columns.
=IFERROR(INDEX(A$2:INDEX(A:A, MATCH(1E+99, A:A)), MATCH(0, COUNTIF(D$1:D1, A$2:INDEX(A:A, MATCH(1E+99, A:A))), 0)),
IFERROR(INDEX(B$2:INDEX(B:B, MATCH(1E+99, B:B)), MATCH(0, COUNTIF(D$1:D1, B$2:INDEX(B:B, MATCH(1E+99, B:B))), 0)), TEXT(,)))
Turns out (thanks to Keatinge) that UNIQUE takes an array of ranges separated by a semi-colon and wrapped in braces. Putting the range limiters from my own solution in achieves:
=unique({A$2:INDEX(A:A, MATCH(1E+99, A:A));C$2:INDEX(C:C, MATCH(1E+99, C:C))})
That sample google-sheets is here.

Using Google Spreadsheets QUERY to Filter

I have the following google sheet where:
Col a= quantities
Col b= product codes, which i´ve split between C and H.
I want to know the quantity according to different "filters"... this filters are the fields between C11 and H11, and they are optional. There are 6 possible filters.
It works using =QUERY formula located in H12 and it returns the sum of quantity values where the filters match...
BUT there´s the possibility of leaving a filter empty to get "all"...
the query is as follows:
=QUERY(A1:H7, "select sum(A) where C contains '"&C11&"' and lower(D) contains lower('"&D11&"') and E contains '"&E11&"' and lower(F) contains lower('"&F11&"') and lower(G) contains lower('"&G11&"') and lower(H) contains lower('"&H11&"') ",-1)
My problem is with the match type: where C contains '"&C11&"' and...
instead of using "contains" it should compare using "matches". this way it would count like it should, but then it won´t accept empty filters.
How can I get it to count only if the field is filled??
What´s wrong with the filter right now? It´s counting "4" matches because model matches "1" happens when model is 1 or 21, also with column D where i´m looking for value X and is also counting value XN as it contains "X". if formula is changed to "matches" instead of "contains", it won´t allow me to leave it blank.
Thank you!
Karl_S formula is great, but it does not sum the quantities in column A. Adapting his approach to SUMIFS seems to do it:
=SUMIFS(A2:A7,C2:C7, IF(ISBLANK(C11), "<>" ,C11),D2:D7, IF(ISBLANK(D11), "<>" ,D11),E2:E7, IF(ISBLANK(E11), "<>" ,E11),F2:F7, IF(ISBLANK(F11), "<>" ,F11),G2:G7, IF(ISBLANK(G11), "<>" ,G11),H2:H7, IF(ISBLANK(H11), "<>" ,H11))
Use this formula instead:
=COUNTIFS(C2:C7, IF(ISBLANK(C11), "<>" ,C11), D2:D7, IF(ISBLANK(D11), "*",D11), E2:E7, IF(ISBLANK(E11), "<>",E11), F2:F7, IF(ISBLANK(F11), "*",F11), G2:G7, IF(ISBLANK(G11), "*",G11), H2:H7, IF(ISBLANK(H11), "*",H11))
If one of the options is blank, it will match all items in that column. Otherwise it should do an exact match.

Countif with len in Google Spreadsheet

I have a column XXX like this :
XXX
A
Aruin
Avolyn
B
Batracia
Buna
...
I would like to count a cell only if the string in the cell has a length > 1.
How to do that?
I'm trying :
COUNTIF(XXX1:XXX30, LEN(...) > 1)
But what should I write instead of ... ?
Thank you in advance.
For ranges that contain strings, I have used a formula like below, which counts any value that starts with one character (the ?) followed by 0 or more characters (the *). I haven't tested on ranges that contain numbers.
=COUNTIF(range,"=?*")
To do this in one cell, without needing to create a separate column or use arrayformula{}, you can use sumproduct.
=SUMPRODUCT(LEN(XXX1:XXX30)>1)
If you have an array of True/False values then you can use -- to force them to be converted to numeric values like this:
=SUMPRODUCT(--(LEN(XXX1:XXX30)>1))
Credit to #greg who posted this in the comments - I think it is arguably the best answer and should be displayed as such. Sumproduct is a powerful function that can often to be used to get around shortcomings in countif type formulae.
Create another list using an =ARRAYFORMULA(len(XXX1:XXX30)>1) and then do a COUNTIF based on that new list: =countif(XXY1:XXY30,true()).
A simple formula that works for my needs is =ROWS(FILTER(range,LEN(range)>X))
The Google Sheets criteria syntax seems inconsistent, because the expression that works fine with FILTER() gives an erroneous zero result with COUNTIF().
Here's a demo worksheet
Another approach is to use the QUERY function.
This way you can write a simple SQL like statement to achieve this.
For example:
=QUERY(XXX1:XXX30,"SELECT COUNT(X) WHERE X MATCHES '.{1,}'")
To explain the MATCHES criteria:
It is a regex that matches every cell that contains 1 or more characters.
The . operator matches any character.
The {1,} qualifies that you only want to match cells that have at 1 or more characters in them.
Here is a link to another SO question that describes this method.

Resources