Sub topic 6
Asking for Formulas
This chapter covers how to ask Gemini in the Sheets side panel for a formula, and how to phrase the request so the result fits your data. It assumes you can already read a simple formula such as =SUM(D2:D10) or =IF(D2>0, "yes", "no"). It also assumes you have the side panel open and a sheet with headers and one record per row.
What Gemini needs to write a formula
Gemini can write a formula only as well as your prompt describes it. A formula depends on four facts, and each one is a place where a vague prompt goes wrong.
- Where the result goes. Name the column or cell, for example "in column F" or "in cell H1".
- Where the inputs are. Name the source column by letter or by header, for example "the Amount from column D".
- The calculation. Say what to compute, with the actual numbers, such as "plus 10% tax".
- The edge cases. Say what should happen with empty cells, text where a number is expected, or no matching rows.
Compare two prompts for the same task.
| Prompt | Problem or strength |
|---|---|
| "Add tax to the amounts." | Gemini must guess the tax rate, the column, the output location, and the blank-row behavior. |
| "In column F, add a formula that shows the Amount from column D plus 10% tax, and leave the cell blank when column D is empty." | States target, source, calculation, and edge case. |
The second prompt is the one from this tutorial's examples. Use it as a template.
In column F, add a formula that shows the Amount from column D plus 10% tax, and leave the cell blank when column D is empty.A correct answer to that prompt is a formula like the one below, placed in F2.
=IF(D2="", "", D2*1.1)Read it from the outside in. The IF tests whether D2 is empty. If so, it returns an empty string, so the cell looks blank. Otherwise it multiplies D2 by 1.1, which is the amount plus 10%.
Where the formula lands
Gemini usually shows its suggestion in the side panel with an option to insert it. It does not always write into your sheet on its own. Check three things before you accept.
- The target cell. Confirm the formula goes where you asked, and that it will not overwrite existing values.
- The reference style. A formula written for row 2 uses relative references such as
D2. When you fill it down, the reference shifts toD3,D4, and so on. That is usually what you want for a per-row calculation. - The range extent. A formula that reads
D2:D50ignores rows 51 and beyond. If your data grows, ask for open-ended ranges such asD2:D.
If a suggestion puts the formula in the wrong place, you can move it by hand or send a follow-up prompt. Follow-ups are covered in a later chapter.
Per-row formulas and whole-column formulas
There are two common shapes for a formula request, and you should say which one you want.
A per-row formula sits in one cell and is copied down. Each copy handles its own row. The IF(D2="", ...) example above is per-row. It is simple to read and to edit, but every new row needs the formula copied into it.
A whole-column formula sits in one cell and fills the cells below it automatically. In Sheets this is done with ARRAYFORMULA.
=ARRAYFORMULA(IF(D2:D="", "", D2:D*1.1))Put this in F2 and Sheets computes F2 down through the last row of column D. New rows are covered with no copying. The trade-off is that the cells below F2 now belong to the array. If you type a value into F3, Sheets shows a #REF! error in F2, because the array can no longer expand into an occupied cell. Clear F3 and the error goes away.
To get this shape, say so in the prompt:
In F2, write one formula that fills the whole column F with the Amount from column D times 1.1, and leaves rows blank when column D is empty.Phrases such as "one formula that fills the whole column" push Gemini toward ARRAYFORMULA. Phrases such as "a formula I can copy down" push it toward the per-row form.
Summary formulas
Sometimes you want a single number or a small table, not a value on every row. Name the grouping and the measure.
To total one category, a prompt such as "Show the total Amount for Groceries" should produce something like this:
=SUMIF(B2:B, "Groceries", D2:D)SUMIF takes three arguments: the range to test (B2:B), the condition ("Groceries"), and the range to add up (D2:D). The two ranges must line up row for row.
To total every category at once, ask for a grouped summary:
=QUERY(A1:E, "select B, sum(D) where D is not null group by B label sum(D) 'Total'", 1)The final 1 tells QUERY that the first row of A1:E is a header. Without it, Sheets may treat the header text as data and the sum can fail or misbehave.
The next chapters take these formulas apart in more detail. For now, notice how each prompt named the measure (Amount), the grouping (Category), and the output.
Naming columns: letters or headers
You can refer to a column by its letter ("column D") or by its header ("the Amount column"). Both work, and each has a failure mode.
- Letters are exact. They fail if you insert a column later, because the letters in your prompt no longer match the sheet. The formula Gemini already wrote adjusts itself when columns move, but a new prompt written from memory will not.
- Headers read naturally and survive layout changes between prompts. They fail when two columns have similar names, such as "Amount" and "Amount (USD)", or when a header cell is empty or merged.
A safe habit is to use both: "the Amount in column D". If Gemini picks a different column than you meant, this is the first thing to check.
Common errors and what causes them
| Symptom | Likely cause | What to do |
|---|---|---|
| `#REF!` in an `ARRAYFORMULA` cell | Another cell below it contains a value, so the array cannot expand. | Clear the cells under the formula. |
| `#VALUE!` | The formula does arithmetic on text, for example an amount stored as `"$12.50"` text. | Convert the column to real numbers, then re-run. |
| `#N/A` or a `QUERY` parse error | The query string references a column the range does not contain, or uses the wrong header count. | Check that column letters in the query are within the range, and check the third argument. |
| The total is 0 | `SUMIF` found no matching text, often because of extra spaces or different spelling in the Category cells. | Compare the criterion to the cell contents character by character. |
| Result ignores new rows | The range was closed, such as `D2:D50`. | Ask for an open-ended range such as `D2:D`. |
| Wrong formula syntax for your locale | Some locales use semicolons, not commas, between arguments. | Edit separators to match your spreadsheet settings. |
Many of these come back to data layout. Mixed types in one column (numbers in some rows, text in others) cause more wrong results than any prompt wording does.
Asking for formulas you do not fully understand
You do not have to be able to write a formula to ask for one, but you must be able to read it before you use it. After Gemini proposes a formula, ask for an explanation in the same panel:
Explain what this formula does, one argument at a time, and tell me what it returns when column D is empty.Then compare the explanation to your intent. If the explanation describes behavior that you did not ask for, reject the formula and rewrite the prompt. The next chapter treats this reading step in detail.
A worked sequence
Suppose your expense sheet has the headers Date, Category, Description, Amount, and Paid By in A1:E1, with data below.
- Ask for the per-row tax column, using the template prompt above. Insert the formula in F2 and fill it down.
- Test with a row where Amount is 100. The result in F should be 110. Then clear D for one row and confirm F is blank for that row.
- Ask for a grouped summary: "In H1, show the total Amount for each Category using the data in A1:E." Expect a
QUERYlike the one above. - Check one category by hand. Filter the data to Groceries, add the amounts on a calculator, and compare them with the
QUERYresult.
Steps 2 and 4 are the verification habit this tutorial builds on. A formula that looks right can still be wrong, and a small test with a known answer is the cheapest way to find out.
Summary of a good formula prompt
A reliable formula prompt names:
- the output location (column F, cell H1),
- the input columns (column D, or the Amount column),
- the calculation with real numbers (plus 10% tax),
- the shape (copy-down or one formula for the whole column),
- the edge case (blank when D is empty).
Exercise
Your sheet has Category in column B and Amount in column D, with data from row 2 down. Write a prompt that asks Gemini for one formula in F2 that fills column F with the Amount plus 5% service charge, leaves rows blank where Amount is empty, and automatically covers rows added later. Then write the formula you expect it to return.
Show answer
Prompt: "In F2, write one formula that fills the whole column F with the Amount from column D plus a 5% service charge, and leaves rows blank when column D is empty." Expected formula: =ARRAYFORMULA(IF(D2:D="", "", D2:D*1.05)). The open-ended range D2:D covers new rows, and the IF returns an empty string for blank rows.
Exercise
A teammate typed a note into F5, below your ARRAYFORMULA in F2. F2 now shows #REF!. Why, and how do you fix it?
Show answer
The array formula needs to expand down column F, but F5 is occupied, so it cannot fill the cells it needs. Clear F5 (or move the note elsewhere), and F2 will compute again.
Exercise
Gemini returns =SUMIF(B2:B50, "Groceries", D2:D50) for a sheet that gains new rows every week. What is the risk, and what would you ask for instead?
Show answer
The closed ranges stop at row 50, so rows added after that are not counted. Ask for open-ended ranges, which gives =SUMIF(B2:B, "Groceries", D2:D).