Dynamic query formula nested inside an array - google-sheets

I have a query formula in Google sheets that updates based on additional columns of data in my Google Sheet seen here =QUERY('Deals List - URL Split'!A:DZ, "select A, C where C contains 'http'",)
So it may add QUERY('Deals List - URL Split'!A:DZ, "select A, E where E contains 'http'",)and then it will end up becoming the below and so on for each additional.
=QUERY('Deals List - URL Split'!A:DZ, "select A, C where C contains 'http'",);QUERY('Deals List - URL Split'!A:DZ, "select A, E where E contains 'http'",)
What I am trying to do is have the resultant query formula which is in cell 'List'!A1 as QUERY('Deals List - URL Split'!A:DZ, "select A, C where C contains 'http'",);QUERY('Deals List - URL Split'!A:DZ, "select A, E where E contains 'http'",) be used in an array formula as a reference so I don't have to update the formula each time a new query formula is added.
The static query formula is
=SORT(ARRAYFORMULA({QUERY('Deals List - URL Split'!A:DZ, "select A, C where C contains 'http'",);QUERY('Deals List - URL Split'!A:DZ, "select A, E where E contains 'http'",)}),1,TRUE,2,TRUE)
and indeally the one that gets the dynamic formula would be like below but I always get an error and get just the literal static formula above.
=SORT(ARRAYFORMULA({'List'!A1}),1,TRUE,2,TRUE)

I think I have an answer (or two) for you. After looking at your sheet, I have to say that I am sure that a simpler design is possible for your sheets, that would simplify everything. Anyway, I've built one formula, using only your data on sheet '2 URL SPLIT'!, and the desired columns from '4 URL FILTER'!A1:1. See my sample tab, GK-6 ITEMS AND URLS, added to your sheet.
The formula, reduced to its basic form, is:
={
IFERROR({'2 URL SPLIT'!$A$2:$A, INDIRECT(INDEX(
{ARRAYFORMULA(IFERROR("'2 URL SPLIT'!"
& TRANSPOSE('4 URL FILTER'!1:1)
& TRANSPOSE(SPLIT(
{"2:"
& TEXTJOIN("~2:",1,TRANSPOSE('4 URL FILTER'!1:1))},"~",0,0))
& ROWS('2 URL SPLIT'!A:A)))},1,0))},{"",""})
}
The formula is not truly dynamic, but it ignores blank columns. So the cheat I've used is to expand the capacity of the formula to include extra blank columns, and if they get filled with data, the data will be used. I've set it to include 50 columns of data, where you are currently using 39, but you could expand it to handle about 200 columns, before it reaches the 50,000 character limit of a cell.
The formula as shown above handles one column. For the one that handles fifty columns, as in my sample sheet, I simply duplicate the inner formula, everything inside the outer braces "{....}" and increment the number in it. You only need to do this once, or copy mine from my sheet. You do not need to update if/when your data columns expand.
I'm happy to add much more explanation if you decide that this formula works for you. But the basis of the formula is dynamically building the ranges of cells to query. The result of this inner part of the formula is shown below. Note that the 2 in each range is hard-coded, and can be changed if your structure changes, but the limit of the range is calculated from your data.
The rest of the formula uses an index into this "table", incrementing by one to select each successive data range, which adds a new column of data to be queried. These data ranges from '2 URL SPLIT!' include column A and one subsequent data column, as specified in '4 URL FILTER'!A1:A, and are stacked one above the other, by using a ";" separator.
The query is then run against this vertical, two column stack, selecting all rows where column 2 contains "http".
The final result is shown below:

Related

How to 'count' only when header matches value?

I have a Google Form that collects a bunch of data from dropdown questions on a Sheet with each question going to one column (as normal). On separate sheets, I want to be able to count how many times each option is selected.
Here is an example of what the response sheet might look like. A, B, and C are all questions.
I would then have separate sheets for 'Person?', 'Place?', and 'Thing?'. The 'Person?' sheet would look something like this:
I want to be able to add in the count of each time the option appears for that question. In the example, notice that 'Napoleon" is in both Col A and Col C. If I just count the number of times 'Napoleon' appears, I will get '2' even though he only appears once in the "Person?" responses.
I originally used a QUERY function like =QUERY('Input Data'!1:1000, "select count(A) where A contains '"&$A2&"'",0). BUT, I need it to be dynamic. So the "Person?" question may not always be Col A. I want the Query (or whatever formula) to search the headers and only return the count of that option for that question even if the column location changes.
Okay, I figured it out! In case someone else is curious, I used this formula:
=QUERY({'Input Data'!A1:L}, "SELECT COUNT(Col"&MATCH("Person?", 'Input Data'!1:1,0)&") WHERE Col"&MATCH("Person?", 'Input Data'!1:1,0)&" CONTAINS '"&$A2&"' label COUNT(Col"&MATCH("Person?", 'Input Data'!1:1,0)&") ''",0)
Lee, I sent you a PM about your most recent post, but in the process, I came across this one. There is no need for multiple formulas or manual entry references. One formula can produce the entire report with headers, listing and counts:
=IFERROR(QUERY(FILTER(FILTER(A:L,A:A<>""),A1:L1="Person?"),"Select Col1, COUNT(Col1) GROUP BY Col1 ORDER BY Col1 LABEL COUNT(Col1) 'Count'",1),"No Matches")
Just fill in the header your looking for between the quotes where Person? is now.
The double FILTERs mean "Start with only rows where Col A is not null and Row 1 reads 'Person?'"
Then QUERY simply returns the unique names in the left column and their counts in the right column. Because the QUERY had a final parameter of 1, any existing header will be kept (in this case, the one you were searching for); and the created column will receive a header (i.e., LABEL) of Count.
IFERROR will give a friendly error message if no matches are found (in which case check that what you entered for the search in the formula exactly matches a column header in the range).

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,", ")))))))

Techniques to accommodate new entries in google sheets

As you can see I transpose codes into unique column headings so that debits and credits are analysed and summated. Summations are transposed in another sheet to create summary profit/loss account. I need help how to replicate the sum formula in column I to serve any expanded transposed unique codes and whether/how I should use arrayformula for the individual cell output.
EDIT
Actual output looks like this:
My problem is to how to automatically accommodate new entries/codes in the totals row and main body of cells. The data belongs to a residents' committee so I can only show anonymous data as image.
EDIT 2
Actual input is imported from bank records, then coded:
Query is pretty good for the SUM part.
Starting in column I, you can do:
=ArrayFormula(INDEX(QUERY(
0+OFFSET(I4,0,0,ROWS(F6:F),COUNTA(UNIQUE(F4:F))),
"select "&
JOIN(
",",
"sum(Col"&SEQUENCE(COUNTA(UNIQUE(F4:F)))&")"
)
),2))
The 0+ or the VALUE in the second one (they both do the same thing here) transforms the data cells to default to 0 if blank, otherwise the query fails. This also lets us refer to the columns by sequence number, which is what we do in the second argument. We build the query into something that looks like select sum(Col1),sum(Col2),...,sum(ColN). Since this gives us a header by default, we could relabel everything in the query statement, but that gives too much extra code, so the easier thing to do is use INDEX to select the sums.
The EQ part is fairly straightforward to Arrayify. Starting in I4:
=ArrayFormula(
(FILTER(F4:F,F4:F<>"")=FILTER(I2:2,I2:2<>""))*
IF(
Array_constrain(G4:G,COUNTA(FILTER(F4:F,F4:F<>"")),1),
G4:G,
-H4:H
)
)
The FILTERs just filter out the blank cells, and the Array_Constrain sizes the G column to the same size as the filtered F column.

partial google sheets string cell comparison

The Primary Sheet has basic info ( narrowed here ), including
Column B - a flag to say "Yes" or "No/Stop"
Column C - a Company Name
A Second sheet "stopphrases" will have a growing list of words and phrases in it. If one of these words match first sheet called "data" > Column C (Company Name), Column B will updated to a "No"
IE -
1) If stopphrases!sheet, range A1:A25 contains "UPS"
2) And the Primary Sheet has "UPS Shipping"
This "partial" match should catch it and insert flag into data.Column B
=MATCH(""&stopphrases!A1:A10&"", C6,0)
I seem to be able to check a partial from a single cell and check the range, but not got the other way - get the partial words from multiple cells and check against a single cell.
I also tried
(If Statement)
=IF(COUNTA(IFERROR(FILTER(stopphrases!$A$1:$A$25,stopphrases!$A$1:$A$25=C2))),"Yes","No")
The results I am looking for are
So again -
IF StopPhrases!A1:A25 Range has a phrase "UPS"
THen any entry in first sheet, Company Name, Column B that CONTAINS "UPS" - IE "UPS Shipping" should flag - this way I can run a condition.
The end result is if a match occurs a "stop" happens and that record doesn't get processed in other spreadsheets that are connected.
You can use regexmatch or regexextract to do this:
first to make the dynamic list of stopphrases you use a JOIN function, an INDIRECT function so you can use COUNTA to allow for it to automatically add the correct number of phrases,
=JOIN("|",INDIRECT("E2:E"&COUNTA(E2:E)+1))
Then you use arrayformula and regexmatch to dynamically match your list of stop words:
=ARRAYFORMULA(IF(ISTEXT(B1:B),if(REGEXMATCH(B1:B,$D$1),"STOP","GO"),))
=ARRAYFORMULA(IF(ISTEXT(B1:B),if(REGEXMATCH(LOWER(B1:B),$D$1),"STOP","GO"),))

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