Dynamic Sheet Name in Query in Google Spreashsheet - google-api

In Google spreadsheet I want to query data in another sheet but the problem is that the name of sheet is present in a cell. So is there a way in QUERY function to dynamically mention sheet name. Basically I am trying to do something like this but with dynamic sheet name:
=QUERY('2012'!A2:F;"select C, sum(F) where A='December' group by C order by sum(F) desc")
I tried to do this but I get Parse Error:
=QUERY(INDIRECT("Overview!L5")!A2:F;"select C, sum(F) where A='December' group by C order by sum(F) desc")
In which Overview!L5 is the cell with sheet name to query. I also tried to concatenate quotes around INDIRECT but that didnt work either.
I think it is quite evident what I am trying to do from the query i.e. get sum of values in cells grouped by values in other cells.

the INDIRECT looks to be the problem.
Try like this:
=query(INDIRECT(A1&"!A5:A10"),"select Col1")
i.e. if Cell A1 contains "food" the above is the same as:
=query(food!A5:A10,"select A")
and the same as:
=query(INDIRECT("food!A5:A10"),"select *")
**Note: the indirect uses "Col1" etc and not "A" because it does not pass the col letters.
Also ... The google groups forum might be a good place to look for spreadsheet formula answers. productforums.google.com/forum/#!categories/docs/spreadsheets

Easiest way to use dynamic structures in a query is to not include functions inside query but prepare strings in separate cells, for instance A1 for address B1 for arguments and then just QUERY(A1;B1)

Related

Query referencing 20 sheets / Indirect error with multiple ranges

I have 20 sheets (Eagle, Kestral etc) and want to reference the whole group of them, in different queries.
To stop query formula text being massive I have tried to use the Indirect function but looks like Indirect may not be able to return multiple ranges.
Example for just 2 sheets:
Query({Indirect(A1)}) where A1 contains the text Eagle!F3:I33;Kestrel!F3:I33
gives Indirect error "not a valid cell/range reference".
The 2 formulas below work OK but become unweildy when referencing 20 sheets.
Query({Eagle!F3:I33;Kestrel!F3:I33})
Query{indirect(A2); indirect(A3)} where A2 is Eagle!F3:I33 and A3 is Kestrel!F3:I33
Suggestions please (no script).
Challenge2 = How to include sheet name (bird) in Col1 of query output. Sheet name (bird) is written in cell A1 of each sheet.
Here is the solution that I settled on.
Problem summary
Challenge 1: Avoid oversized query formula when referencing many sheets/tabs.
Challenge2: Return sheet name as part of the query output.
Key information
Script is not an option as causes access and performance issues for users in my organisation.
Indirect function cannot pull multiple ranges into a Query.
There is not a function that returns sheet names (except within Script).
I started with a static list of sheet names.
Each sheet contains Name and Total data, but needs to be tagged with sheet name to identify it in output of query. Each sheet also included the sheet name in cell A1 (but not used in solution).
Solutions
Solution to Challenge 1: Specify the unique sheet ranges & select statements within hidden helper columns then reference them in the query.
Solution to Challenge 2: Insert sheet name as text within each select statement.
=query(
{query({indirect(B4)},C4);query({indirect(B5)},C5);
query({indirect(B6)},C6);query({indirect(B7)},C7);
query({indirect(B8)},C8);query({indirect(B9)},C9);
query({indirect(B10)},C10);query({indirect(B11)},C11);
query({indirect(B12)},C12);query({indirect(B13)},C13);
query({indirect(B14)},C14);query({indirect(B15)},C15);
query({indirect(B16)},C16);query({indirect(B17)},C17);
query({indirect(B18)},C18);query({indirect(B19)},C19);
query({indirect(B20)},C20);query({indirect(B21)},C21);
query({indirect(B22)},C22);query({indirect(B23)},C23)}
,"where Col3 >="&F2 &B2 ,0)
useful screen shot - helper columns and output
Cells F2 & B2 are user defined. F2 is the minimum value to return. B2 relates to ordering of output.
B2 creates an extra bit of text for select statement, depending on user defined dropdown in E2.
=if(E2="order by lap count"," order by Col3 desc",)
The ,0 at the end of the final wraparound query is the optional query header row clause. Zero tells query that the input data has no headers. Necessary for this query.
The curly brackets inside each sheet query convert column names F, G, H to Col1, Col2 Col3.
The curly brackets and semicolons in the final wraparound query combine the sheet query outputs into an array, one underneath the other.
Top Tip – When referencing multiple sheets/tabs in a query, it is better create a wraparound query (as above) to filter the output . This is because if you were to filter the individual sheet queries and one of them returned no data, the curly brackets in the wraparound query would return an array error.

Returning the last value from the FILTER function in google sheets

I'm using Google Sheets and would like to get the last value in column when it is filtered based on the values in a separate column as shown in the screenshot:
I'd like to get the last value from column A, where the value in column B matches that specified in cell D1.
I've managed to do this with the following:
=INDEX(FILTER($A:$A,$B,$B=$D$1),COUNTA(FILTER($A:$A,$B:$B-$D$1)),1)
This works but it seems unnecessary to have the second FILTER and COUNTA as it makes it harder to understand. Is there no way I can just return the last value from the FILTER function?
Since posting this I've found another way that's more concise, but I have to confess I don't actually understand how it works:
=ArrayFormula(LOOKUP(2,1/($B:$B=$D$1),$A:$A))
Here you will know about sort and array_Constrain:
=array_constrain(filter(sort({A1:A,row(A1:A)},2,false),B1:B=D1),1,1)
or you can use query:
=query(filter({A1:A,row(A1:A)},B1:B=D1),"Select Col1 order by Col2 desc limit 1")
or you can use indirect:
=indirect("A" & max(filter(row(A:A),B:B=D1)))
I know this is a way that I do that sometimes. it takes advantage of the VLOOKUP(....TRUE) [default] option.
=VLOOKUP(9^99,FILTER({ROW(A:A),A:A},B:B=D1),2)

How to skip columns in “List from a range” Criteria?

Is it possible to create a "List from a range" Data Validation rule in Google Sheets where the range skips columns?
For example:
Cells A6:A11 is limited to the range A1:B3. Cells B6:B11 is limited to the range A1:A3 AND C1:C3 (skips column B).
Creating a Data Validation rule for cells A6:A11 is trivial as I simply need to create a Criteria of "List from a range = A1:B3".
However, creating the Data Validation rule for cells B6:B11 is not so intuitive since Google Sheets does not allow me to create a Criteria using the syntax "List from a range = A1:A3, C1:C3".
Does the "List from a range" Criteria support a syntax that allows us to skip columns within a range?
Note: I currently have a work around for this where I defined an array formula in D1 = =ArrayFormula(if({1,""},A1:A3,C1:C3)) and then use D1:E3 as the Data Validation range. But this is a hacky solution and I'm hoping there is a better way to accomplish my goal.
The solution is to use { } to create a combination of columns or rows that will result in some sort of virtual table on-the-fly.
Example:
Assuming you have a spreadsheet with Name, Age, Gender, Phone and Address in A, B, C, D and E, and you want to skip the Gender (column C) while using the UNIQUE statement, you can use something like this.
Put in G1 the following formula:
=UNIQUE({A1:B, D1:E})
From the cell G1, the spreadsheet will populate the columns G, H, I and J with unique combinations of A, B, D and E, excluding the column C (Gender).
The same application of a combined range can be used in any formula and also you can combine multiple different ranges, including cross Spreadsheets and Files.
It is a very useful trick if you need to combine pieces of multiple spreadsheets for data visualization or reports. However, always remember you cannot manipulate the displayed data. You can still search through it, format it, etc., but you cannot change it. On the other hand, it will auto-update always if the data source gets updated, which is very useful.
Note: Try it with LOOKUP, VLOOPUP or HLOOKUP.

Empty filter results gives #VALUE! error

I've been working on this project where I need to consolidate data from two other sheets within the spreadsheet and filter the result for easy viewing. But I realized the problem when the filter gives no result there will be a #VALUE! error. The error isn't solved even when I have used IFERROR.
Link to the sample of the Google Spreadsheets.
There are two classes and I wish to filter out those who passed in the class and populate the table in the collated sheet.
You should be able to do something like this:
=query({Class1!A2:C; Class2!A2:C}, "where Col3 = 'pass'")
(change the sheet names and ranges to suit !).
(Also check the formula I entered in A1 of sheet 'JP')

Google Spreadsheet filter

I'm trying to filter a list of employee by department in google spreadsheet
=filter(A:C,!B2:B6=D2)
my sample data is
Name Department age
asd dep1 12
fds dep2 2
plo dep3 3
sdfsdf dep1 56
shg dep2 98
now I already got the filter part working.. my question is after filtering. how can I tell GSpreadsheet to only display specific column values... like use department as filter condition but only return name and age?
eventually I also want to move my result to a different sheet.
My first choice of approach for this issue was to use google macro like thingy and just hide the rows and comlumns not needed if you have suggestions for this please don't hesitate to provide.
The easiest way to filter is the [Data-Filter], otherwise [View-List] is also able to filter. Using a formula you will be displaying the results in a different column, usually on a different page.
My suggestion to omit column B would be to make two separate column filters like this:
=filter(A:A,B:B=D2)
=filter(C:C,B:B=D2)
The exclamation is only necessary if you are on another sheet, you may also be trying to do something like this: filter(A2:A,B2:B="dep1") the real power of the formula is where you want to manipulate the value before your comparison like this: filter(A:A,right(B:B,1)="1") otherwise I'm not sure what you are referring to, other than Google Apps Scripts I am not aware of any functionality very similar to VBA.
If this did not answer your question can you be a little bit more specific?
I would use the QUERY function here:
=QUERY(A:C, "select A,C where (B = 'dep1')")
This will return with two columns (Name and Age - that is "A" and "C" respectively). The filter condition is provided in the WHERE clause.
For moving the result to another sheet, I would do it another way around: Create another sheet, and then write:
=QUERY(employees!A:C, "select A,C where (B = 'dep1')")
Assuming that your original worksheet's name is "employees".
I hope it helped.

Resources