How to Use Grafana Dashboard Variables
In this tutorial, you'll learn about How to Use Grafana Dashboard Variables. We cover key concepts, practical examples, and best practices.
The Problem
Your Grafana dashboard shows data for all servers at once, forcing viewers to manually edit queries every time they want to filter by a specific environment, region, or host. Without dashboard variables, each panel has hard-coded filter values and users cannot interactively drill down into the data.
Quick Fix
1. Define a variable for environment selection
In Dashboard Settings (gear icon at the top):
Variables → Add variable → Type: Query
Name: environment
Label: Environment
Data source: Prometheus
Query: label_values(up{job="node"}, env)
Refresh: On Dashboard Load
Click "Update" — a dropdown selector appears at the top of the dashboard.
2. Use the variable in a panel query
up{env="$environment", job="node"}
Each panel now dynamically filters by the selected environment.
3. Chain variables for dependent filtering
Create a second variable that depends on the first:
Variables → Add variable
Name: instance
Label: Instance
Type: Query
Query: label_values(up{env="$environment", job="node"}, instance)
Refresh: On time range change
When the user changes the environment, the instance dropdown refreshes to show only instances in that environment.
4. Add an "All" option for multi-value selection
In the variable settings:
Include All option: ✅
Custom all value: .*
Use the regex matcher in your query:
up{env=~"$environment", instance=~"$instance"}
5. Use variables in dashboard links
Create a link to another dashboard, passing the current selection:
{
"title": "Detailed Server View",
"type": "dashboards",
"url": "/d/abc123/other-dashboard",
"params": "var-environment=$environment&var-instance=$instance"
}
6. Repeat panels for each selected value
In any panel:
Panel title → Repeat options
Repeat by variable: instance
Grafana creates one panel per selected instance value, perfect for per-server CPU or memory panels.
7. Use variables in alerting queries
avg by (instance) (up{env="$environment"})
8. Use a text box variable for manual input
Variables → Add variable → Type: Text box
Name: custom_tag
Label: Custom Tag
Default: all
Use it in queries:
up{env="$environment", custom_tag=~"$custom_tag"}
9. Test variables with the query inspector
Open any panel → Inspect → Query to see the rendered query with the variable values substituted.
10. Use template variables in dashboard links
/grafana/d/abc123/my-dashboard?var-environment=production&var-service=api
These URL parameters override the default variable values when someone clicks the link.
Prevention
- Use
label_values()for query-type variables — they auto-populate from your data sources - Set variable refresh to "On time range change" for chained variables so they update when the time range shifts
- Add regex filters to exclude unwanted values (e.g., filter out unscheduled nodes with regex)
- Use "Include All option" with custom value
.*for flexible multi-select filtering - Document variable names with a consistent prefix (
var-) in dashboard links to make them easy to reference in other dashboards
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro