Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Queries and Transformations

Grafana Expressions, Mixed Data Sources and Reusing Query Results

Combine queries with server-side expressions (Math, Reduce, Resample, Threshold), use the Mixed data source and reuse results with the Dashboard data source.

Sometimes one query is not enough. You may want a ratio of two queries, a single number from a time series, or a chart that mixes a database and Prometheus. Grafana has three tools for that: server-side expressions, the Mixed data source, and the Dashboard data source. This lesson shows each of them and the differences from client-side transformations.

Grafana does not run here, so the steps are described in words; the TryIt blocks are Python stand-ins for the math the expressions perform.

Server-side expressions

An expression is an extra row in the query editor, added with + Expression. It does not call a data source. It takes the results of other queries (referenced by their letters) and computes something on the Grafana server. Expressions matter for two reasons: they work for alerting (alert rules are built from queries plus expressions), and they let you calculate on data from different sources.

The expression types are:

  • Math: arithmetic on numbers and series, such as $A / $B, $A * 100, or abs($A - $B). It supports functions like abs, ceil, floor, round, sqrt, log and comparison and logical operators that return 0 or 1.
  • Reduce: collapse a series into one number with a function (Last, Mean, Min, Max, Sum, Count). Options decide how to treat missing values (drop them, or replace with zero or a value you choose).
  • Resample: change the interval of a series (for example to 1m) and fill gaps by a method such as last value, so two series line up in time before a Math expression.
  • Threshold: compare a number or series to a limit (Is above, Is below, Is within range, Is outside range); the output is 1 (true) or 0 (false).
  • Classic condition: the older all-in-one condition for alert rules, kept for compatibility. New rules use Reduce plus Threshold.
  • SQL: newer versions can also run a SQL query over the results of earlier queries; the feature set depends on your Grafana version, so check the docs for yours.

Each expression has its own letter, so you can chain them: A and B are queries, C is Math $A / $B, D is Reduce of $C, E is Threshold of $D.

Python Stand-in: Math, Reduce and Threshold chain

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here both the last value (12%) and the mean (about 5.9%) are above the 5% limit, but with a slightly calmer series the mean could stay below while the last value fires, so the reduction you choose changes whether the alert fires. That choice is explained in alert rules.

Expressions run on the server and are not available for every panel type in every version, but they work in time series, stat, table and others. For a purely visual reshape of one result, a transformation is simpler; for anything that an alert must evaluate, use expressions, because transformations never run in the alert engine.

The Mixed data source

Choose -- Mixed -- in the data source picker. Now every query row has its own data source dropdown. Query A can read Prometheus, query B can read PostgreSQL, query C can read a CSV or JSON API. Everything is drawn in the same panel. The cost: each panel sends several requests, and you must make sure time ranges and units agree.

Good uses:

  • Overlay a business metric (orders per minute from SQL) on a system metric (CPU from Prometheus) to look for correlation.
  • Compare the same metric across two clusters that have separate Prometheus instances.
  • Draw a threshold line fetched from a configuration table.

Combine Mixed with expressions: expressions work on the results of any queries, so $A / $B can divide a Prometheus series by a SQL series, if both have compatible time stamps (use Resample to align them).

The Dashboard data source

Another way to avoid duplicate work is the built-in -- Dashboard -- data source. It lets a panel reuse the result of another panel's query on the same dashboard, instead of querying the data source again. In the query editor, choose -- Dashboard --, then pick the source panel. Three benefits:

  • One expensive query feeds many panels (a table, a stat and a graph) and loads once.
  • Different panels can apply their own transformations to the same raw data.
  • The panels stay in sync because they share a single result.

You can choose whether to use the source panel's data with or without its transformations applied. If the source panel is deleted, the dependent panels show an error.

Choosing the right tool

flowchart TD
    Q{"What do you need?"}
    Q -->|"Reshape one result"| T["Transformation (client)"]
    Q -->|"Compute across queries<br/>or for alerting"| E["Expression (server)"]
    Q -->|"Queries from two sources<br/>in one panel"| M["Mixed data source"]
    Q -->|"Same query in many panels"| D["Dashboard data source"]

Four tools, four jobs.

A handy rule: if you would write the same logic in an alert rule, build it with expressions. If it only affects how a table or graph looks, use a transformation.

Worked example: error budget burn ratio

Suppose a dashboard shows how fast you are using up an error budget of 0.1% errors. The panel has two PromQL queries:

  • A: sum(rate(http_requests_total{status=~"5.."}[$__rate_interval]))
  • B: sum(rate(http_requests_total[$__rate_interval]))

Then a Math expression C: ($A / $B) / 0.001. A value of 1 means errors arrive exactly at the budgeted pace; 14 means burning fourteen times too fast. Hide queries A and B with the eye icon so only C is drawn, and add a threshold at 1 in the panel options.

Python Stand-in: burn rate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where are server-side expressions evaluated?

    Expressions run in the Grafana backend, which is why alert rules can use them.
  2. 2. Which expression type turns a series into a single number such as the last value?

    Reduce collapses a series with a function such as Last, Mean, Min, Max or Sum.
  3. 3. What does the Dashboard data source do?

    It lets panels share one query result on the same dashboard.
  4. 4. Which tool must you use for logic an alert rule has to evaluate?

    Transformations are client-side and do not run in the alerting engine; expressions do.

Summary

  • Expressions (Math, Reduce, Resample, Threshold, Classic condition, and in newer versions SQL) compute on the server from other queries.
  • Use Reduce plus Threshold for alert conditions; use Resample to align series before Math.
  • The Mixed data source lets one panel combine queries from different data sources.
  • The Dashboard data source shares one query result between panels.
  • Transformations are for display shaping; expressions are for calculations that alerts must repeat.

Next: Dashboard Variables