Sub topic 8
Summaries: SUMIF, QUERY, and ARRAYFORMULA
This chapter covers three functions that Gemini often produces when you ask for a summary: SUMIF, QUERY, and ARRAYFORMULA. You will learn what each one does, when to ask for it, and how to check it before you keep it.
Prerequisites. You can open the Gemini side panel, you know what a range such as D2:D means, and you have used SUM and IF. The examples use the monthly expense table from earlier chapters, with these columns:
| Column | Header | Contents |
|---|---|---|
| A | Date | Date of the expense |
| B | Category | For example Groceries, Rent, Transport |
| C | Description | Free text |
| D | Amount | Number |
| E | Paid By | Name of the person |
Row 1 holds the headers. Each later row is one expense.
Why these three functions
A summary answers a question about many rows at once, such as "How much did we spend on groceries?" Gemini can write the formula for you, but you still decide which function fits the question. Each function has a different job.
| Function | Job | Output |
|---|---|---|
| `SUMIF` | Add values that meet one condition | One number |
| `ARRAYFORMULA` | Apply one formula to a whole column | A column of results |
| `QUERY` | Filter, group, and sort like a database query | A small table |
If you know which job you need, you can name it in your prompt and get a better result.
SUMIF: one total for one condition
SUMIF adds the numbers in one range for every row where another range matches a condition. Its form is SUMIF(criteria_range, criterion, sum_range).
=SUMIF(B2:B, "Groceries", D2:D)This formula looks at every cell in B2:B. Where the cell equals "Groceries", it adds the matching cell from D2:D. The two ranges must start on the same row and line up, so row 5 in B is paired with row 5 in D.
A prompt that produces it:
In cell H2, show the total Amount in column D for rows where Category in column B is "Groceries".Notice what the prompt names: the target cell, the sum column, the condition column, and the condition value. A vague prompt such as "total my groceries" leaves Gemini to guess which column holds the amounts.
Common SUMIF errors
- Misaligned ranges.
SUMIF(B2:B, "Groceries", D3:D)shifts the pairing by one row and gives a wrong total without any error message. - Text that almost matches. "Groceries " with a trailing space, or "grocery", does not match "Groceries".
SUMIFignores letter case but not spaces or spelling. - Numbers stored as text. If an Amount is left-aligned and looks like a number, it may be text.
SUMIFskips it. Fix the column type first (see the data layout chapter). - Several conditions.
SUMIFtakes one condition. For two, such as category and person, ask forSUMIFS.
You can also use an operator inside the criterion. ">100" matches amounts above 100. To compare with a cell, join the pieces: ">"&H1.
ARRAYFORMULA: one formula for a whole column
Normally you write a formula in one cell and drag it down. ARRAYFORMULA lets one formula fill the entire column, and new rows are covered automatically.
=ARRAYFORMULA(IF(D2:D="", "", D2:D*1.1))Read it from the inside out:
D2:D*1.1multiplies every Amount by 1.1, which adds 10% tax.IF(D2:D="", "", ...)leaves the result blank where the Amount is empty. Without this check, empty rows would show 0.ARRAYFORMULA(...)tells Sheets to compute the result for every row, not only the first.
You place this formula once, in F2. The results spill down column F. Do not put anything in the cells below F2. If a cell in the spill area is not empty, Sheets shows a #REF! error that says the array result was not expanded because it would overwrite data.
A good prompt names the output cell and the edge case:
In F2, add one formula that fills column F with the Amount from column D plus 10% tax for every row. Leave the result blank where column D is empty. Do not use a separate formula in each row.The last sentence matters. Without it, Gemini may fill each row with its own formula, which works but is harder to maintain.
When ARRAYFORMULA is the wrong choice
- You want to edit single rows differently. A spilled result cannot be changed one cell at a time.
- The sheet is very large and slow. Whole-column array formulas recalculate often.
- Your header is in row 1 and you want a header on the result too. Put a label in
F1yourself, or wrap the header in the formula.
QUERY: filter, group, and sort
QUERY runs a short command written in Google's query language against a range. It is the best choice when you want a summary table with one row per group.
=QUERY(A1:E, "select B, sum(D) where D is not null group by B label sum(D) 'Total'", 1)The function takes three arguments:
- The data range,
A1:E. It includes the header row. - The query string, in quotes.
- The number of header rows,
1.
Inside the query string, columns are named by letter, not by header text. The parts are:
| Part | Meaning |
|---|---|
| `select B, sum(D)` | Return the Category and the total Amount |
| `where D is not null` | Ignore rows with an empty Amount |
| `group by B` | One output row per distinct Category |
| `label sum(D) 'Total'` | Name the second output column "Total" |
The clauses must appear in a fixed order: select, where, group by, pivot, order by, limit, label. Putting label before group by gives a parse error.
Add sorting like this:
=QUERY(A1:E, "select B, sum(D) where D is not null group by B order by sum(D) desc label sum(D) 'Total'", 1)The result is a table in the sheet, with the largest category first. It updates when the source data changes.
Common QUERY errors
- Wrong header count. If the third argument is
0or missing, Sheets may guess. The header row can then be treated as data, or the headers disappear. - Mixed types in a column.
QUERYdecides a column's type from the majority of its values. If Amount holds numbers and some text, the text cells are treated as empty. Your totals will quietly be too low. - Text and dates in the query. Text goes in single quotes inside the query string:
where B = 'Rent'. Dates need thedatekeyword and the formatyyyy-mm-dd:where A >= date '2026-11-01'. - Not enough space for the result. The output spills like
ARRAYFORMULA, so the cells below and to the right must be empty.
Because the query string is plain text, it does not update itself when you insert a column. If you add a column before D, the query still refers to D. Check the query after you change the layout.
Choosing the right one
The diagram shows how to pick a function from the question you are asking.
You can also combine them. A QUERY can feed a chart, and an ARRAYFORMULA can add a helper column that QUERY then groups. If you ask Gemini for a combined solution, ask it to explain each part.
Asking Gemini for summaries
Name the function when you know which one you want, and name the shape of the result when you do not.
- "Use SUMIF to total Amount for Category 'Rent' in H2."
- "Create a table with one row per Category and the total Amount, sorted largest first."
- "Fill column F for all rows, using one formula."
When you describe the shape, Gemini may pick a different function than you expect. That is fine, but read the formula before you keep it. If it chose a function you do not know, ask: "Explain this formula step by step."
Verifying a summary
A summary hides the rows it used, so an error is easy to miss. Use these checks.
- Compare against a manual total. Select the Amount column and read the sum shown at the bottom right of the sheet. The sum of all category totals from
QUERYmust equal it, unless you filtered some rows out. - Use a small test case. Copy five rows to a blank sheet, work out the answer by hand, then run the formula on those five rows.
- Check the edges. Add a row with an empty Amount, a new category, and a category with a trailing space. See whether each one is handled the way you intended.
- Check the range. Click the formula cell and confirm the highlighted ranges cover all your data and start on the correct row.
Here is a small test. Suppose the data is:
| Row | Category | Amount |
|---|---|---|
| 2 | Groceries | 40 |
| 3 | Rent | 500 |
| 4 | Groceries | 25 |
| 5 | Groceries |
SUMIF(B2:B, "Groceries", D2:D) must return 65. The empty Amount in row 5 adds nothing. If you get a different number, check for text values or a shifted range.
Summary of the key points
- Use
SUMIFfor a single total under one condition, andSUMIFSfor several. - Use
ARRAYFORMULAto fill a column from one cell, and keep the spill area empty. - Use
QUERYfor grouped tables, and remember that clauses have a fixed order and columns are named by letter. - Name the function, the columns, the output cell, and the edge cases in your prompt.
- Verify every summary against a manual total.
Exercise
Your Amount column is D, Category is B, and the data starts in row 2. Write a prompt that asks Gemini for a table of the total Amount per Category, largest first, and then name two checks you would run on the result.
Show answer
Prompt: "In cell H1, create a table using QUERY on A1:E with one row per Category in column B and the total Amount from column D, ignoring empty amounts, sorted from largest to smallest total. Label the total column 'Total'." Checks: (1) add up the Total column and compare it with the sum of column D shown at the bottom right of the sheet; (2) copy a few rows to a blank sheet, calculate one category by hand, and compare it with the table. Also acceptable: add a row with a trailing-space category and see how it is grouped.
Exercise
You enter =SUMIF(B2:B, "Rent", D3:D) and the total is lower than expected, with no error. What is the cause, and how do you fix it?
Show answer
The criteria range starts at row 2 and the sum range starts at row 3, so every Category is paired with the Amount from the row below. Change the sum range to D2:D so both ranges start on the same row.