docs/sources/visualizations/dashboards/variables/_index.md
A variable is a placeholder for a value that you can use in dashboard queries, panel titles, links, and other dashboard elements. When you change the value of a variable, Grafana updates every dashboard element that uses that variable.
Variables are displayed as drop-down lists (or in some cases text fields) at the top of the dashboard. These controls let viewers change what the dashboard displays without editing the dashboard.
For example, if you need to monitor several servers, you could make a dashboard for each server. Or you could create one dashboard and use panels with variables like this one, where you can change the server using the variable selector:
{{< figure src="/media/docs/grafana/dashboards/screenshot-selected-variables-v12.png" max-width="750px" alt="Variable drop-down open and two values selected" >}}
Variables help you create interactive dashboards that adapt to different contexts. Instead of hard-coding values such as server, application, region, or sensor names, you can define a variable once and reuse it throughout the dashboard.
Using variables also allows you to single-source dashboards. If you have multiple identical data sources or servers, you can make one dashboard and use variables to change what you are viewing. This reduces dashboard duplication and maintenance.
You can use variables in:
To work with variables, you typically do the following:
To see variable settings, click Edit in the top-right corner of the dashboard, click the Dashboard options icon, click Settings, and then click Variables.
{{< docs/play title="Templating - Interactive dashboard" url="https://play.grafana.org/goto/B9Xog68Hg?orgId=1" >}}
Grafana supports two broad kinds of variables:
Choose a variable type based on how you want Grafana to get the value and how users interact with it.
| Variable type | Use when |
|---|---|
| Query | The list of values comes from a data source query, such as server names, metric names, label values, or data centers. |
| Custom | You want to define a fixed list of values manually. |
| Text box | Viewers need to enter a free-form value. |
| Constant | A dashboard needs a reusable value that viewers don't change. |
| Data source | Viewers need to switch a dashboard or query between data source instances. |
| Interval | Viewers need to change the time grouping or aggregation interval in queries. |
| Filters | Viewers need dashboard-wide key/value filters for supported data sources. |
| Switch | Viewers need to toggle between two configured values. |
For the complete list of variable settings and type-specific steps, refer to Add variables.
A template is any query that contains a variable.
Queries with text that starts with $ are templates.
For example, if you administer a dashboard that monitors several servers, it can have panels that use a template query like this one:
{{< admonition type="note">}} Grafana documentation and the application typically refer to a template query as a query, but the terms variable and template variable are often used interchangeably. {{< /admonition >}}
groupByNode(movingAverage(apps.$app.$server.counters.requests.count, 10), 2, 'sum')
The following image shows a panel in edit mode using the query:
{{< figure src="/media/docs/grafana/dashboards/screenshot-template-query-v12.1.png" max-width="750px" alt="A panel using a template query" >}}
{{< admonition type="note">}} Grafana documentation and the application typically refer to a template query as a query, but the terms variable and template variable are often used interchangeably. {{< /admonition >}}
By default, variable values are synced to the URL using query parameter syntax, var-<varname>=value.
For example:
https://play.grafana.org/d/HYaGDGIMk/templating-global-variables-and-interpolation?orgId=1&from=now-6h&to=now&timezone=utc&var-Server=CCC&var-MyCustomDashboardVariable=Hello%20World%21
In the preceding example, the variables and values are var-Server=CCC and var-MyCustomDashboardVariable=Hello%20World%21.
You can prevent a variable from being synced to the URL by setting skipUrlSync to true in the variable definition within the dashboard JSON model. When set, the variable value won't appear as a var- query parameter in the URL.
This is useful when you want to keep URLs clean, prevent users from overriding a variable value through the URL, or avoid exposing sensitive values in shared links.
{{< admonition type="note">}}
Constant variables have skipUrlSync set to true by default, since their value is fixed and not intended to be changed through the URL.
{{< /admonition >}}
For more information about URL variables, shared links, time ranges, and filters, refer to Create dashboard URL variables.
The following dashboards in Grafana Play provide examples of template variables:
Use the following topics based on what you want to do next:
The following pages contain the full variables documentation:
{{< section >}}