Is there a way to create a dynamic Query on Google Sheets? - google-sheets

I'm creating a database on Google Sheets for work and while I'm aware it's not the best solution, due to a number of restraints, that's what I've got to work with atm.
The thing is, I'm creating this for people who don't really know how to work with formulas on Google Sheets so I'm trying to be as user friendly as I can.
I'll use this sample sheet to try and demonstrate what I'm trying to do:
https://docs.google.com/spreadsheets/d/1tXM0IlswQVdwFum9a0pR5NsbtL5Uq_AH1tPbytXGvVg/edit#gid=0
One of the sheets is the Database, which will countain multiple columns of data (there's 10 on the sample, but the actual database is much larger).
There is another sheet called Column Index where I have a list of all columns, what kind of data they represent and a checkbox for people to select what data they want.
Finally, theres a third sheet called Data Extraction and I want to get the checked columns from the Database and send them to this sheet so people can download or copy this data to other worksheet.
It looks like a QUERY situation to me, but I'm not sure if it's possible to do it dynamically

This should do it:
=ArrayFormula(QUERY(
Database!A:K,
"select A,"&
TEXTJOIN(
",",
1,
IFNA(
REGEXEXTRACT(
ADDRESS(
1,
MATCH(
IF(
'Column Index'!C2:C,
'Column Index'!B2:B,
""
),
Database!1:1,
0
)
),
"[A-Z]+"
)
)
)
))
I built the query string, selecting the column indices that we wanted. No need for the "Col Index" Column.
Using Address, we can generate the appropriate column letter using a column address, then we just extract the letters part with a regex.

What I got was not that different as I also tried to construct the query string. The downside is it may be a bit buggy (but it works)
Here it is:
=query(Database!A1:K15,"select A,"&join(",",ArrayFormula(SUBSTITUTE(ADDRESS(1,filter(row('Column Index'!C2:C11),'Column Index'!C2:C11),4), "1", ""))))

Related

Trying to count rows matching multiple criteria

Using Zapier, when someone fills out my Wufoo application form, a line is created with some of the information, including Date Created as well as the locations selected from a checkbox field. On my second sheet, I want to be able to have a total of how many applications have come in just today for each location. I figured the easiest way to do this would be a CountIFs function, but I'm probably wrong.
https://docs.google.com/spreadsheets/d/1mIw_O6KT2QCyKeKmZ4aJ1EHzOkZ4xj49ZyjEMOCwUxA/edit#gid=0
Here is a copy of what I'm building if you'd like to experiment. I'm using =countifs(Sheet1!E2:T1000,"Buford,GA (Atlanta)",Sheet1!E2:T1000,"="&TODAY()) to try to find this, but always get 0 as the result.
try this:
You needed to specify the columns, in your case: E2:E to look up for the name, and S2:S for the date.
=countifs(Sheet1!E2:E,"Buford, GA (Atlanta)",Sheet1!S2:S,"="&TODAY())
You can also use Partial Match in order to use your headers in Sheet2 as the first criteria in your countifs:
=countifs(Sheet1!E2:E,"*"&B$1&"*",Sheet1!S2:S,"="&TODAY())
Then you had a problem of formatting in Sheet1 within the Date's Column (S): your second date in cell S3 was formatted as Date Time, as oppose to your first date in S2 which was formatted as Date only.
Note: The above suggestions are to resolve your personal attempt, otherwise they are other ways of achieving what you're looking for.
UDPATE (ALTERNATIVE SOLUTION):
you could use the following QUERY which summarize everything within a table and sort it in descending order:
= {"Names","Count";
ARRAYFORMULA(
SORT(
TRANSPOSE(
SUBSTITUTE(
QUERY(Applications!D1:T,
"Select Count(D),Count(E), Count(F), Count(G), Count(H), Count(I), Count(J),
Count(K), Count(L), Count(M), Count(N), Count(O), Count(P), Count(Q), Count(R), Count(S) where T = date '"&TEXT(today(),"yyyy-mm-dd")&"'
"),
"count", "")
),
2, 0)
)
}

Query to select a large range of columns without manually entering each column Google Sheets

I am using the solution presented in this post:
https://webapps.stackexchange.com/questions/101926/google-sheets-query-to-select-a-large-range-of-columns-without-manually-entering
But now I need to query data from a different spreadsheet. I know the importrange requires the use of 'Col' to specify the column. However, I don't know how to change this string:
=arrayformula(join(",", substitute(address(1, column(C:F), 4), "1", "")))
you can use:
=ARRAYFORMULA(JOIN(",", "Col"&COLUMN(C:F)))

vlookup(importrange) function doesn't find data from query result table in another spreadsheet

I've two teams. Each of those team has its own spreadsheet file they work with. I've created a new spreadsheet document that uses query(importrange) function to create a single table of all results from each team's spreadsheets.
The idea is to then use the vlookup(importrange) function in yet ANOTHER spreadsheet document to deliver specific query results. i.e. by vlookup'ing email address from the query database, import other relevant information to that email address.
I've been using this logic for a few months now and everything seemed to work fine. The only difference is that my past experience was with query result table that contained only 1 column of data.
Now, when the query result table contains 7-9 columns, this no longer seems to work.
I've concluded, that only the first column is somehow visible for the vlookup(importrange) function, while other columns seem to be invisible.
The Query result table formula:
=QUERY({IMPORTRANGE("Team A","In Progress!A:M");IMPORTRANGE("Team B","In Progress!A:M")},"where Col1 is not null",1)
The formula on another spreadsheet that should act as a verifier/checker:
=IFerror(arrayformula(VLOOKUP(D3,IMPORTRANGE("the query result's sheet","In Progress!A:G"),{1,4,6,7},0)),"-")
I really hope the problem is described clearly. I expect vlookup(importrange) to import data from query result table in another spreadsheet document.
=ARRAYFORMULA(IF(LEN(A3:A), IFERROR(VLOOKUP(A3:A,
QUERY(IMPORTRANGE("15xOcSh39zZgn1829DOYF5hB6WeuaTO81pyi6Ek3R-Gw", "Sheet1!A:H"),
"select Col2,Col1,Col3,Col4,Col5,Col6,Col7", 0), {2, 5, 6, 7}, 0), "-"), ))

Filter order data to rows

I need to make sheet that will contain each order code, address, quantity and dates in one row for each order code.
I have sheet "Data" with orders (Address ID, Product code, quantity and Order ID) and i need to put it in other sheet "Export" in specific order. Is there a way to do it with functions or script?
I made test spreadsheet so everyone can see preview of what i need: test sheet
For example i have Address ID A001, i need to get each code and quantity from this address ID and put in row, in test spreadsheet i made example.
Sorry if I am asking too much, but i need some help or advice :)
Made picture so it would be easier to understand:
picture
The operation you need is called "unpivot" and has a couple of possible solutions, some of them purely functional and some utilizing Google Apps Script. Search for "unpivot" to explore them. Here goes one of possible functional solutions. It is simplified compared to your precise needs because I am not sure which of your headers ("dimensions") are unique. I'm assuming here that only the product code and address id are unique, because it looks like this in the data.
Make a new worksheet and put the formula below in A1:
=arrayformula(
split(
transpose(
split(
concatenate(
arrayformula(Data!$A$5:$A & "|" &Data!$C$3:$G$3 & "\"
)
);
"\")
);
"|")
)
Then put another formula in C1:
=arrayformula(
vlookup(A1:A; Data!$A$5:$G; match(B1:B; Data!$C$3:$G$3; 0)+2; 0)
)
This will give you the rudimentary idea of how to construct first the list of all possible combinations of dimension values (Code, Address ID in this case) and then fill in possible dependent values using VLOOKUP. Then you can use more VLOOKUPS to add more columns as needed.

Google Sheets formula that returns all values for which multiple criteria in other columns apply

My problem, generally stated:
I need a formula that returns all the values in a specific column for which multiple criteria in other columns of the respective row apply.
My problem, specifically stated:
I would like a formula that returns all the values in Column A for which Column C is "John", Column E is "Apples", Column G is "Earth" and both Columns H and I are empty. See here for a simplified illustration of my problem with dummy data. The correct formula, dragged down, would output a list with the values "1", "4", and "14". If you'd like to try out some stuff in the linked spreadsheet, feel free to do so in a copy of the original sheet so others can see my original data/formulas.
What I've tried so far:
Simply filtering was not an option because the data is on a separate sheet within the same spreadsheet. I also knew VLOOKUP and INDEX/MATCH were not going to do what I wanted - VLOOKUP doesn't handle multiple criteria, and while the MATCH part of INDEX/MATCH can be turned into an array to specify multiple criteria, it only returns the first value for which all conditions are true, while I need all of them.
I then tried the following formula (Formula 1 in the linked spreadsheet):
=IFERROR(INDEX($A$2:$I, SMALL(IF(COUNTIF($K$2, $C$2:$C)*COUNTIF($K$3, $E$2:$E)*COUNTIF($K$4, $G$2:$G), ROW($A$2:$I)-MIN(ROW($A$2:$I))+1), ROW(A1)), COLUMN(A1)),"")
It worked like a charm, until I wanted to include the condition that both columns H and I should be empty. I tried this, but for some reason I don't understand it didn't work (Formula 2 in the linked spreadsheet):
=IFERROR(INDEX($A$2:$I, SMALL(IF(COUNTIF($K$2, $C$2:$C)*COUNTIF($K$3, $E$2:$E)*COUNTIF($K$4, $G$2:$G)*COUNTIF($K$5, $H$2:$H)*COUNTIF($K$6, $I$2:$I), ROW($A$2:$I)-MIN(ROW($A$2:$I))+1), ROW(A1)), COLUMN(A1)),"")
Then I tried to nest my first formula into an IF/VLOOKUP (Formula 3 in the linked spreadsheet):
=IFERROR(IF(VLOOKUP(INDEX($A$2:$I, SMALL(IF(COUNTIF($K$2, $C$2:$C)*COUNTIF($K$3, $E$2:$E)*COUNTIF($K$4, $G$2:$G), ROW($A$2:$I)-MIN(ROW($A$2:$I))+1), ROW(A1)), COLUMN(A1)),$A$2:I,8,FALSE)<>"","",INDEX($A$2:$I, SMALL(IF(COUNTIF($K$2, $C$2:$C)*COUNTIF($K$3, $E$2:$E)*COUNTIF($K$4, $G$2:$G), ROW($A$2:$I)-MIN(ROW($A$2:$I))+1), ROW(A1)), COLUMN(A1))),"")
This worked if I only asked for column H to be empty, but a) it is very unwieldy, b) it gives you blanks in the list it returns, which I do not want, and c) I could not get it to work for the condition that both columns H and I need to be empty using OR.
That's where I'm stuck, and I haven't come up with a good solution. Knowing this forum, I'm sure someone has an elegant solution I was not smart enough to find :)
I'm on phone so formating will suffer.
Create a new column on the left and insert the following into cell A2
=if(D2="John", if(F2="Apples", if(H2="Earth", if(I2="", if(J2="", B2,""), "" ), "" ), "" ), "")

Resources