In Google Sheets, the IMPORTRANGE formula can connect two sheets and import data between them. The QUERY function is another powerful tool in Google Sheets. By using QUERY with IMPORTRANGE, you can import a range of data with specific criteria, such as filtering for values that are less than or greater than a specified threshold.
First, I will explain how to apply the less than (“<“) or greater than (“>”) comparison operators in a column containing dates. Then, we will move on to another example using a numeric field.
Example 1: Import Data Range with Less Than/Greater Than in Date Field
For this example, I have a two-column table in one Google Sheets file, with the range A1:B. The first column contains sales dates, and the second contains sales quantities. The tab name is “Sheet1.”
Date | Quantities |
1/12/2017 | 50 |
5/12/2017 | 100 |
10/12/2017 | 150 |
10/12/2017 | 200 |
15/12/2017 | 50 |
20/12/2017 | 10 |
21/12/2017 | 1000 |
Steps:
- Copy the URL of “Sheet1” from the address bar of your browser.
- In another Google Sheets file, enter the following IMPORTRANGE formula in cell A1:
=IMPORTRANGE("URL", "Sheet1!A1:B")
. - Replace
URL
with the copied URL from the first file. - Google Sheets may display
#REF!
. Hover over the error and click “Allow Access” to import the data. - To filter this imported range with less than or greater than conditions, wrap the formula in a QUERY formula.
Assume you want to filter rows where the dates in the first column fall between 10/12/2017 and 20/12/2017 (inclusive). Enter these dates in two cells, e.g., C1 and C2.
Replace the original formula with the following:
=QUERY(IMPORTRANGE("URL", "Sheet1!A1:B"), "Select * where Col1>=date '"&TEXT(C1,"yyyy-mm-dd")&"' and Col1<=date '"&TEXT(C2,"yyyy-mm-dd")&"'", 1)
Explanation:
When importing a range with less than or greater than conditions, it’s important to understand how to use the simple comparison operators in the QUERY function.
One more thing! The date criteria in cells C1 and C2 are in “dd/mm/yyyy” format, but when comparing dates, use the keyword ‘date’ followed by a string literal in the format “yyyy-MM-dd.” Example: date "2017-12-10"
or date'"&TEXT(C1,"yyyy-mm-dd")&"'
.
For more details, check out my tutorials:
- How to Use Date Criteria in the QUERY Function in Google Sheets
- Examples of the Use of Literals in Query in Google Sheets
Example 2: Import Data Range with Less Than/Greater Than in Numeric Field
Let’s filter the same data used in the first example. This time, we will import the sales quantities in column B within a certain threshold.
To import a range with less than or greater than conditions in a numeric field, use numeric literals like this: where Col2 > 0 and Col2 < 100
.
If the criteria are in cells C1 and C2, it should be specified as: where Col2 > "&C1&" and Col2 < "&C2&"
.
This is because numeric literals are specified in decimal notation in the QUERY function.
Example:
=QUERY(IMPORTRANGE("URL", "Sheet1!A1:B"), "Select * where Col2 > "&C1&" and Col2 < "&C2&" ", 1)
Additional Tip
What if you want to aggregate the imported data?
Using the sample data and formula from above, you only need to make one change to the formula:
Replace Select *
with Select SUM(Col2)
to aggregate the values.
Alternatively, you can use COUNT instead of SUM to count the records that meet the criteria.
Resources
- How to Use IMPORTRANGE Function with Conditions in Google Sheets
- Dynamic Sheet Names in Importrange in Google Sheets
- How to Vlookup Importrange in Google Sheets [Formula Examples]
- How to Use Query with Importrange in Google Sheets
- Dynamic Column Id in Query Importrange Using Named Ranges
- Relative Cell Reference in Importrange in Google Sheets
- Sumif Importrange in Google Sheets – Examples
Hi Prashanth,
Thank you very much for the tutorials and for your effort.
Can I share with you my sheets, of course without personal data? Could you please help me? I have to import data for the blue criteria (Sheet Test 02, Report): Column C 7,8 & 10,11. I can offer payment for your effort, it is very important for me.
— Links removed by Admin —
Thank you in advance!
Anita
Hi,
Sent request for access to your Sheets. I’ll try to help you!
Hi, Anita,
Please correct the date entered in cell G2,G3, and G47 (Sheet: Copy of Test 1, Tab: Professional Test). The date format is wrong. There are other issues.
I am not going into the detail of the solution here as other readers may not find it useful. So I am sending back you the files (I have made copies of the files for editing) via email.
Best,
Dear Prashanth,
First of all, thank you very much for this website, you are doing a great job.
I have the following problem, could you please help me?
I have 2 separate Spreadsheets, one of them contains a column with “Received date (for the whole year)”, the second one contains columns for each calendar week separately.
I have to import data from the first file to the other one, and I need a function that counts how many requests I received for each week separately.
For example, how many requests have I received in week 6 (04/02/2019-10/02/2019)?
Please 🙂
Anita
Hi, Anita,
I’ll try to help you with an example soon. Once I am ready with that tutorial, I’ll update you.
Best,
See if this tutorial helps?
How to Count Orders Per Week in Google Sheets
Just a little correction: Formula1 doesn’t work as typed. There are extra spaces within the single quotes enclosing the date to text conversions that throws a parsing error.
Instead of:
"&TEXT(B1,"yyyy-mm-dd")&" ' and Col1<=date' "&TEXT(B2,"yyyy-mm-dd")&" ' ",1)
it should be:
"&TEXT(B1,"yyyy-mm-dd")&"' and Col1<=date' "&TEXT(B2,"yyyy-mm-dd")&"' ",1)
Otherwise an excellent set of tutorials. Thank you very much! They've helped me considerably.
Sorry that 2nd line should be:
“&TEXT(B1,”yyyy-mm-dd”)&”‘ and Col1<=date'"&TEXT(B2,"yyyy-mm-dd")&"'",1)
Another space somehow made it into the formatting. HTML formatting problem on the site instead?
Hi, Ken,
You are right! I myself noticed that in the past.
I am posting the formulas after a thorough test from my side. But the issues happen when I publish my posts. If I post the formulas as CODE then the spacing issue happens and may often affect the Query formulas.
If I post the formulas as plain text formats, then the double/single quotes may cause the issue.
So the solution is to remove the extra space between single Double quotes in Query formulas (if the formula is posted as CODE) else retype the single double quotes.
Thanks for the time taken to notify me.