Implementing Dynamic Filtering with Grafana Dashboard Variables
Learn how to build interactive Grafana dashboards using query variables, chained dependencies, and multi-value selections to filter data across different data sources.
21 Apr 2026, 23:12 UTC

Static dashboards fail as soon as your infrastructure scales. Creating a separate dashboard for every server, region, or microservice is unmaintainable. To solve this, Grafana uses Dashboard Variables—interactive inputs like dropdown menus at the top of a dashboard that allow users to filter all panels simultaneously.
The goal of a well-implemented variable strategy is to move from hardcoded values in your queries to dynamic references that update automatically as new data is added to your data sources.
Prerequisites
- Access to a Grafana instance (v7.0+ recommended).
- At least one configured data source (e.g., Prometheus, PostgreSQL, or InfluxDB).
- Edit permissions for the dashboard in question.
Creating a Query Variable
The most powerful variable type is the 'Query' variable, which fetches its options directly from your data source. This ensures that if a new host is spun up, it appears in your dashboard dropdown without manual configuration.
Navigate to Dashboard Settings (the gear icon in the top right) and select Variables. Click Add variable.
Example: Prometheus Host Label Selection
To create a list of all hosts from a Prometheus environment, use the following configuration:
| Field | Value |
|---|---|
| Name | host |
| Type | Query |
| Data source | Prometheus |
| Query | label_values(up, instance) |
| Multi-value | Checked |
| Include All option | Checked |
Injecting Variables into Queries
Once the variable is defined, you must reference it in your panel queries. The syntax varies based on the data source's query language.
Prometheus (PromQL) Syntax
When using multi-value variables in Prometheus, you must use regex matching to handle the multiple strings passed by Grafana:
rate(http_requests_total{instance=~"${host}"}[5m])
Note the =~ operator is critical here because Grafana replaces ${host} with a pipe-separated list of values (e.g., host1|host2).
SQL (PostgreSQL/MySQL) Syntax
For SQL databases, use the IN clause to handle multiple selections. Use the :csv formatter to ensure the variable is passed as a comma-separated list of quoted strings:
SELECT * FROM logs WHERE server_name IN (${host:csv})
Implementing Chained Variables (Dependencies)
You can chain variables so that selecting one filters the options available in another. For example, selecting a 'Region' should only show 'Hosts' located in that region.
- Create the first variable (e.g.,
region) with a query likelabel_values(region). - Create the second variable (e.g.,
host). - In the
hostquery, reference the first variable:label_values(instance, region="${region}").
When the region changes, the host dropdown will automatically refresh to show only relevant instances.
Verification and Diagnostics
To ensure your variables are functioning correctly, use the following checks:
- Query Inspection: Click on a panel, select Inspect > Query. Verify that the
${host}placeholder has been replaced by the actual selected value before being sent to the data source. - Multi-value Validation: Select multiple values in the dropdown. If the panel returns no data or a syntax error, verify that you are using
=~(Prometheus) orIN(SQL) instead of a strict equality operator. - Latency Check: If the dashboard takes several seconds to load upon changing a variable, check the indexing of the columns used in the variable query.
Rollback and Recovery
If a variable configuration breaks the dashboard panels:
- Revert Variable: Return to Dashboard Settings > Variables and delete the problematic variable or change its type back to 'Custom' to hardcode values temporarily.
- Query Reset: Remove the variable reference from the panel query (e.g., replace
${host}with a literal string) to restore panel visibility while debugging the variable query.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.