Skip to main content

Dashboards · SQL · How-to

How to build a KPI dashboard from your SQL database, step by step

By DataViz team 4 min read

In short

To build a KPI dashboard from a SQL database, choose three to seven metrics tied to a decision, define each once as a query or dataset metric, show each as a big number with its trend, add date and segment filters, set how fresh the data must be, and share it with the people who act on it.

Before you start: choose KPIs that drive a decision#

A KPI earns its place on a dashboard when someone will act differently because it moved. Start from the decisions, then the questions, then the numbers:

KPIThe question it answersShown as
Revenue this monthAre we on track for the month?Big number, with last month beside it
Gross margin %Are we selling profitably?Big number with a trend line
Average order valueAre customers spending more or less?Line chart by week
Repeat customer rateDo customers come back?Big number with a trend line
Revenue by categoryWhere does the money come from?Bar chart, sorted

Three to seven KPIs is plenty for one dashboard. If you have twenty, you have several dashboards.

Step 1: connect with a read-only login#

Ask for a database user that can only read, and only the schemas you need. It protects the data from a mistaken query and makes the BI tool's access easy to review. Point it at a read replica if your main database is busy.

Step 2: write each KPI as a query#

Write the SQL once and check it against a number you already trust. In PostgreSQL:

-- Revenue and gross margin by month
SELECT date_trunc('month', order_date) AS month,
       SUM(revenue)                                    AS revenue,
       ROUND(100.0 * SUM(revenue - cost) / NULLIF(SUM(revenue), 0), 1) AS gross_margin_pct
FROM orders
GROUP BY 1
ORDER BY 1;
-- Average order value by week
SELECT date_trunc('week', order_date) AS week,
       SUM(revenue) / COUNT(DISTINCT order_id) AS avg_order_value
FROM orders
GROUP BY 1
ORDER BY 1;

NULLIF keeps a month with no revenue from failing on division by zero.

Step 3: define each metric once#

Save the query as a dataset and define the KPIs on it as named metrics (revenue, margin, order count). Every chart then uses the same definition, so "revenue" means the same thing on every dashboard, and a change to the definition reaches all of them.

Step 4: choose the right chart for each KPI#

  • A single number (revenue this month): a big-number card, with the comparison period next to it.
  • A trend (margin by month): a line chart. Start the axis at zero for amounts; for percentages, a tighter range is fine.
  • A ranking (revenue by category): a bar chart, sorted largest first.
  • A share of a whole with two to five parts: a pie or donut. With more parts, use bars.

Step 5: lay it out so it reads in five seconds#

Put the most important number top left, where people look first. Group related KPIs in a row. Give every chart a title that says what it shows ("Revenue by month, last 12 months"), not what it is ("Line chart").

Step 6: add the filters people will ask for#

A date range and one or two segments (region, channel) cover most questions. Apply them to the whole dashboard so every tile answers the same question.

Step 7: decide how fresh the data must be#

A sales dashboard checked each morning doesn't need to query the database every second. Pick the freshness the decision needs, and show on each tile when its data was last updated. Our guide to dashboard data freshness covers the trade-offs.

Step 8: share it with the people who act on it#

Share by role rather than by person, so new team members get it automatically. Send it as a scheduled report to people who won't log in, and set an alert on the KPIs that need attention the moment they cross a line.

Doing it in DataViz#

The whole path takes about fifteen minutes in DataViz: connect a database (PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, Amazon Redshift or ClickHouse) or upload a CSV, Excel, JSON or SQLite file; explore it in SQL Lab; save a dataset with its metrics; build big-number and trend charts; arrange them on a dashboard with filters; and share it, schedule it by email, Slack or WhatsApp, or set an alert. The quickstart walks through it with a sample file.

Questions and answers

How many KPIs should a dashboard have?

Three to seven. Each should be tied to a decision someone makes; a dashboard with twenty KPIs is usually several dashboards for different audiences.

What is the difference between a KPI and a metric?

A metric is any measured number, such as order count. A KPI is a metric chosen because it shows progress towards a goal and someone acts when it moves, such as gross margin against a target.

How often should a KPI dashboard refresh?

As often as the decision needs. Most business dashboards are well served by data a few minutes to an hour old; operational screens need about a minute; finance reports often refresh daily.

Can I build a dashboard from a CSV or Excel file?

Yes. In DataViz you can upload a CSV, Excel, JSON or SQLite file, query it in SQL Lab like a database, and build charts and dashboards on it.

Which chart should I use for a KPI?

A big-number card for a single value with its comparison period, a line chart for a trend over time, and a sorted bar chart for a ranking such as revenue by category.