retrieve only unique values via query - sorting

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

Related

How to use Google Sheets to create table of Likert scale results grouped by counts

I want to take responses to Likert survey items and create a summary table of the counts of each response (text labels: strongly disagree, disagree, neutral, agree, strongly agree).
(sample data set & manually created desired results table available in this sheet)
I am only able to do one question at a time with QUERY.
=QUERY(likertTable,"select B,count(B) group by B",1)
Creating an array of QUERY functions does not always work when one option is not selected for a question, resulting in mismatched row size.
Is there a way to create the desired table with a single QUERY—or other (e.g., VLOOKUP, COUNTIF)—formula?
Try this, based on the order of criteria F3:F7
=query(arrayformula(split(flatten(B2:D2&"~"&vlookup(B3:D,{$F$3:$F$7,row($F$3:$F$7)},2,0)),"~")),
"select count(Col2) where Col2 is not null group by Col2 pivot Col1")
try:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(IF(B3:D="",,B2:D2&"×"&B3:D)), "×"),
"select Col2,count(Col2) where Col2 is not null group by Col2 pivot Col1"))
to sort, add "sort column" manually and pass through another QUERY:
=QUERY({ARRAYFORMULA(QUERY(SPLIT(FLATTEN(IF(B3:D="",,B2:D2&"×"&B3:D)), "×"),
"select Col2,count(Col2) where Col2 is not null group by Col2 pivot Col1")),
{"sort";4;2;3;5;1}},
"select Col1,Col2,Col3,Col4 order by Col5",1)

Google sheets query in need of a ifs statement

I am trying to select different column depending on the column where a certain string is found.
This is my curent query.
=QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26, Col3 where Col16 = '"&D27&"' or Col18 = '"&D27&"' or Col20 = '"&D27&"'")
Right now, it brings only 2 columns of data, but I would need it to also bring the columns following the one where it will find the sting "D27".
Try
=query({
QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26,Col3,Col16,Col17");
QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26,Col3,Col18,Col19");
QUERY(IMPORTRANGE(Configuration!I5,"Employes!A2:Z"),"select Col26,Col3,Col20,Col21")},
"select Col1,Col2,Col4 where Col3='"&D27&"'")

Google Sheets query order by row ends with

I have table of data which looks like:
J2150 IMPAC-BRIGA (RH)
J2283 BAYWA-FERGU (NK)
J2284 BAYWA-DIAPR (NK)
J2320 BOSCH-OWNER (ML)
J2475 GIPPS-GIPWF (NK)
J2568 GWFLD-CASTL-002 (PW)
J2663 AUSTRA-BARHA-001 (NK)
J2690 PHOTO-NEWAT (KT)
J2692 TETRI-MANGA (NK)
I'm using a Google Sheets query but I want to order the table by the project manager ie the initials at the end eg (CM).
I've been trying to use 'ends with' and 'order by' but this doesn't do what I want; eg
select Col2 where Col2 ends with '(CM)' group by Col2 order by Col2
I could separate out the initials into a new column in the original data, select and sort on that but is there an elegant way of sorting by the end of the row rather than the start?
try the below formula:
Assuming your data range is A2:B, if not then change your data range accordingly
=Query({A2:A,B2:B,Arrayformula(SPLIT(B2:B," "))},"Select Col1,Col2 where Col4 <>'' order by Col4")
=ArrayFormula(QUERY(REGEXEXTRACT(A:A,"^(.+?) (.+)$"),"select * where Col2 ends with '(NK)' order by Col2"))
I realised my original question wasn't precise enough: the rows are one record and not separate entries
J2150 IMPAC-BRIGA (RH)
J2283 BAYWA-FERGU (NK)
J2284 BAYWA-DIAPR (NK)
J2320 BOSCH-OWNER (ML)
J2475 GIPPS-GIPWF (NK)
J2568 GWFLD-CASTL-002 (PW)
J2663 AUSTRA-BARHA-001 (NK)
J2690 PHOTO-NEWAT (KT)
J2692 TETRI-MANGA (NK)
But I did want to sort on the two-letter suffix in the () at the end.
This is what I came up with:
query({'Project Overview'!$B$1:$B$373,
arrayformula(REGEXEXTRACT('Project Overview'!$B$1:$B$373,"\(([A-Z]{2})\)")),
'Project Overview'!$c$1:$d$373},
"select Col1, Col2, Col3, Col4 where Col3 is not null order by Col2
label Col1 'Project', Col2 'Project Manager'", 1)
This extracts the first column, 'B' and then uses REGEXTRACT to pull the 2-letter code from the same data as the second column.
Project
Project Manager
4-Oct-21
11-Oct-21
Kinley (CM)
CM
1
Kinley (CM)
CM
1
J1911 KINEL-KENT (CM)
CM
4
8
J2741 SIGNL-DARLP (DD)
DD
2
J2745 MANGO-FEASB (DD)
DD
1
J2754 CPENG-WANG (DD)
DD
16
8
J2754 CPENG-WANG (DD)
DD
20
16
J2754 CPENG-WANG (DD)
DD
4
DARETON O&M (JM)
JM
0.5
0.5
DARETON O&M (JM)
JM
2
4

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

Is there a function that cold help me summarize time intervals with duplicate values in some of them in google sheets?

I have a table that looks like this:
I want to make it look like this:
I tried with =unique() formula, but it doesn't work, since I quantities that duplicate in different time intervals. Thank you for your help!
Try
Query( {range} , "select max([end interval]), quantity",1)
Where the range is the raw data.
I would use offset ranges to detect when there is a change in quantity:
For start time
=ArrayFormula(query(
if((indirect("D1:d"&counta(D:D)-1)<>indirect("d2:D"&counta(D:D)))*(indirect("d2:D"&counta(D:D))<>0),indirect("A2:A"&counta(A:A)),""),
"select Col1 where Col1 is not null format Col1 'hh:mm'",0))
For end time
=ArrayFormula(query(
if((indirect("D3:d"&counta(D:D)+1)<>indirect("d2:D"&counta(D:D)))*(indirect("d2:D"&counta(D:D))<>0),indirect("b2:b"&counta(B:B)),""),
"select Col1 where Col1 is not null format Col1 'hh:mm'",0))
For quantity
=ArrayFormula(query(
if((indirect("D1:d"&counta(D:D)-1)<>indirect("d2:D"&counta(D:D)))*(indirect("d2:D"&counta(D:D))<>0),indirect("d2:d"&counta(D:D)),""),
"select Col1 where Col1 is not null",0))

Resources