Google Sheets' dynamic data range in query function - google-sheets

is it possible to have a dynamic data range when using query function in Google sheet?
What I would like to do is, using a dropbox, change the data range used in query function.
For example, I have 4 tables in 4 different sheets. On my main sheet, I want using my dropbox, perform a query on my selected table.
Is it necessary to do it with a script?

You can make a dynamic query without using a script.
The query string can contain a reference to other cells.
Example in Sheets.
This example has a pulldown for the data set in B2, a pulldown for the value set in B4. The data ranges include one from another sheet. I am using named ranges to simplify the lookup process. Each data set n is named DataN.
You can separate out the query string from the cell with the actual query function call. The trick is to build up a query string using INDIRECT, COLUMN, and VALUE. I placed this in cell A10:
="select " & mid("ABCDEFGH",COLUMN(INDIRECT(B2)),1) & " where " & mid("ABCDEFGH",VALUE(COLUMN(INDIRECT(B2)))+1,1) & "=" & """" & B4 & """"
The four quotes let us place a literal quote in the query string. The '&' character does string concatenation.
The use of MID as a way of translating the COLUMN function to a letter I got from here.
Then your cell with the query uses the values of the data set pulldown (B2) and the value of the query string (A10) this like:
=QUERY(INDIRECT(B2),A10,1)

Related

Combining a Cell Reference and Wildcard as Criterion in Google Sheets CountIf Formula

I'm struggling with writing the proper syntax for this formula in Google Sheets. In one sheet called Game Log, in the H column I have data that can be a range of names (1 - 10 names per row). I'm trying to construct a COUNTIF statement that would search for the name in all the rows for that column. There can be several other names in the same column so I need to use the wildcard * to find any occurrence of the name in each row. So for example, the current code below would count all occurrence of Adam in the rows.
=COUNTIF('Game Log'!H3:H102, "*Adam*")
What I would like to do is replace the hard codes "Adam" with a cell reference (in this case B2). Is it possible to combine that cell reference with the wild card? The know the code below doesn't work (as it would return text counting occurrences of B2), but is something like this possible?
=COUNTIF('Game Log'!H3:H102, "*B2*")
Have you tried something like this?
=COUNTIF('Game Log'!H3:H102, "*" & B2 & "*")
That ought to look for any string value, followed by the cell value, followed again by any string value. It's essentially just performing separate checks, in sequence, which allows you to search for different value types (in this case string wildcard + cell value + string wildcard).

Google Sheets Query and Comma Separated Data

I have a simple data query in a Google Sheet:
=query('2019'!$A$2:$A,"select A")
The data in column A has multiple cells that contain comma-separated values. Only the data in the first cell (A2) returns with all of the comma-separated values. How do I get all of the data returned in this query?
It's very likely the problem is cause because the data without commas are of ty numbers, boolean, dates, time, duration, in other works, they are not text values.
This happens because QUERY assigns a data type for each column based on a sample of each column data. If the column includes data of different type, they aren't included.
The way to solve this is to prior adding the data to QUERY convert all the column values to the same data type, in this case all should be text. To force that all the values are treated as text you could preppend and apostrophe / single quote. Other methods are
set the cell number formatting to plain text
concatenate the cell value to an empty text "" like =A1&""
try like this:
=ARRAYFORMULA(QUERY(TO_TEXT(2019!A2:A), "select A", 0)

Google Sheets Query referencing a cell's text

When I perform this function, it works:
=Query(CRM!1:1085,"Select B where D contains 'FirstName LastName' ",4)
When I perform this one, where B31 is where the text "FirstName LastName" is located in the sheet, the output is only ONE of the many results:
=Query(CRM!1:1085,"Select B where D contains '&B31&' ",4)
I want to be able to use the cell rather than write the quoted text in the formula. This is because I need to quickly replicate the formula for many different values. Also because it needs to be automated in case of changes in the source.
B31 is interpreted as literal string, when you use just 'B31'. You need to use double quotes along with single ones and concatenate:
'"&B31&"'

Dynamic number of WHERE conditions in Google Sheet query function

I'm building a spreadsheet that can be used to import and then filter wireshark capture .csv's. The idea is - you import the .csv into the second tab, and on the first tab you are able to display the data based on a number of include/exclude filters. Here is a dummy sheet for reference:
https://docs.google.com/spreadsheets/d/17TtZHRiXCaH1XClq5SGRdIuyLZnMOrl6Ygrbfo9BbfM/edit?usp=sharing
As you can see in the spreadsheet, I have four possible "include" values and four possible "exclude" values, each being determined by selection of a drop down-list. The intention is for the QUERY() function in cell A6 to display the data based on these filters.
Example of the filter at work
I know how to use WHERE to do it for a single filter condition:
=query('Import CSV Here'!1:349,"select * where ("&B4&"="&B2&")")
And how to do it for multiple filter conditions:
=query('Import CSV Here'!1:349,"select * where ("&B4&"="&B2&") and ("&C4&"="&C2&")" )
What I can't figure out is how to write the QUERY function so that it only factors in a WHERE condition for filter columns that aren't blank (and therefore have been actively populated from their drop down menus). Is there an efficient way to do this?
You can wrap each filter condition in an if clause and use put something that evaluates to true if it is blank.
=query(
'Import CSV Here'!1:349,
"select * WHERE " &
IF(ISBLANK(B2), "1=1", B4 & "=" & B2) &
" and " & IF(ISBLANK(C2), "1=1", C4 & "=" & C2) )

QUERY using cell contents as SQL variables

Here is what the spreadsheet I'm working with looks like:
Summary table
(top of sheet)
Source data
(same sheet, below output)
I'm using the QUERY function to populate the appropriate feeds in the summary table with the data starting at A24 needs to be placed into.
Here is the formula I'm using in cell C6 (similar formulas are used throughout the summary table):
=QUERY($A$24:$D$57, "Select D Where B='ENQ' and A='2/27/14 - Thu'")
This gets the right information, but the formula needs to be edited to be unique in each cell it's used in. The problem being unable to quickly populate the cells with A='2/27/14 - Thu' being too specific.
I was trying to set it up so that:
date in A would compare with dates found on headers in ROW 2 before accepting data
room type in B would compare with value from A in each row of the summary table
How can the QUERY function be written to refer to these values as variables, instead of using the literal strings in my original function?
Instead of fixed strings such as 'ENQ', you can have your formula refer to the index in column A of your output. For example, you could change your formula to this, in cell C4:
=QUERY($A$24:$D, "Select D Where B='" & $A4 & "' and A='2/27/14 - Thu'")
^^^^^^^
The ampersand (&) is used to concatenate string segments.
Note that since the source data extends to the bottom of the data range of the sheet, we can forego specifying the bottom row. The range $A$24:$D will take in all rows, so it will automatically adjust to additional source data.
To compare dates, both values need to be dates. "2/27/14 - Thu" is not recognized as a date in your source data sheet, but as text, even though you've set the numeric format to date. So make sure you change all your source data to have actual dates in column A.
You can still have your dates formatted the way you like - see this screenshot. The content of A2 is now a proper date, "2/27/2014", but the format is set to display "mm/dd/yy - DDD". As long as the data is a date, the query can be built using the spreadsheet TEXT function to interpret the date in its yyyy-mm-dd format.
With dates in your source column, you can use todate() scalar function to tell QUERY to convert the date in the source to a query-date, and the date keyword to treat the following literal string (from C2) as a date.
The resulting function for cell C4 is:
=QUERY($A$24:$D , "Select D Where B='" & $A4 & "'and todate(A) = date '" & TEXT($C$2,"yyyy-MM-dd") & "'")
^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Similarly, for D4:
=QUERY($A$24:$D , "Select C Where B='"&$A4&"'and todate(A) = date '"&TEXT($C$2,"yyyy-MM-dd")&"'")
Your calculations in column E can be improved to handle #N/A results appearing in columns C or D:
=if(isna(C12*D12),0,C12*D12)

Resources