-
Notifications
You must be signed in to change notification settings - Fork 1
Mathematical Expression
This guide explains how to build dependent calculated graphs using Grafana Math expressions together with CompareQueries time-shifted series.
It addresses Issue #19 — Add compatibility with expressions and related scenarios in Issue #12 (diff / ratio between current and shifted data).
Version requirement: CompareQueries v2.1.0+ (backend plugin enabled).
Grafana Math expressions run on the server. Every query referenced by an expression ($A, $B, …) must execute in backend mode.
Before v2.1.0, CompareQueries was frontend-only and could not join expression pipelines. Since v2.1.0, the plugin proxies shifted queries through Grafana /api/ds/query. That proxy needs a Service Account token configured on the CompareQueries datasource.
Flow:
Service Account Token → CompareQueries datasource (Basic Auth) → Backend plugin (/api/ds/query proxy) → -- Mixed -- panel ├─ Query A: target datasource (base series) ├─ Query B: CompareQueries (shifted series) └─ Query C: Math Expression ($A - $B) → Calculated graph
- Open Administration → Users and access → Service accounts.
- Create a service account (or reuse one) with permission to query datasources.
- Add a Service account token with at least Viewer-level data access.
- Copy the token for Step 2.

- Go to Connections → Data sources → CompareQueries.
- Set Authentication to Basic authentication.
- Paste the Service Account token from Step 1.
- Set Grafana URL only if auto-detection is wrong (reverse proxy, sub-path, etc.).
- Click Save & test.

For plain dashboard viewing,
No Authenticationmay work in some setups. For Math expressions and Alerting, use Basic authentication.
- Edit a panel and set datasource to
-- Mixed --. - Add a target datasource query (refId
A) for the current time window — your base series. - Add a CompareQueries row (refId
B):- Pick the same Target Datasource.
- Build the same query in the embedded editor.
- Add Time-shift rows:
1d,1w, etc. - Enable Process TimeShift when you need diff/ratio on the same time axis (recommended).
- Set Alias (
suffix/prefix/absolute) so series names stay distinct.

Example layout
| refId | Datasource | Role |
|---|---|---|
A |
Prometheus | CPU usage — today |
B |
CompareQueries | Same query, shift 1d, alias 1d
|
C |
Expression | Math: $A - $B
|

- In the same Mixed panel, Add query → Expression.
- Choose Math (or Reduce / other types as needed).
- Reference upstream refIds, for example:
- Difference:
$A - $B - Ratio:
$A / $B - Percent change:
100 * ($A - $B) / $B
- Difference:
- Run the query.

SQL datasources may return wide tables (one column per metric). Math expressions work best when each series is a numeric field with a stable name.
Tips (v2.1.1+):
- Prefer Time series format over Table when the datasource supports it.
- Enable Process TimeShift before subtracting or dividing.
- Use distinct Alias per time-shift row.
- If the expression picker misses a field, check Query inspector for separate numeric fields per shift.
For each Time-shift row, the backend:
- Shifts the query time range (
1d= one day back). - Proxies the query via Grafana
/api/ds/query. - Filters points to the intended window.
- Applies alias rules to field names.
- Adds a
timeshiftlabel on each field. - Optionally realigns timestamps when Process TimeShift is on.
| Symptom | Likely cause | Fix |
|---|---|---|
| Expression disabled / "frontend only" | Not on backend mode | Upgrade to v2.1.0+; enable Basic Auth |
401 in backend logs |
Missing or bad token | Set Service Account token |
Expression cannot find $B
|
Wrong refId or empty shift | Check refId, shift format (1d, 1w), target returns data |
| Diff flat or misaligned | Timestamps not aligned | Enable Process TimeShift |
| SQL diff wrong columns | Wide table format | Use time-series format; distinct aliases |
| UI works, Alert fails | Alert always uses backend | Configure Basic Auth (see README Alerting section) |