Sorting received data from query - sorting

I have a Google Sheet where in sheet B C D E data gets entered and exported to sheet E via a query.
But it sorts it per sheet and not mix them in order.
I don't want a date order of sheet B then sheet C.
I need it that 1-1-23 on sheet D is in front of 31-12-2022 from sheet B
This is the query formula I use now
=QUERY({SheetB!A2:N;SheetC!A2:N;SheetD!A4:N;SheetE!A5:N},"select * where Col1 is not null",0)
Col1 is the column with the dates in it which need to be sorted.
I tried sorting via the Sort A-Z method that is not working I also tried to add Sorty By Col1 as shown below
=QUERY({SheetB!A2:N;SheetC!A2:N;SheetD!A4:N;SheetE!A5:N},"select * where Col1 is not null",0,Sorty By Col1)
Example sheet:
https://docs.google.com/spreadsheets/d/1gQUkJUW8ChWj59vtDu64rI1ozZjY6b5ABmzns6lgF9o/edit?usp=sharing

try:
=QUERY({SheetB!A2:N;SheetC!A2:N;SheetD!A4:N;SheetE!A5:N},"select * where Col1 is not null ORDER BY Col1 DESC",0)

Related

Importrange + query formula

Can anyone help? I'm trying to create a separate sheet that would automatically import the data from this source sheet below using importrange and query formula.
source sheet
I want to import specific column (columns A, E & F) data only if the column G is equal to "EUR". Here's my formula but it's not working for me.
=QUERY(IMPORTRANGE("this is where i put my source sheet URL","All Orders!A2:G1000")"select=col1,col5,col6 where col7=EUR") )
Try this:
=QUERY(IMPORTRANGE("this is where i put my source sheet URL","All Orders!A2:G1000"),"SELECT Col1, Col5, Col6 WHERE Col7='EUR'") )
Col is case sensitive and when you are comparing to a string you need to wrap it in single quotes.

Google Sheets Formula - Get Total from filtered dates per row (undefined number of columns)

I have this data in Google Sheets where in I need to get the total of the filtered data columns per row. The date columns are not fixed (may increase over time, I already know how to handle this undefined number of columns). What my current challenge encountered is how can I efficiently get a summary of totals per user based on filtered date columns.
My data is like this:
My expected result is like this:
My current idea is this:
Here is a sample spreadsheet for reference:
https://docs.google.com/spreadsheets/d/1_dByPabStGQvh94TabKxwFeUyVaRFnkBCRf4ioTY5jM/edit?usp=sharing
This is a method to unpivot the data so you can work with it
=ARRAYFORMULA(
QUERY(
IFERROR(
SPLIT(
FLATTEN(
IF(ISBLANK(A2:A),,A2:A&"|"&B1:G1&"|"&B2:G)),
"|")),
"select Col1, Sum(Col3)
where
Col2 >= "&DATE(2022,1,1)&" and
Col2 <= "&DATE(2022,1,15)&"
group by Col1
label
Col1 'Person',
Sum(Col3) 'Total'"))
Basically, its creating an output of User1|44557|8 -- it then FLATTENs it all and splits by the pipe, which gives you three clean columns.
Run that through a QUERY to SUM by the person between the dates and you get what you're after. If you wanted to use cell references for dates, simply replace the dates with the cell references.
To expand the table, change B1:G1 and B2:G2 to match the width of the range.

Expanding query with where or filtering in formula in google sheets

I have this formula which works great: =ArrayFormula(QUERY(TRANSPOSE(TRIM(SPLIT(JOIN(";",'TAB'!I2:I),";"))&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))
It takes values in column that are separated with ; and counts unique entries and plots everything in a table.
Question: I would like to add a filter/condition so that it would plot only the values what have specific entries in another column. Like A or B ir C, but not all values.
I have tried: =ArrayFormula(QUERY(TRANSPOSE(TRIM(SPLIT(JOIN(";",'TAB'!I2:I),";"))&{"";""})&'RAW-TODOS'!F2:F,"select Col1 * where 'TAB'!F2:F ='A'or 'B' or 'C', count(Col2) group by Col1 label count(Col2) ''",0))
but probably because of obvious reasons it did not work. Please help me with this one.
Thank you in advance.
try:
=INDEX(QUERY(TRIM(FLATTEN(SPLIT(FILTER('RAW-TODOS'!B:B,
REGEXMATCH('RAW-TODOS'!A:A, "NEW|IN PROGRESS")), ";"))),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"))
Input the criteria in J1, which is referred by my formula.
leave it blank to match all rows
for single value, simply input it, i.e. NEW
for multiple values, join them with |, i.e. NEW|IN PROGRESS
=ArrayFormula(QUERY(SPLIT(FLATTEN('RAW-TODOS'!A2:A&"♦"&TRIM(SPLIT('RAW-TODOS'!B2:B,";"))),"♦"),CONCATENATE("select Col2,count(Col2) where Col2 is not null",IF(J1="",," and Col1 matches '"&J1&"'")," group by Col2 label count(Col2) ''"),0))

retrieve only unique values via query

Here's the formula I currently use:
=query(IMPORTRANGE("XXXX","XXXXX!A:H"),
"select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8
where Col1> date '"&TEXT(F1,"yyyy-mm-dd")&"' and Col3 = '"&B1&"' and Col4 = '"&D1&"'
order by Col1 desc",1)
The formula is working.
Col1 includes input dates. I retrieve only values that are after a date listed in F1.
Col3 and Col3 include some properties which are selected in cells B1 and D1, accordingly.
Col5 includes strings (client names). client name can repeat on several rows.
I'd like to retrieve just the most recent one. Any ideas on how to do it?
And, to add more fun into the question, would it be the same idea to retrieve the oldest row per client?
Here's a link to demo sheet, details in the "unique query" tab.
Another challenge can be to retrieve X number of row per client, and not just the most recent one.
try:
=SORTN(QUERY(IMPORTRANGE("1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg", "Activity list!A:E"),
"where Col1 > date '"&TEXT(I2, "yyyy-mm-dd")&"'
and Col2 = '"&I3&"'
order by Col1 desc", 1), 99^99, 2, 4, 1)
SORTN explained:
99^99 all rows - no limits
2 means "merge mode"
4 collapse 4th column into unique values
1 return 4th column ascending - 0 for descending
Solution
I am basing myself on a SQL expression that would achieve this result but unfortunately Google Sheets QUERY language is not as expressive. That's why the resulting formula looks a bit confusing.
=query(
IMPORTRANGE("https://docs.google.com/spreadsheets/d/1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg/edit","Activity list!A:E"),
"select Col1, Col2, Col3, Col4, Col5
where Col1= date'"&
JOIN("' or Col1 = date '",
ARRAYFORMULA(TEXT(ARRAY_CONSTRAIN(
query(query(
"THE IMPORTED RANGE",
"select Col1,Col2,Col3,Col4,Col5
where Col1> date '"&TEXT(I2,"yyyy-mm-dd")&"' and Col2 = '"&I3&"'
order by Col1 desc",1),
"select MAX(Col1), Col4
group by Col4
order by MAX(Col1) desc
label MAX(Col1) ''", 0),
1000, 1),
"yyyy-MM-DD")
))&"'",1)
Queries specification starting from the inner one:
Filter the data with your criteria.
Get the most recent submission grouping by Client.
Join the results with the whole dataset to fetch the other column values.
Use the ARRAY_CONSTRAIN formula to retrieve the columns with the dates.
The same approach goes for the oldest submission changing MAX for MIN aggregate function.
Note: This is not suited for daily multiple submissions.
I think the easiest way to do this is using a Vlookup into a query(). unfortunately, it involves using the IMPORTRANGE() twice, but I still think it's more efficient than some other possible methods. You'll find it in A2 of the MK.Help tab on your sample sheet.
=ARRAYFORMULA(IFERROR(VLOOKUP(UNIQUE(query(IMPORTRANGE("1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg","Activity list!A:E"), "select Col4 where Col1> date '"&TEXT(I2,"yyyy-mm-dd")&"' and Col2 = '"&I3&"'
order by Col1 desc",1)),query(IMPORTRANGE("1LoHg53hzQvYtOLTcDwxLY8OrKVN4F7usX8YI41BtdWg","Activity list!A:E"), "select Col4,Col1,Col2,Col3,Col5 where Col1> date '"&TEXT(I2,"yyyy-mm-dd")&"' and Col2 = '"&I3&"'
order by Col1 desc",1),{2,3,4,1,5},0)))

ArrayFormula with sum of previous rows

I have an ArrayFormula to calculate a value for each row, and for each 6th row I want it to calculate the sum of the previous 5 instead.
Example sheet: https://docs.google.com/spreadsheets/d/18g2bOOBqsUgmy3ZXINOl6hcaMXf-uYJv7PGft247FjU/edit?usp=sharing
I have tried several routes, including google script, but keep banging my head against limitations of ArrayFormula.
You need make group by rows
My E.g
Cell A2 (Name groups):
=ArrayFormula(IF(B2:B<>"",FLOOR((ROW(A2:A)-2)/5)+1,""))
Column B (Your Data)
Cell E2 (Result):
=QUERY({QUERY({A2:B},"select Col1,sum(Col2) where Col1>0 group by Col1");
QUERY({A2:B},"select Col1,Col2 where Col1>0")},
"select Col2 where Col1>0 order by Col1,Col2 label Col2 ''")
Function References
Query

Resources