How to use Column Value as header in Query in google sheet - google-sheets

I was trying use query to show results as below. But I couldn't figure out how to use Column A (months) as a header.
I knew how to use Vlook or Filter function to generate same results. Since my data is huge, I prefer to use Query function to keep my data flexible. Thank you for helping.
https://docs.google.com/spreadsheets/d/1ljrMdM4m0YT35EP7HxY-Gdj9SrEv6DHoQw_qL8Z9c-Y/edit?usp=sharing

Something like this should work, needs tweaking with ordering though.
=query(Sheet1!A1:C71,"select B, sum(C) group by B pivot A")

Related

Unpivoting using QUERY function is not fetching the expected result

I am currently working on a dataset that includes several columns, mostly the dates. What I am trying to achieve is - unpivot all the date columns to use for my subsequent calculations. I use the following formula to unpivot: =ARRAYFORMULA(SPLIT(FLATTEN(Data!A2:A&"|"&Data!D1:AG1&"|"&Data!D2:AG),"|"))
Even though this returns the expected result, when I try to nest this within a Query function, it does not work correctly. This is how I applied the formula - QUERY(ARRAYFORMULA(SPLIT(FLATTEN(Data!A2:A&"|"&Data!D1:AG1&"|"&Data!D2:AG),"|")),"Select * WHERE Col3 IS NOT NULL")
PS: When I change the data range to say, A2:A100, it gives me the correct result. However, it does not help since lot of new data would get added and I want the formula to be dynamic.
Here's the link to the sample sheet - https://docs.google.com/spreadsheets/d/1dgFY5mT9nUJtFefjAros-XpWXRMFtxEf8Fqrv82N5Ys/edit#gid=1813140523
Any help/suggestions would be highly appreciated
Not sure where you got your SPLIT(FLATTEN technique,
but you have to include both the 3rd and 4th parameters of the split function as FALSE or 0. so in your case it would be:
=ARRAYFORMULA(SPLIT(FLATTEN(Data!A2:A&"|"&Data!D1:AG1&"|"&Data!D2:AG),"|",0,0))
If you do that you'll find your query works.
Also note that the way you have it it's not really working. If you look all the way down in column 1 you'll find a bunch of dates formatted to look like integers.

Google Sheets Order By Query isnt showing any data

I am trying to bring in my list of employees from another file and have them sorted when they come in. When I do this:
=QUERY(importrange(K1, "Employees"), "Select *", 1)
Everything works great, but as soon as I add the order by method,
=QUERY(importrange(K1, "Employees"), "Select * order by Col4", 1)
It no longer works and just shows the header. Col4 is the correct column and if I use "Select Col4" all of the correct data shows up once again. Any idea of why this is happening?
Try putting your IMPORTRANGE() function in curly brackets. I was able to reproduce (what I believe to be) a similar error in my sheet.
When I defined a range to query, it would not work when using "Col4". This did work when I used D, though.
However, when I then placed the range into curly brackets, it understood Col4, and was able to order it.
Given that you did not get an error and instead got no values, make sure that the values in the column you are sorting by are of one majority datatype.
For example:
In this, the majority of column 4 are numbers, so when the data is sorted all the letters are left out.
My best advice is to simply check your datatypes. Dissimilar types won't sort properly with QUERY.
Make sure the formatting of the source data is not mixed.
Example: Text, Number, ...
Format the columns in the same format to the last row.

Google sheets formula - lookup (alphabetical sort)

I want to create a formula, that gets me the specific value(s) from row in another table. The formula I've created
=LOOKUP(E5;Ingredients!$A$6:$B$49;Ingredients!$F$6:$F$49)
gives me false results. But when I sort the values by alphabet the results are correct.
Is there some way to create a formula that is not dependent on alphabetical sort of source table?
From https://support.google.com/docs/answer/3256570?hl=en-GB
"Notes:
The LOOKUP function will only work properly if data in search_range or search_result_array is sorted. Use VLOOKUP, HLOOKUP or other related functions if data is not sorted."
Personally, I've never really used the lookup functions because of issues like this, so I'm a bit rusty on the specifics of how they all work. My go-to is the INDEX MATCH solution, which might be something like
=index(Ingredients!$F$6:$F49, match(E5, Ingredients!$A$6:A$49))
What I'm also not sure about is how Lookup is supposed to work when you're giving it more than one column as the input, though; you're giving it A and B - I thought that syntax was for an array where the output comes from the last column, and I don't know what happens if you then specify the output column as well, as you've done.

How to pick one of the results of UNIQUE() in Google Sheets

Problem
I have a column with duplicate items in Google Sheets, and I would like to get one of the unique values (say, the last one) in the cell of the formula. Is there a way to do this with just formulas (i.e., no scripts/macros)?
What I've tried
Not sure if this is the best way, but I've tried using the UNIQUE(range) function, which returns a list of distinct values, and I tried to pick one with FILTER(range, condition1, [condition2, …]), but I've only managed to do it when I know in advance and hard-code in the number of unique values.
Since I can get the length of the unique list with =LEN(UNIQUE(my_range)), I tried using the REPT(text_to_repeat, number_of_repetitions) function.
For example,
=REPT(0&";",2) & 1 returns "0;0;1"
but
=FILTER(UNIQUE(A$1:A$26), {REPT(0&";",2) & 1})
(or any variation I tried) doesn't quite work.
P.S.
I realise this is not the most suitable problem for a spreadsheet, and I do wish I was using something like Python, but this is the restriction at the moment.
try:
=QUERY(UNIQUE(A1:A), "offset "&COUNTA(UNIQUE(A1:A))-1)
Or more old-school using index:
=index(unique(A:A),counta(unique(A:A)))
You can also just enter a number fot the one you want e.g.
=index(unique(A:A),2)

How to keep the hyperlink while using =QUERY formula in google spreadsheet?

I wrote a =QUERY formula in Google spreadsheet. However I would like to copy not only the values of the cells but also the embedded links from the range of cells I am performing the query on. This is what I wrote:
=QUERY('Tab'!6:1963,"select C where (E='Major' and D >= now())")
There must be a way to tell the query to bring the URL as well along the content of the cells.
The query function only supports certain data types:
Supported data types are string, number, boolean, date, datetime and timeofday.
It doesn't handle other things one might embed into a spreadsheet, such as images or hyperlinks. (Hyperlinks are coerced to strings.) After all, the query language is not something Sheets-specific, it has its own data models that interact with Sheets only to an extent.
A solution is to use filter instead of query, if possible. It can do many of the things that query does. For example,
=QUERY(Tab!6:1963,"select C where (E='Major' and D >= now())")
can be replaced by
=filter(Tab!C6:C1963, (Tab!E6:E1963="Major") * (Tab!E6:E1963 >= now()))
which will return the links as expected. (And even images inserted with =image() if you got them.) The multiplication operator is logical and in the filter formula.
I know this is three years later, but I ran into this issue and my query would have converted to a complicated nest of FILTER and SORT functions. So I ended up doing something like this: ARRAYFORMULA(VLOOKUP(QUERY('Tab'!6:1963,"select C where (E='Major' and D >= now())"),C:C,1,FALSE))
Which worked.
Often this question comes into play with IMPORTRANGE. And Google's official answer does not really help (i.e. QUERY only works with strings, etc.). It is possible to give the filter range as an imported range, too, then it works:
=FILTER(IMPORTRANGE("XXX","Data!A1:A),
IMPORTRANGE("XXX","Data!B1:B")>0)
where column A is the data you want to import and column B is the filter

Resources