Using Google Spreadsheets QUERY to Filter - google-sheets

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.

Related

Arrayformula to calculate sum for each entry in a list

I need to automatically calculate the respective score of list of entries in a list. Basically I need something in the lines of "If letters in column F exist in column A, then sum for each entry in column A and if letter not found in F then return 0". It should be an array type of formula.
I tried both query function, which didn't work the way I wrote it, and arrayformula, but it doesn't give me sum per row.
=query(b3:g,"select sum (G) when F contains B group by B")
=arrayformula(sumifs(G4:G,B4:B,F4:F))
I know you can use a combination with mmult, but I don't know how to combine that with matching the column values for the letters in the list.
The file is listed below and is structured as following.
The score list in column A is fixed, and each month data in F-G column is posted, if the score is 0 for that month then the letter doesn't show up in the F column. I need an automatic way to calculate so I get the numbers in sheet 2.
https://docs.google.com/spreadsheets/d/1eyyqNL-LKw5F4kdDnbAPzN28jc4kWKMCJ-GAkn2yoXg/edit?usp=sharing
MAP() or BYROW() will do the trick. Try below formula-
=MAP(B3:B17,LAMBDA(x,SUMIFS(G3:G,F3:F,x)))
For >300 use-
=MAP(B3:B17,LAMBDA(x,SUMIFS(G3:G,F3:F,x,G3:G,">300")))
To make your input reference dynamic can use B3:INDEX(B3:B,COUNTA(B3:B)) as array argument or MAP() function. Try-
=MAP(B3:INDEX(B3:B,COUNTA(B3:B)),LAMBDA(x,SUMIFS(G3:G,F3:F,x)))

Google Sheets: Find a Row that Matches Only a Few Specific Characteristics

I can't seem to find the right equation to find a cell from a row that matches only a few specific characteristics. In this example, I am trying to find the equation for Column D which would be the cell in A that has the same cells for B & C.
Hope this makes sense!
I'll provide two options.
If you're sure your data will only ever have zero or one match, you can place the following formula into D2 of an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,SUBSTITUTE(VLOOKUP(B2:B&C2:C,{B2:B&C2:C,A2:A},2,FALSE)&VLOOKUP(B2:B&C2:C,SORT({B2:B&C2:C,A2:A,ROW(A2:A)},3,0),2,FALSE),A2:A,"")))
However, if you think more than one match may turn up and you want "None" to be returned if there is no match, you can use the following formula in D2 or an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(REGEXEXTRACT(REGEXREPLACE(SUBSTITUTE(VLOOKUP(B2:B&C2:C,TRIM(SPLIT(FLATTEN(QUERY(QUERY({B2:B&C2:C&"~",A2:A&","}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),2,FALSE),A2:A,""),"^[,\s]+$","None"),"([^,\s].+[^,\s])[,\s]*$"),"[,\s]+",", ")))
The second formula will work even if there will only ever be zero or one match; it's just not necessary to have it be that lengthy. And the second formula is only as lengthy because it was unclear from your posted examples whether the data in Col A, B and C will really only ever be one word or not; so the formula is built to assume there will not always be one-word strings in those columns.
Either formula will provide results for the entire column without dragging.
Here's an option, You can use this formula in column D2:
=iferror(textjoin(", ",true,query($A$2:$C,"Select A where A is not null and A != '"&$A2&"' and B = '"&$B2&"' and C = '"&$C2&"'",0)),"None")
Limitation:
You need to manually drag the formula to its succeeding rows. Arrayformula() cannot be used in looping the query string values.
What it does?
Using query(), filter the data from A2:C that has the same current row last name(Column B) and food(Column C) at the same time having a different first name(Column A)
If there are multiple results, use textjoin() to combine them with ", " as its delimiter.
If there is no matched found, it will return an error, hence use iferror() to set the default value to "None"
Output

How can I use REGEXMATCH and SPLIT in a filter in Google Sheets?

I have two data sets:
Data set 1 is data from our Sales team
Data set 2 is data from our Finance team
The common element between these two sets is the Invoice ID column (col A in data set 1 and col E in data set 2). However, in data set 1, this data is stored as an array, and in data set 2, each value of the array is displayed on its own row.
GOAL
I'd like to be able to enrich data set 2 (cols F & G) with the data from data set 1, however, I'm having trouble making that work. I've tried using =FILTER(A3:A7, REGEXMATCH(TEXT(E3, "0"), TEXT(ARRAYFORMULA(SPLIT(A3:A7, ",")), "0"))), but that gives me the following error: "FILTER range must be a single row or a single column.". I understand that this happens because of the SPLIT function, but I don't know how else to go about this.
The sheet can be found here.
Any help is super appreciated.
I've added a new sheet ("Erik Help") to your sample spreadsheet. The following single formula will produce all results for F3:G ...
=ArrayFormula(IF(E3:E="",,VLOOKUP("*"&"%"&E3:E&"%"&"*",{REGEXREPLACE("%"&A3:A&"%","[,\s]","%"),B3:C},{2,3},FALSE)))
The % symbol is just used to "pad" every element from Col A and Col E in something unique in order to differentiate search term 1 from, say, 14 (i.e., the VLOOKUP will search for %1% instead of just 1, which will not be found in %14%, etc.) The % symbol has no special meaning; it could have been any unique character we were sure wouldn't normally turn up in Col A or Col E.
REGEXREPLACE replaces all commas and spaces with this special % character in addition to the front and back placements. So a Col-A list like 1, 14 would be seen by sheets at this point as %1%%14%.
The * is a wildcard symbol that, appended front and back to the search term, which will allow it to be found in elements of Col A that contain lists.
Results from the second and third columns (e.g., {2,3}) of the virtual array are returned.
Give a try on-
=FILTER($B$3:$B$7,ArrayFormula(MMULT(--(SPLIT($A$3:$A$7,", ")=E3),SEQUENCE(Columns(ArrayFormula(SPLIT($A$3:$A$7,", ")))))))

Google Spreadsheet array filtering

I have a column in google spreadsheet name domains which contains the array of values like ['non work', 'work', 'work', 'non work'], is there a way to find the count within that column which equals to work
Below is the screenshot of what I want to achieve.
In Work Domain Count column values should be respectively 3, 2 and 0
=SUMPRODUCT(REGEXMATCH(TRIM(SPLIT(REGEXREPLACE(A1,"[\[\]\']",""),",")),"^work$"))
REPLACE the non word characters like ',[ with null
SPLIT by the comma ,
Check whether each of the words MATCHes only work
SUM up the TRUEs of such MATCH.

Google sheets conditional formatting based on =QUERY result

I am trying to conditionally format a row in Google Sheets based on the result of a QUERY operation. I can get the QUERY to return the value I want (either 0 or non-zero), however QUERY insists on returning a header row.
Since the QUERY now takes up 2 rows for every row of data, changing the format of the row based off the QUERY result starts to get weird after just a few rows.
The problem I am ultimately trying to solve in the case where I enter a list of names, and I want to compare each name to a list of "NSF Names". If a name is entered, and it exists on the NSF list, I would like to flag the entry by highlighting the row red.
Help is greatly appreciated.
Edit: Formula, as requested:
=query(A:D,"select count(A) where C contains '"&E1&"' and D contains '"&E2&"'")
A:D is the data set (A is a numeric ID, B is full name, C and D are first and last names respectively).
E1 and E2 are placeholders for the person's first and last name, but would eventually be replaced with parsing the person's name, as it's inputted on the sheet (TRIM(LEFT(" ", A2) etc...)
I can get the QUERY to return the value I want (either 0 or non-zero),
however QUERY insists on returning a header row.
There might be better ways to achieve what you want to do, but in answer to this quote:
=QUERY(A:D,"select count(A) where C contains '"&E1&"' and D contains '"&E2&"' label count(A) ''")
A query seems a long-winded approach (=countifs(C1:C7,E$1,D1:D7,E$2) might be adequate) but the label might be trimmed off it by wrapping the query in:
index(...,2)

Resources