BlogRAG
How an assistant should answer questions that depend on tables and spreadsheets
Sort the questions first. A question about one value in one row can be answered by retrieving that row as text, provided the row carries its column names. A question that adds, filters, counts or compares across rows needs a query against the data, because search returns a handful of chunks and the answer depends on all of them. Show the query and the data date with every computed answer.
Three kinds of table questions
- Look-up: one value with its context, such as the notice period for one supplier or the price of one part
- Computation: totals, averages, rankings, counts or filters across many rows
- Explanation: what a column means, why a figure changed or which rule produced a value, which usually lives in a document next to the table
Most assistants receive all three mixed together. The design question is how to recognise each kind and send it down a path that can answer it.
Why top-k retrieval cannot add up a column
Retrieval selects the few chunks most similar to the question. A total over three thousand order lines needs all of them, and no similarity ranking will return exactly the rows a filter would. The model then sums whatever it was given and presents the result with confidence. Missing rows look like smaller numbers, not like an error.
Pasting the whole sheet into the prompt avoids the selection problem for small tables, but arithmetic over many numbers in text is still unreliable. A database or a dataframe does that job exactly.
Making rows retrievable for look-ups
For look-ups, index each row as its own search document. Azure AI Search, for example, offers a delimitedText parsing mode that turns each line of a CSV file into a separate document; without it, the whole file becomes one document. Its SQL indexer maps a single table or view, row by row, and keeps decimal and money values as strings because converting them to floating point would lose precision.
Write each row as text that a person could read alone: column names next to values, units and currency spelled out, the sheet or table name and the date of the export. A row that says only 48213, 3, 0.15, PL is unfindable by meaning and ambiguous to the model.
A query path for calculations
For computation, let the model produce a query against a known schema, run it in the database, and answer from the result. LlamaIndex documents this text-to-SQL pattern and advises naming the tables to query, because loading every schema can overflow the context; for many tables it retrieves the relevant schema first. The same guide warns that executing arbitrary SQL is risky and suggests restricted roles, read-only databases and sandboxing.
In practice we add more limits: a database role that sees only approved views, row-level filters for the user asking, timeouts and row caps. For the questions people ask every week, predefined parameterised queries exposed as tools are easier to test than free-form SQL. Either way, the answer should show the query or report used, the result, the row count and the data timestamp, so a reader can check it.
Routing one export, question by question
The example below is invented. One sales export feeds both paths: rows are indexed for look-ups and loaded into a table for queries.
Source: monthly sales export, one row per order line (hypothetical)
Question Route Why
Discount on line 3 of order 48213? retrieval one line row, found by
order ID + line number
Which region had the highest revenue in Q2? query aggregates every Q2 row
How many Polish orders totalled over 10,000 EUR? query sum line amounts per order,
then count distinct orders
above the threshold
Why is the Q2 figure lower than in the board pack? explain needs the definitions note,
then a query to reconcile
Answer contract for "query":
show the query or named report used, the result,
the data timestamp and the row count it coveredTest the router as carefully as either path. A computation question sent to retrieval returns a plausible wrong number, which is harder to notice than an error message.
Spreadsheets that behave like documents
Many business spreadsheets are not tables. They contain merged headers, several tables on one sheet, notes typed into cells and figures that exist only after formulas are calculated. Several copies circulate by email. Before connecting such a file, decide which copy is authoritative and whether the data should first move into a proper table with defined columns. The reconciliation work is often worth more than the assistant.
Questions
Can the model do the arithmetic itself?
For a few numbers it often gets it right, but you cannot tell when it does not. Exact results should come from a database, a dataframe or a calculation tool, with the model choosing the operation and explaining the result.
Is text-to-SQL safe to offer to everyone in the company?
Only behind the same access rules people already have. Run queries under a role limited to approved, read-only views, apply row-level filters for the user, and log every query. If those controls are not in place, start with predefined queries instead.
What about tables inside PDFs and Word files?
Extract them as tables rather than running text. Layout analysis can recover rows and headers, and long tables split across pages need the header row repeated in each chunk. If the figures are also used in calculations, load them into a structured store as well and keep the document as the evidence for look-ups.
Related services
RAG development
Assistants that answer from your internal documents, with permissions, citations and tests on your own questions.
AI engineering and architecture
Assistants, voice interfaces and agents in the products you sell, with the serving and retrieval underneath.
MCP server development
MCP servers that connect approved internal APIs to AI assistants, with narrow permissions, approval for changes and tests.
More from the blog
Chunking business documents
How to split contracts, policies and long procedures so that an answer stays with its exceptions and definitions, and how to choose a strategy by experiment on your own questions.
RAG document permissions
How access rules travel from source systems into a retrieval index, where they get lost, and which negative tests show that a user cannot retrieve what they may not open.
Retrieval or generation?
A diagnostic procedure for finding out whether a wrong RAG answer started in ingestion, search, reranking or the model, using labelled evidence and per-question traces.
Talk to an engineer
Bring one piece of work you would like to improve. The call takes 30 minutes and needs no access to your systems or your data.
Booking runs on Google Calendar.
Prefer to write first? Leave your email and a few lines, and we reply with times.
Look for the draft in your email program
Send it from there, and an engineer replies in person with times for a 30-minute call. If no draft has opened, write to us at miki@cloudsail.com.