Sub topic 10
Analytics: Asking Questions in Plain Language
Natural-language query (NLQ) lets a user type a question such as "What was revenue by region last quarter?" and get a chart or number back. This chapter explains how that works inside an analytics platform, where it fails, and how to check its answers.
This chapter assumes you know what a dashboard, a metric, and a dimension are, and that you have a basic idea of what a large language model (LLM) does. It builds on the earlier chapter on the data each platform holds, because NLQ can only answer from data the platform already models.
What the feature automates
Before NLQ, a person who wanted a number had three options: find an existing dashboard, ask an analyst, or write a query in SQL (Structured Query Language, the standard language for reading tables). NLQ replaces the second and third options for routine questions. It translates the question into a structured query, runs that query against the platform's data, and shows the result.
This is a retrieval-style feature, not a prediction. The numbers come from the data through a normal query. The model only translates between your words and the query language. That separation matters for trust, and it is the main difference from a feature that asks an LLM to "recall" a figure.
How a question becomes an answer
Most platforms run the same sequence of steps.
- Interpret. The model reads the question and decides which metric, dimensions, filters, and time range it refers to.
- Ground. The model is given a description of the available data, usually called a semantic layer. This is a catalog that names each metric, defines how it is calculated, and lists the fields you can group or filter by.
- Generate. The model writes a structured query, either SQL or a platform-specific query object.
- Execute. The platform runs the query. Row-level permissions apply here, not in the model.
- Present. The platform picks a chart type and displays the result, often with the generated query or a text description of the interpretation.
Why the semantic layer matters
"Revenue" can mean gross bookings, net revenue after refunds, or recognized revenue. If the platform only exposes raw tables, the model has to guess which columns to sum. If it exposes a semantic layer with one defined net_revenue metric, the model picks from a short list and the definition stays consistent across every dashboard and every question.
A small example shows the idea. The metric definitions below are a plain mapping, and the function turns a metric name and a dimension into SQL. A real platform uses an LLM for the first step, but the second step is deterministic, which is the safer design.
METRICS = {
"net_revenue": "SUM(amount - refunded)",
"orders": "COUNT(*)",
}
DIMENSIONS = {"region": "region", "month": "strftime('%Y-%m', order_date)"}
def build_query(metric: str, dimension: str, table: str = "orders_table") -> str:
if metric not in METRICS or dimension not in DIMENSIONS:
raise ValueError("Unknown metric or dimension")
return (
f"SELECT {DIMENSIONS[dimension]} AS {dimension}, "
f"{METRICS[metric]} AS {metric} "
f"FROM {table} GROUP BY 1 ORDER BY 1"
)
print(build_query("net_revenue", "region"))Because the model can only choose from METRICS and DIMENSIONS, it cannot invent a column or a formula. Unknown names are rejected instead of producing a plausible but wrong query.
Common question types and what they need
| Question | What the system must resolve | Typical data needed |
|---|---|---|
| "How many new contacts did we add last week?" | Metric, relative date | Contact table with creation date |
| "Which campaign had the highest click rate?" | Ranking, a ratio metric | Send and click events |
| "Show open rate by device since June" | Dimension, date range | Event data with device field |
| "Why did signups drop on Tuesday?" | Comparison, possible causes | Several metrics and segments |
The last row is harder than the others. "Why" questions are not a single query. The platform has to run several comparisons and report which segments changed, which overlaps with the automated insights feature covered later in this tutorial. Treat any one-sentence "because" from an NLQ tool as a hypothesis to check.
Where it goes wrong
NLQ failures usually look like correct answers. The chart renders, the number looks reasonable, and the interpretation is wrong. Common causes:
- Ambiguous terms. "Customers" may mean accounts, contacts, or paying users. If the semantic layer has several candidates, the model may pick any of them.
- Relative dates. "Last quarter" depends on the fiscal calendar and the time zone. A platform that assumes calendar quarters will be wrong for a company with a fiscal year starting in April.
- Wrong join or grain. Summing deal value after joining deals to activities counts each deal once per activity. The total is inflated, and nothing in the chart shows it.
- Silent filters. The model adds a filter you did not ask for, such as excluding test accounts, or omits one you assumed, such as excluding refunds.
- Fabricated fields. If the model is not constrained to the catalog, it can reference a column that does not exist. This usually produces an error. A worse outcome is a similarly named column with different meaning.
- Stale or incomplete data. The answer is correct for the data present. If a sync failed yesterday, "yesterday's signups" is low.
Permissions and privacy
The query runs with the permissions of the person asking. A good implementation enforces access in the data layer, so a sales rep who cannot see salary data cannot get it by asking in plain words. Check two things in any platform you evaluate:
- Whether row-level and column-level rules apply to generated queries.
- Whether the question text and the schema are sent to an external model provider, and under what retention terms. Column names and sample values can themselves be sensitive.
Verifying an answer
A short routine catches most errors:
- Read the interpretation the tool shows. Confirm the metric name, filters, and date range.
- Open the generated query if the platform exposes it. Check the grouping and any joins.
- Compare against a known figure, such as a number on an existing certified dashboard for the same period.
- Ask the question two different ways. Different answers signal ambiguity.
- Check the data freshness timestamp.
The following script automates step 3 for a team that keeps a small set of known-good answers. It compares what the tool returned with the trusted value and flags differences above a tolerance.
GOLDEN = {
"net revenue last month": 182_400.0,
"new contacts last week": 1_250.0,
}
def check_answer(question: str, returned: float, tolerance: float = 0.01) -> str:
expected = GOLDEN.get(question)
if expected is None:
return "no reference value"
error = abs(returned - expected) / expected
return "ok" if error <= tolerance else f"mismatch ({error:.1%})"
print(check_answer("net revenue last month", 182_100.0))
print(check_answer("new contacts last week", 1_900.0))
print(check_answer("churn last month", 0.04))A list like this also works as a regression test. Re-run it when the vendor updates its model or when you change the semantic layer.
Decision criteria
Natural-language query suits some situations better than others.
- Good fit: exploratory questions, simple aggregations, one-off lookups for non-analysts, questions over a well-modeled dataset with few metrics.
- Poor fit: figures that go into board reports or financial filings, complex multi-step definitions, and data with unclear ownership. Use certified dashboards or reviewed queries for these.
- Preparation that pays off: write metric definitions, add synonyms (such as "clients" for "accounts"), hide deprecated fields, and document the fiscal calendar.
Pairing NLQ with human review follows the same logic as other automated features. A wrong answer that only informs a private look at the data is low risk. A wrong answer that triggers a budget change is not, so a person should check it first.
Exercise
A marketing manager asks the platform, "What was our revenue by region last quarter?" The chart shows a total 30% higher than the finance team's report. Name three likely causes and one check for each.
Show answer
(1) Metric ambiguity: the tool summed gross bookings instead of net revenue. Check which metric definition the interpretation shows. (2) Date mismatch: the tool used calendar quarters while finance uses fiscal quarters. Check the date range in the generated query. (3) Join inflation: a join to a table with several rows per order counted amounts repeatedly. Check the joins and compare the order count with a plain count of orders. A fourth possibility is missing refund or test-account filters, which you can find by reading the filters in the generated query.
Exercise
Why is it safer for the model to choose from a fixed list of metrics and dimensions, as in the build_query function, than to write free-form SQL against raw tables?
Show answer
A fixed list limits the model to defined metrics and fields, so it cannot invent columns or formulas. Each metric has one agreed calculation, so answers match dashboards. Unknown names fail with an error instead of returning a plausible but wrong number. Free-form SQL allows wrong joins, missing filters, and access to fields the user should not see.