Sub topic 10
Analytics: asking questions in plain language
This chapter assumes you know what a dashboard, a metric, and a large language model (LLM) are. You should also be comfortable reading a simple SQL query.
What the feature does
Natural-language query lets a user type a question such as "What was revenue by region last quarter?" and get a table or chart back. The feature automates the step of translating a business question into a database query. It does not decide which question is worth asking.
This is mostly a generative feature with a retrieval step. An LLM generates a query, and the platform retrieves the data by running that query.
How it works
Most platforms follow the same pipeline.
- The platform collects the question and the user's context, such as their role and the dataset they are viewing.
- It retrieves a description of the data model: table names, column names, metric definitions, and sometimes sample values.
- The LLM generates a query, usually SQL or an internal query format.
- The platform validates the query and runs it with the user's permissions.
- The result is shown as a table or chart, often with the generated query visible.
Step 2 matters most. The model can only use names and definitions it has been shown. A platform with a well-documented data model, where "revenue" is defined once, answers far better than one with columns named col_17. This is why data foundations come first in this tutorial.
A small example
The sketch below shows the validation step. It accepts only a single read-only SELECT statement.
import re
def is_safe_query(sql: str) -> bool:
"""Allow only one read-only SELECT statement."""
statement = sql.strip().rstrip(";")
if ";" in statement:
return False
if not re.match(r"(?i)^select\b", statement):
return False
forbidden = r"(?i)\b(insert|update|delete|drop|alter|grant)\b"
return re.search(forbidden, statement) is None
generated = "SELECT region, SUM(revenue) FROM sales GROUP BY region"
print(is_safe_query(generated))
print(is_safe_query("DROP TABLE sales"))A real platform goes further. It limits the query to tables the user may see and sets time and row limits. Pattern checks like this one are a minimum, not a full defense.
Common errors
- Ambiguous terms. "Customers" may mean accounts, contacts, or paying users. The model picks one, and the answer looks confident either way.
- Wrong time window. "Last quarter" may be calendar or fiscal. "Recent" has no fixed meaning.
- Wrong join or filter. The query runs without error but double-counts rows or leaves out a segment.
- Invented columns. The model refers to a field that does not exist. Validation usually catches this, and the user sees an error.
- Missing permissions. If the query runs with broader access than the user has, it can expose restricted data.
Habits that reduce risk
- Read the generated query or the plain-language restatement of it before trusting the number.
- Compare one answer with a known figure, such as last month's report.
- Use governed metric definitions so "revenue" means one thing.
- Keep the user's permissions attached to every query.
Where this fits
This feature relies on the same data foundations as anomaly detection, and it sits beside automated insights. Anomaly detection pushes alerts to you. Natural-language query lets you pull answers on demand.
Exercise
A user asks "How many customers did we lose last month?" The platform returns 48, but the finance report says 31. Name two likely causes in how the question was translated.
Show answer
Possible causes include different definitions of "customers" (for example, contacts versus paying accounts), different definitions of "lost" (cancelled versus inactive), or a different "last month" window (calendar versus billing period). Reading the generated query shows which one the model chose.