Variable named range cell references in Queries - google-sheets

The primary sheet has one column with dates; and thousands of rows on this sheet.
There are hundreds of other sheets, named by date. The format of each of these other date sheets is identical.
I need a query that can use each date on the primary sheet as reference to query a corresponding date sheet.
I would like to drag this formula down across all rows, and have the formula ref the date in the row rather than manually enter each cell.
Since I can't name the date sheets by date alone, I will need a naming formula that adds a word to the dates.
I've tried applying Named-Ranges to the date sheets and referencing them in the cells with import-range, but this did not work. I also tried inserting a link to the named ranges in each date cell, but this did not work either.
I've attached a screen grab of the idea. The data is just for illustration, it is fake. Sorry if this is a dumb question, I'm new to this, experimented, researched, could not find a solution. Thanks!

try:
=QUERY({INDIRECT("C"&ROW(C8)&"!A:H")},
"select Col5,Col8
where Col3 > date '"&TEXT(C8, "yyyy-mm-dd)&"'
and Col4 = 'SALE'
and Col5 <= "&D8 + (D8*E8)&"
and Col8 < 1
and Col8 > 0.01
order by Col8
limit 1", 0)

Related

How to ignore text with GSheets Query dateDiff function?

=QUERY(IMPORTRANGE(some_range);"
SELECT Col2,dateDiff(Col20,now())
WHERE Col20 IS NOT NULL AND dateDiff(Col20,now()) <= 30
ORDER BY Col2 ASC
LABEL Col2 'SANITARNA'
";0)
So, i have this query formula which works perfectly for a column that has only dates. However i need to apply it to a column where there are dates and some text values. The problem is when i change the dateDiff column i get an error "Unable to parse query string for Function QUERY parameter 2: Can't perform the function 'dateDiff' on values that are not a Date or a DateTime values" which makes sense. However, i cant seem to figure out how to incorporate a filter within the dateDiff function to just skip the text values and only output the ones that have dates. My best guess so far is that the filter has to be applied within the dateDiff function in SELECT and not WHERE. I've tried a filter/isnumber formula but get parse error and my brain is fried and can't see the problem.
Test sheet: https://docs.google.com/spreadsheets/d/1thpXBSp-Vt1E5MGaM89Xko6GvekjmzyidO94Sil2AjQ/edit?usp=sharing
See my newly added sheet ("Erik Help"), which contains the following version of your original formula:
=QUERY(FILTER(A:C,ISNUMBER(C:C))," SELECT Col1,dateDiff(Col3,now()) WHERE Col2 IS NOT NULL AND dateDiff(Col3,now()) <= 30 ORDER BY Col1 ASC LABEL Col1 'PRIJAVA DO' ",0)
FILTER will first filter in only those rows where the value in Col C is a number. And since all dates are numbers as far as Google Sheets is concerned, that will be only your rows with dates in Col C.
NOTE: You currently have no date values in Col C that are less than or equal to 30 days from TODAY() — that is, all of your Col-C dates are in the future — so your table is returning empty (yet without error, because it is working). Because QUERY is acting on a FILTER of the original data and not on the original data itself, all QUERY column references must be in Colx notation, not A-B-C notation.
The solution Erik Tyler provided works, ill just add here the syntax for the importrange as well for anyone with similar problem.
=QUERY(FILTER(
IMPORTRANGE("some_range";"some_sheets!A:QQ");
ISNUMBER(
IMPORTRANGE("some_range";"some_sheets!C:C")));"
SELECT Col2,dateDiff(Col20,now())
WHERE Col20 IS NOT NULL AND dateDiff(Col20,now()) <= 30
ORDER BY Col2 ASC
LABEL Col2 'SANITARNA'
";0)

Google Sheets - Count Unique Values of Importrange Data between Given Dates

I have a sheet with a Name Column and corresponding Date column. I'm attempting to count the number of times unique Names appear for a specific Date range. The data is in a separate spreadsheet, so I'm using importrange in the formulas.
I created a sample spreadsheet here - we can pretend that the "Data" tab is on a separate spreadsheet file. I have the below formula so far but I'm not sure how to tell it to only count the unique Name values between the Dates in cells B2 and C2.
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1VHR-T9bo0E1KH4yZp-9wTW6JGjKh4b3qMdm99MP_vB8/edit#gid=0","Data!A:B"),"Select Count(Col1) where Col2 >= date '"&TEXT(B2,"yyyy-mm-dd")&"' and Col2 <= date '"&TEXT(C2,"yyyy-mm-dd")&"' Label Count(Col1) ''")
Thanks for your help!
This formula should do it:
=COUNTUNIQUE(QUERY(IMPORTRANGE("1VHR-T9bo0E1KH4yZp-9wTW6JGjKh4b3qMdm99MP_vB8","Data!A2:B"),"select Col1 where Col2 >= date '"&TEXT(B2,"yyyy-mm-dd")&"' and Col2<= date '"&TEXT(C2,"yyyy-mm-dd")&"'",0))

Google Sheets - Query Table with output corresponding to specific cell criteria

I am trying to create a query based on a date range, that will display output based on the values in another column.
Here is the sample dataset I'm working with.
I would like the # Allotted (Column F) to be queried into 2 separate columns, depending on whether the Cost = 0. If the Cost = 0, I want the # Allotted to be listed under column "Free Trial" - otherwise, it should be listed under "Purchased."
I tried to create 2 separate queries for the "Purchased" and "Free Trial" columns but I can't figure out how to tell it to list the output based on a key value, such as Customer.
You can see my attempt in the sheet attached as well as what I'd like the output to look like. I highlighted the columns I'm having trouble with.
Thank you for your help!
Try:
={query(
{query({A:J},"select Col1,Col3,Col9,Col6,Col7 where Col7 =0 ",1);
query({A:J},"select Col1,Col3,Col6,Col9,Col7 where Col10 > date '"&text(A2,"yyyy-mm-dd")&"' and Col10 < date '"&text(B2,"yyyy-mm-dd")&"' and Col7>0 ",1)}
,"where Col2 is not null order by Col3,Col4 label Col1 'Customer',Col2 'Type', Col3 'Purchased', Col4 'Free Trial', Col5 'Cost'",1)}
I figured it out! Ended up using Filter instead of Query so I could filter based on the criteria selected.
I updated the solution to the sheet linked in the question.
For column "Purchased":
=iferror(FILTER(F4:F14,A4:A14=G19,J4:J14>=A$2,J4:J14<=B$2,G4:G14<>0),"")
For column "Free Trial":
=iferror(FILTER(F4:F14,A4:A14=G19,J4:J14>=A$2,J4:J14<=B$2,G4:G14=0),"")

Combine data from 2 forms to one sheet depending on the date in Google Sheets

I have 2 forms and I have their results on the same sheet. I used the following formula
=Query({importrange(...);importrange(...)}, "SELECT * where Col1 is not null")
I want to have all the results from 1 form and the results depending on the date from the other form on the same sheet.
I have tried the following formula for July
=Query({importrange(...);importrange(...)}, "WHERE month(Col1)=5")
but it shows ONLY the results with July in the date from both forms.
How can I combine the results?
EDIT 2
(following OP's concern)
...Looks like it works only when there are entries with July in the date in the second spreadsheet. I need all the entries in the first spreadsheet to be visualized at any time
We can easily fix that by wrapping the whole formula with the IFERROR function
=IFERROR({QUERY(IMPORTRANGE("xxxxxx","form1!A1:E"), "where Col1 is not null");
QUERY(IMPORTRANGE("xxxxxx","form2!A1:E"), "where Col1 is not null and month(todate(Col1))=6",0)},
QUERY(IMPORTRANGE("xxxxxx","form1!A1:E"), "where Col1 is not null"))
Just make sure that Col1 (the Timestamp) in your results sheet is formatted as Date time
This final formula says:
Import data from the 1st sheet and under it append data from the 2nd sheet based on a certain month.
If that month is missing (which results to an error), import data from just the 1st sheet.
EDIT
(following OP's clarification)
...but my results are not on the same spreadsheets. I'd like to keep them on different spreadsheets.
Since there are different spreadsheets involved you must use the IMPORTRANGE function (as you very correctly already do).
Then the formula would be:
={QUERY(IMPORTRANGE("xxxxxx","form1!A1:E"), "where Col1 is not null");
QUERY(IMPORTRANGE("xxxxxx","form2!A1:E"), "where Col1 is not null and month(todate(Col1))=6",0)}
(As before please adjust ranges to your needs. Just make sure you keep the same number of columns)
NOTES:
1. Though July is the 7th month, we use month(todate(Col1))=6. That is because in "query language" January is month 0 and not 1.
2. In our second part we use as headers 0. This way we get to avoid returning them for the second query.
3. Since the imports come from forms we keep open ranges so when new answers are submitted, then get imported as well.
If all your sheets are in the same spreadsheet, you do not need IMPORTRANGE.
Using INDEX -which is much "lighter"- your formula would be:
={QUERY({form1!A2:O29}, "where Col1 is not null");
QUERY({form2!A2:O29}, "WHERE month(Col1)=6")}
Now your form2 results will be placed under the ones from form1
Having an additional query, you can have them both ordered by the timestamp in column 1
=QUERY({QUERY({form1!A2:O29}, "where Col1 is not null");
QUERY({form2!A2:O29}, "WHERE month(Col1)=7")},"order by Col1")
(Please adjust all sheet names and ranges to your needs)

use Google Sheets QUERY to sum column weeknum = weeknum on a different sheet

I have a spreadsheet with 2 sheets in it, I want to summarize the weekly results by date. I'm trying to use the query sum function to summarize everything since I wasn't able to do it with arrayformula.
but I'm not able to do it with a query as well. I don't want to just copy-paste the sum function from each row to the next I want to just type the date I need in column A and get all the results in the different columns.
https://docs.google.com/spreadsheets/d/1ZsKXw32ycO_5KGD2I-Ug_GmqSIB_Z-D3Z1jlGd6fpTE/edit?usp=sharing
link to sheets.
getting the data from the database sheet. I want to display the data-oriented by date and summed.
=ArrayFormula(IFERROR(FILTER(QUERY(DataBase!A2:E,"select A,sum(E)
where A is not null
group by A
label sum(E)''"), WEEKNUM(DataBase!A2:A)=WEEKNUM(A2:A))
))
I tried this formula and it dosent work..
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A,
QUERY(FILTER(DataBase!A2:E,WEEKNUM(DataBase!A2:A)=WEEKNUM(A2)),
"select Col1,sum(Col5)
where Col1 is not null
group by Col1
label sum(Col5)''"), 2, 0)))

Resources