Skip to content
Lesson 2 of 2AI for Developers

Building with AI · Lesson 2 of 2

Prompting for data analysis

Get accurate formulas, pandas code, and SQL by describing your data clearly, sanity-checking results, and protecting private information.

12 min readIntermediate
Sign in to bookmark

You're learning as a guest — everything is open. Sign in to save progress, earn XP and pick up where you left off.

Describe the data before you ask the question

"How do I calculate monthly sales?" has a dozen right answers depending on what your data looks like. An AI assistant can write excellent spreadsheet formulas, pandas code, and SQL — but only for data it understands. Your first job is to describe the structure.

A good data description includes:

  • Columns and types — names, and whether each is a date, number, text, or category.
  • A few sample rows — real-looking but safe (more on privacy below).
  • Size and quirks — roughly how many rows, blank cells, duplicate IDs, mixed date formats.
  • The tool — Excel or Google Sheets? pandas or SQL? Which SQL database?
Before

How do I get total sales per month?

After

I have a Google Sheets tab called "Orders" with about 5,000 rows: - Column A: order_date (dates, formatted like 2025-03-14) - Column B: customer_id (text) - Column C: amount (number, in USD; some cells are blank) - Column D: status ("paid", "refunded", or "pending") I want a summary table showing total paid sales per month for 2025. Blank amounts should be ignored. Give me the formula(s), tell me which cell to put them in, and explain how they work.

Spreadsheet formulas

Excel and Google Sheets share many functions but not all of them, and some functions only exist in newer versions. Always say which one you use. If your locale uses semicolons instead of commas between formula arguments, mention that too.

Prompt
I'm using [Excel for Microsoft 365 / Google Sheets].
My data is in [sheet name], rows [2–N], with these columns:
[list columns with letters, names, and data types]

I want to [goal, in plain words].
Give me the formula, say exactly which cell it goes in, and explain each part. If there's a simpler alternative using a pivot table, mention it briefly.

pandas and SQL

For code, the column names and types matter even more because a typo means an error. Paste the schema or the output of a quick inspection, like df.dtypes and df.head() in pandas, or the CREATE TABLE statement for SQL.

SQL dialects differ — date functions in PostgreSQL, MySQL, SQLite, and SQL Server aren't the same — so name yours.

Prompt
I'm using [PostgreSQL / MySQL / SQLite / pandas 2.x in Python].
Here is my table structure:
[paste CREATE TABLE or df.dtypes output]

Here are 5 sample rows (anonymized):
[paste rows]

Write a query/code that [goal]. Handle [known quirk, e.g. NULL amounts, duplicate rows].
Add comments explaining each step, and tell me one quick check I can run to confirm the result is correct.

A typical answer might look like this:

python
import pandas as pd

df = pd.read_csv("orders.csv", parse_dates=["order_date"])
paid = df[(df["status"] == "paid") & df["amount"].notna()]
monthly = paid.groupby(paid["order_date"].dt.to_period("M"))["amount"].sum()
print(monthly)

Sanity-check the numbers

Language models are not calculators. If you paste a big table and ask "what's the average?", the model may produce a number that looks right but is off. Two safer patterns:

  1. Ask for the method, not the answer. Have the AI write a formula or code, then run it yourself on the real data. The computer does the arithmetic.
  2. Use AI tools that execute code. Some assistants can run code on uploaded files. That's far more reliable than mental math — but still check the code it ran.

Then sanity-check the result like an analyst would:

  • Row counts — does the filtered data have roughly as many rows as you expect?
  • Totals — does the sum of the monthly figures match the overall total?
  • Spot checks — calculate one month by hand or with a simple filter.
  • Plausibility — is any value impossibly large, negative, or zero?

Tip: Ask the AI to help you check its own work: "What are three ways this calculation could be silently wrong with my data?" It often points out issues like duplicates, time zones, or blanks being treated as zero.

Protect private data

Watch out: Don't paste personal data — names, emails, phone numbers, addresses, account numbers, health or salary information — into an AI tool unless you're certain it's allowed by your organization and the tool's data policy.

You rarely need real data to get good help. Instead:

  • Share column names and types plus a few made-up rows with the same shape.
  • Replace identifiers with placeholders (customer_001, customer_002).
  • Ask the AI to generate synthetic sample data for testing your formula or code.
  • Run the final code on the real data locally, where it never leaves your machine.

The pattern to remember

Describe the structure, name the tool, state the goal and the quirks, and ask for a method you can run. Then verify the output before anyone makes a decision based on it.

Common mistakes

  • Mistake: Pasting a large table and asking the AI to calculate totals directly.

    Fix: Ask for a formula or code, then run it on the real data so a computer does the math.

  • Mistake: Not saying which SQL dialect or spreadsheet app you use.

    Fix: Name the exact tool so you get functions and syntax that actually work there.

  • Mistake: Sharing a spreadsheet full of customer names and emails to get help with a formula.

    Fix: Share only the column structure and a few made-up rows with the same shape.

Practice exercise

Describe your data for a reliable formula

You have a spreadsheet of household expenses with columns for date, category, description, and amount. You want to know how much you spent on groceries each month this year.

Rewrite the starter prompt so the AI can give you a formula that works the first time. Include the tool, column layout, data types, a couple of made-up sample rows, quirks, and how you'll check the result.

Your draft stays in this browser tab — it isn't sent anywhere.Try your draft in the Playground

Hints

Self-check

Tick each item your prompt covers.

0 of 5 checked

You can do the whole exercise as a guest.Sign in to save progress

Knowledge check

Test your understanding

Question 1 of 4

Why is it better to ask for a formula or code than for the AI to calculate the answer from pasted data?

Finished reading?

Guests can read every lesson.Sign in to save progress