I am trying to populate columns Product Name and Supplier. However, the template I am filling in also has Description in column C. How can I amend the QUERY formula I have to essentially say skip column C and populate relevant data in column D (of course I'd insert the relevant column inside SELECT..)? Or alternatively, can I amend the QUERY to fill the Description column with blanks and then move on to the Supplier column?
you can use something alike this:
=QUERY(A1:B4,"Select A,' ',B LABEL ' '''")
QUERY accepts "anything" as the select clause, so you can do:
=QUERY(data,"select D, '', F offset 1 label '' ''",0)
Or without all the quotes:
=QUERY(data,"select D, 0/0, F offset 1 label 0/0 ''",0)
Related
(Related to this question)
I want to split the values in each cell, that is either blank or contains one or more comma-separated tags. Can I do this from within the QUERY? Or, how would I copy the column to a scratch column that is longer because the cell values are split into one or more columnar values?
This formula works nicely to show tags and counts, but treats each cell as a single text value:
=QUERY(Notes!D1:D, "Select D, count(D)
where D matches '^(?!(?:Labels|Tags)$).+'
group by D order by count(D) DESC label count(D) ''")
I also have this formula, which returns an array of non-blank, comma-separated values in a range:
=ArrayFormula(SPLIT(filter(Notes!D1:D, not(isblank(Notes!D1:D))), ","))
But this also has the problem that it splits values across columns (instead of rows), so I can't use the results as a simple range.
I have tried wrapping occurences of D, the data column, with the ArrayFormula. Each time I get a #VALUE! error from QUERY.
For what I get you're trying to do, you may find useful to FLATTEN your range and make it all in one column:
=FLATTEN(ArrayFormula(SPLIT(filter(Notes!D1:D, not(isblank(Notes!D1:D))), ",")))
Just if needed, you can add TRIM too so you don' have undesired spaces:
=FLATTEN(ArrayFormula(TRIM(SPLIT(filter(Notes!D1:D, not(isblank(Notes!D1:D))), ","))))
I don't know what your purpose then is, but you can wrap this in a QUERY to count as you expressed in your post too. Since it's a new column, you should name that column Col1:
=QUERY(FLATTEN(ArrayFormula(TRIM(SPLIT(filter(Notes!D1:D, not(isblank(Notes!D1:D))), ",")))),"Select Col1,COUNT(Col1) group by Col1 order by count(Col1) DESC label count(Col1) ''",)
I have a 'Raw Invoice' tab which is an excel file copy/pasted directly over. I'm then trying to format the data in the 'Invoices' tab in that column order using a query. I need to be able to break out the Student Name into two separate columns, hopefully within the query itself. Preferably it would then change it so column C is Last Name and column D is First name and the rest of columns shift over one.
I don't know if there's a way to perform a SPLIT function within the query. Right now I'm using a clunky method by doing a VLOOKUP on the student ID to get the names from another tab (not included in the Sample GS cuz it's an importrange from a work file), but it then creates two separate queries. Ideally I can somehow split column C within one query, but am getting lost by nesting queries and arrays together. I might be able to use REGEXEXTRACT, but again get lost in where to put it in the query or whether that's overkill.
QUERY('Raw Invoice'!$A:$I,"Select F,B,A, 'Bobs Diner',D,G,I where C is not null label 'Bobs Diner' 'Company' Format F 'M/DD/YYYY' ",1)
Link to sheet.
split in query arguments is not possible but you can do:
=ARRAYFORMULA(QUERY({IFERROR(SPLIT('Raw Invoice'!A:A, ", ")), 'Raw Invoice'!B:I},
"select Col7,Col3,Col1,Col2,'Bobs Diner',Col5,Col8,Col10
where Col3 is not null
label 'Bobs Diner' 'Company'
format Col7 'M/DD/YYYY'", 1))
Wat I suggest is to implement in Row Invoice
=arrayformula(split(A3:A,","))
and then
=QUERY('Raw Invoice'!$A:$K,"Select F,B,J,K, 'Bobs Diner',D,G,I where C is not null label 'Bobs Diner' 'Company' Format F 'M/DD/YYYY' ",1)
I'm busy with creating an spreadsheet where I can get the average price of holidaydeals with a specific tag (f.e. a destination). When I do this for only one tag column the formula is working :) :
=averageifs(C4:C10,E4:E10,L1,F4:F10,"Frankrijk vakantie")
But... I have more then 10 tag columns in total and in every column something like "Frankrijk vakantie" could be found. My simple mind thought, okay lets change F4:F10 (in this example) to F4:G10 to look for "Frankrijk vakantie" in two columns. But... the formula didn't work.
Link to spreadsheet: https://drive.google.com/file/d/1Gw5VC5qT1bzbFIPBu5j4dLXBmJjh7GuW/view?usp=sharing
I've also added a screenshot. I hope that someone can help me with this. Would be great, thank you!
In L2 try
=query({C4:C11, ArrayFormula(N(mmult(N(F4:O11="Frankrijk vakantie"), transpose(column(F3:O3)^0))>0))}, "Select AVG(Col1) where Col2 > 0 label AVG(Col1) '' ")
and see if that works?
EDIT: to include a month/year filter try
=query({C4:C11, ArrayFormula(N(mmult(N( (F4:O11="Frankrijk vakantie")*(E4:E11=L1)), transpose(column(F3:O3)^0))>0))}, "Select AVG(Col1) where Col2 > 0 label AVG(Col1) '' ")
The formula makes use of a virtual array, containing the values of column C and the output of the mmult() function. The latter creates a column with 1's if 'Franrkijk vakantie' is found in that row AND the date in column E matches the date in L1. The query then averages the values from column C and filters out the rows where the conditions of the MMULT() are not met.
EDIT 2: To check for a 'double match' in the row, try
=query({C4:E11, transpose(query(transpose(F4:S11),,9^99))}, "Select AVG(Col1) where Col3='"&L1&"' and Col4 contains 'Frankrijk vakantie' and Col4 contains 'Europa vakantie' label AVG(Col1)''", 0)
Change range to suit.
=ARRAYFORMULA({
AVERAGE(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(Data!D$2:E856<>"", "♠"&Data!D$2:E856&"♦"&Data!F$2:G856, )),,999^99)),,999^99), "♠")), "♦"),
"select Col2
where lower(Col1) contains '"&LOWER(A2)&"'
offset "&COUNTIF(Data!D$2:E856, A2)-6))})
I have the above formula that I am using. What this does is the following:
The last 6 times A2 shows up in either Column D or E, it accumulates the corresponding value in column F or G. Those 6 values are then turned into an average, as well.
I am trying to add one condition to this. I want it to only take those last 6 instances where column H and I are also something specific.
So when A2 shows up in column D, I only want to use the row if Column I is the value "X". When A2 shows up in column E, I only want to use the row if Column H is "X".
I am unable to get this implemented into my function myself and desperate for some help. One problem is that the "X" search is in reversed column order (ie. when A2 is in D, trying to search I...and when A2 is in E, trying to search G...which isn't the order those appear in the alphabet). Also just not sure where in formula this conditional even needs to go.
Data! just references the sheet I have my dataset dumped into, obviously.
I'm using Google Sheets and using the following formula without any issue:
=QUERY(Sheet!A:D,"select A, max(B) where not B is null group by A label A 'Client', max(B) 'Most Recent'")
Obviously, this is returning columns A and B of the referenced data
However, I would like to also return the corresponding cells from column C.
sample data: https://docs.google.com/spreadsheets/d/1CJKiDNPUWMMUbLLb-UDA6iRA2PBnlMHDsEB9vELe0Rk/edit?usp=sharing
Using this example, what I would like to see is cell G2 populated with "Pizza" (i.e., from C3), and so on.
Am I using the right formula? What do I need to change?
What you are trying to do is not very SQL-like, because max(B) does not point to any particular row: it's just the maximum value of B among the selected rows. This value could be attained by several rows, making the choice of C, D ambiguous. I don't think that a single query can do this.
Here is an implementation with unique and several query commands.
In E2, enter =unique(A:A) to get the list of unique client names.
In F2, enter
=query(A:D, "select B,C,D where A ='"&E2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so either add label B '', C '', D '' or just don't include the header row in the queried range.
Version with grouping by C,D
To also select C and D within a single query, expand the select clause
select A, max(B), C, D
which will then require including those in the group by clause:
group by A, C, D
The formula will be
=QUERY(A:D,"select A, max(B), C, D where not B is null group by A, C, D label A 'Client', max(B) 'Most Recent'")
This does mean that the only rows to be grouped together will be those where not only A but also C and D are equal.
A possible one-formula solution:
=ArrayFormula(IFERROR(VLOOKUP(UNIQUE(A2:A),SORT(A2:C,2,0),{1,2,3},0)))
Here is what I did that finally worked.
In E1, enter =unique(A:A) to get the list of unique client names. In F2, enter
=query(A$2:D101, "select B,C,D where A ='"&F2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so just add desired text in header row.