How to use generalized projection in a Google docs query? - google-sheets

I wish to use a generalized projection as follows in a Google query with no success
=query('Form Responses 2'!A:N,"Select if(month(F)>6,year(F),year(F)-1) , N,B,C,D,E,F,G,H,I,J,K where F >= date '2011-07-01' and F <= date '2050-06-30' order by F desc, B")
Wishing to list the Year in column F in a required way.
Will be grateful if you can help me correct this.

You need to put the existing columns and your new column into an array
=arrayformula(query({A:N,if(isdate(F:F),if(month(F:F)>6,year(F:F),year(F:F)-1),F:F) },"Select Col15,Col14,Col2,Col3,Col4,Col5,Col7,Col8,Col9,Col10,Col11 where Col6 >= date '2011-07-01' and Col6 <= date '2050-06-30' order by Col6 desc, Col2",1))
I also had to use isdate() because if you have headers in your table the month() and year() functions will give an error.

Related

Query/Pivot Function: Header Title Descending Order & Month Format mmmm yyyy

[Goal]
I want to create a table with the Query Function where it counts the number of 'Drivers' for each month in a dynamic manner. Meaning that when the data (example sheet is called 'Data') is updated, it'll be updated automatically as well.
[What I was able to do so far]
I was able to create a table with the Query Function, however, it only displays 1 column worth of Months when I want to show up to 4 months. And I also want to show the recent months from the left and the older months on the right.
[Formula that I have so far]
=QUERY(Data!$A:$B,"
SELECT B,
Count(B)
Where B != '' AND MONTH(A)=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
Group By B
Pivot A
Order By B asc
Label B 'Drivers', Count(B) '"&TEXT(A2,"MMMM YYYY")&"'",1)
[Issue that I'm facing]
I've tried specifying the date range like the below, however there are 2 problems.
The date format is not mmmm yyyy (Example: May 2022) and it'd show as: 2022-2-1 May 2022
The months are ordered in an ascending manner (Example: 2022-2-1, 2022-3-1, 2022-4-1) instead of descending (Example: 2022-4-1, 2022-3-1, 2022-2-1)
So I'm not sure what I need to do to fix this. Hopefully I can have support.
Where B != '' AND MONTH(A)<=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
AND MONTH(A)>=MONTH(DATE'"&TEXT(EDATE(A2,-3),"YYYY-MM-DD")&"')
[Sample Sheet]
https://docs.google.com/spreadsheets/d/1AJYTRga9-dXbj64nl4RfpDKs5JYSFwP2SiR7v7gAhMI/edit#gid=1297239620
=ARRAYFORMULA(REGEXREPLACE(""&TRANSPOSE(QUERY(TRANSPOSE(
QUERY({Data!A:B, TEXT(Data!A:A, "yyyymmdd×MMMM yyyy")},
"select Col2,count(Col2)
where Col2 != ''
AND MONTH(Col1)<=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
AND MONTH(Col1)>=MONTH(DATE'"&TEXT(EDATE(A2,-3),"YYYY-MM-DD")&"')
group by Col2
pivot Col3", 1)),"order by Col1 desc")),"^(.*×)", ))
Reference:
How do I change the date format in a Google Sheets query pivot table with date filters?
Sort Query Pivot Table - Google Sheets

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: select top N cases within groups using QUERY

I want to make a report: "select salesmen who have N recent Status codes = 2,3,4,5".
A structural example of my data has 35 rows (including 1 header row):
link.
This file has a Date, Sales code (id of a salesman), Status code (id of how successful a transaction was) and other fields which are not necessary for the purpose.
I ended up using three formulas:
a QUERY function with IMPORTRANGE.In the example data it is slightly simpler - take only Sales Codes, Status Code, and Date from another sheet, then order by Date, Sales Code. Formula in A9:
=QUERY({Source!D1:E, Source!A1:A}, $B$4, 1)
an additional column with a sequentional numbering. Formula in D10:
=ArrayFormula(if(len(A10:A), ROW(A10:A) - MATCH(A10:A,A10:A,0) - 8, ))
a QUERY function to extract only N cases (let's say 5). Formula in F9:
=QUERY(A9:D, "select A, B where D <="&B3, 1)
Is there a way to combine all 3 steps into one so I get the output like in F10:G24 using one (and hopefully fast :)) formula? The formula I tried (expanded for readability):
=QUERY(
{
QUERY({Source!D1:E, Source!A1:A}, $B$4, 1),
ArrayFormula(
IF(len(J10:J),
ROW(J10:J) - MATCH(J10:J, J10:J, 0) - 8,
)
)
},
"select Col1, Col2 where Col4 <="&B3,
1
)
It gives me the error:
"Function ARRAY_ROW parameter 2 has mismatched row size. Expected: 28. Actual: 991."
I also tried finite data ranges in ROW() and MATCH() but that yields an empty table.
In the original database there are ~3500 rows and they will expand, so I think I should stick to infinite ranges to automate data extraction.
Hm well it's a bit of a nightmare TBH - a similar question has cropped up before but no easy answer. Here is a draft which involves repeating the basic query several times-
=ArrayFormula(query({query(A2:E,"select * where E>=2 and E<=5 order by D, A desc"),
row(indirect("2:"&count(filter(E2:E,E2:E>=2,E2:E<=5))+1))-
match(query(A2:E,"select D where E>=2 and E<=5 order by D, A desc"),
query(A2:E,"select D where E>=2 and E<=5 order by D, A desc"),0)
},"select * where Col6<=5"))
But needs looking at more to see if it can be simplified.
This is the full-column version including headers - I think it's OK
=ArrayFormula(query({query(A:E,"select * where E>=2 and E<=5 order by D, A desc"),
row(indirect("1:"&count(filter(E:E,E:E>=2,E:E<=5))+1))+1-
match(query(A:E,"select D where E>=2 and E<=5 order by D, A desc"),
query(A:E,"select D where E>=2 and E<=5 order by D, A desc"),0)
},"select * where Col6<=5"))

How to query spreadsheet column data, matching and summarising values?

I want to get the total of entries in a column for a particular value. The values are a number such as 0.5 or 1 and then a code such a H, S, O or WFH. i.e "0.5 H" or "1 S"
It is a absence spreadsheet recording holiday, sickness, appointments and working from home. One sheet has a row for every day in the year, the columns represent all the staff members. I wish to be able to query the values under the columns and then summarise that per staff member / per month.
I have googled but not found anything similar enough to put me in the right direction.
Any help would be greatly appreciated.
query function might help you:
about
Query Language Reference
Please, provide data sample to get more help.
your query will look like this:
=query({A:D}, "select Col1, Col2, sum(Col4) where Col3 = '1 S' group by Col1, Col2")
In this sample query formula summarises data in A:D per columns A, B (Col1, Col2) and checks the value in column C (Col3) eqwuals '1 S'.

Consolidate two ranges in date wise order in google spreadsheet

I am knew to google spreadsheet and trying to unite/consolidate two ranges in order, I am struggling hard with this & have written formula on spreadsheet but that is wrong.
Two Ranges are:
Range-1 = D1:F12
D1:D12 E1:E12 F1:F12
Word Date Name
Range-2 = G1:I12
G1:G12 H1:H12 I1:I12
Word Date Name
Now I want to unite/consolidate Range-1 & Range 2 where words should appear in column A, Date should appear in column B & Name should appear in column C & criteria is that date of new range should be in ascending order.
You may please see below mentioned link:
https://docs.google.com/spreadsheets/d/1tiKUdREYudh1htLIfh9VSWO27N5zCXwJAVKr0gqaSXE/edit#gid=0
Please help, thanks in advance.
try:
=query({D1:F; G1:I}, "where Col1 <>'' ")
and see if that works.
If you need it ordered by date:
=query({D1:F; G1:I}, "where Col1 <>'' order by Col2")

Resources