Google Sheets QUERY Date Criteria and Multi-Condition Formula
Generate Google Sheets QUERY formulas with date criteria without parse errors, then check the QUERY total against a FILTER sum and boundary dates.
Writing a QUERY formula in Google Sheets with date range conditions often triggers a PARSE_ERROR. For validating imported tabular data prior to formula aggregation, see table data validation.
[1] Define the source sheet structure and criteria cells. The sample below includes 6 rows designed to test boundary dates.
| Date (Col A) | Assignee (Col B) | Status (Col C) | Tasks (Col D) |
|---|---|---|---|
| 2026-10-01 | Alex | Completed | 15 |
| 2026-10-05 | Emma | In Progress | 8 |
| 2026-10-12 | Lucas | Completed | 22 |
| 2026-10-20 | Alex | Pending | 5 |
| 2026-10-31 | Emma | Completed | 14 |
| 2026-11-02 | Lucas | Completed | 19 |
Place criteria in dedicated cells:
- Cell
F1: Start Date (2026-10-01) - Cell
F2: End Date (2026-10-31) - Cell
F3: Status (Completed)
[2] Construct the prompt. The Google Sheets QUERY function relies on the Google Visualization API, requiring date literals formatted specifically as date 'yyyy-mm-dd'. When referencing cells, wrap the cell reference in the TEXT function to prevent syntax mismatches. QUERY itself follows the Google Docs help page; this guide's own design is to write the cell coordinates in the prompt so the model builds a formula with cell references.
You are a Google Sheets formula expert.Review the following Google Sheets data structure and filter conditions, and write a QUERY formula to aggregate task counts by assignee within a date range and status condition. [Data Structure]- Range: {data_range} (including header row) - {columns} [Sample Data]{sample_data} [Filter Criteria Cells]- {start_date_cell}: Start Date- {end_date_cell}: End Date- {status_cell}: Status [Requirements]1. Write a QUERY formula that filters rows where Date is on or after start date, Date is on or before end date, and Status equals target status, then aggregates the sum of Tasks grouped by Assignee.2. Ensure the date criteria avoid Google Sheets QUERY PARSE_ERROR by properly constructing date 'yyyy-mm-dd' literals using cell references and the TEXT function.3. Provide a brief explanation of each clause (SELECT, WHERE, GROUP BY, etc.) and an equivalent formula using the FILTER function for cross-checking results.[3] Execute the prompt to generate the formula.
$ agy -p "$(cat run/query.en.1.prompt.txt)" --model gemini-3.8-flash --effort high --output-format text > run/query.en.1.out.txtThe resulting formula is recorded in run/query.en.1.out.txt.
=QUERY(A1:D20, "SELECT B, SUM(D) WHERE A >= date '" & TEXT(F1, "yyyy-mm-dd") & "' AND A <= date '" & TEXT(F2, "yyyy-mm-dd") & "' AND C = '" & F3 & "' GROUP BY B LABEL SUM(D) 'Total Tasks'", 1)The WHERE clause dynamically formats cell values into date '2026-10-01' strings, preventing parsing errors.
| Clause / Component | Purpose |
|---|---|
A1:D20 & , 1 | Sets the source range and defines row 1 as the header |
SELECT B, SUM(D) | Selects Assignee and calculates the sum of Tasks |
WHERE ... | Combines A >= date '...', A <= date '...', and C = '...' |
GROUP BY B | Aggregates tasks per unique Assignee |
LABEL SUM(D) 'Total Tasks' | Renames the aggregated header column |
The expected output on the sample dataset:
| Assignee | Total Tasks || :--- | :---: || Alex | 15 || Emma | 14 || Lucas | 22 |Two separate executions (query.en.1, query.en.2) were run. The two formulas are the same once whitespace is normalized. The second run also gave a cross-check formula using LET.
[4] Cross-check the result. First, a check inside the sheet: add up the matching task counts directly with FILTER and compare with the sum of the QUERY total column. Compare sums rather than row counts because QUERY returns one row per assignee group while FILTER counts matching source rows. If one assignee has two completed records, the two numbers differ (in this sample every assignee is different, so they happen to be equal).
=SUM(FILTER(D2:D20, (A2:A20 >= F1) * (A2:A20 <= F2) * (C2:C20 = F3)))On the sample this formula should give 51, equal to the QUERY total column 15 + 22 + 14.
Outside the sheet, use the script. It extracts the =QUERY(...) formula from the response, substitutes the criteria values for F1, F2, and F3 to rebuild the query string that would be sent, applies it to the 6-row sample sheet, and compares the per-assignee totals with a separately computed reference. It understands only the SELECT col, SUM(col) WHERE ... GROUP BY col shape and does not connect to Google Sheets. It does not cover columns that mix date serials and text dates.
$ python3 run/check_query.py run/query.en.1.out.txt --lang enExtracted formula: =QUERY(A1:D20, "SELECT B, SUM(D) WHERE A >= date '" & TEXT(F1, "yyyy-mm-dd") & "' AND A <= date '" & TEXT(F2, "yyyy-mm-dd") & "' AND C = '" & F3 & "' GROUP BY B LABEL SUM(D) 'Total Tasks'", 1)Query it builds: SELECT B, SUM(D) WHERE A >= date '2026-10-01' AND A <= date '2026-10-31' AND C = 'Completed' GROUP BY B LABEL SUM(D) 'Total Tasks'Formula result: Alex 15, Emma 14, Lucas 22 (51)Reference result: Alex 15, Emma 14, Lucas 22 (51)PASS: matchThe formula from the second response (query.en.2) built the same query string and passed with the same result (2 of 2). The flaw test used 7 formulas: boundary operators changed to > and <, a missing date prefix, the date format yyyy/mm/dd, the status cell wrongly set to F2, the range A2:D20 with header argument 1, and two valid formulas (Col1 notation, and A2:D20 with header argument 0). All 5 faulty ones were rejected and both valid ones were accepted (not model output).
$ python3 run/check_query.py --test-flaws --lang en[rejected] case 1 boundary > and < -> as expected: Formula result Lucas 22 (22)[rejected] case 2 no date prefix -> as expected: UNSUPPORTED: unsupported condition "A >= '2026-10-01'" (date columns need date 'yyyy-mm-dd')[rejected] case 3 format yyyy/mm/dd -> as expected: UNSUPPORTED: TEXT format 'yyyy/mm/dd' is not yyyy-mm-dd[rejected] case 4 status cell F2 instead of F3 -> as expected: Formula result (0)[rejected] case 5 range A2:D20 with header 1 -> as expected: Formula result Emma 14, Lucas 22 (36)[accepted] case 7 header 0 on A2:D20 -> as expected: Formula result Alex 15, Emma 14, Lucas 22 (51)[accepted] case 6 valid, Col notation -> as expected: Formula result Alex 15, Emma 14, Lucas 22 (51)[5] Address edge cases. Both formulas the model produced in this run passed. A mismatch between the data range and the header argument changes the result, as case 5 above shows. The generated formula fixes the header argument to 1. If you change the range to A2:D20, the 2026-10-01 row is read as a header, Alex's 15 drops out, and the total becomes 36.
To use a range without the header row, append this instruction to the prompt requirements. This is the form that passed with total 51 in case 7 above.
- Set the data range to pure data excluding headers (e.g., A2:D20) and set the third header argument of QUERY to 0.