This is documentation for the next version of Grafana documentation. For the latest stable release, go to the latest version.

Grafana Cloud Enterprise Open source

Variables

Note: Looking for prebuilt Grafana dashboards? Check out our full library of dashboards and more →

A variable is a placeholder for a value. When you change the value, the element using the variable will change to reflect the new value.

Variables are displayed as drop-down lists (or in some cases text fields) at the top of the dashboard. These drop-down lists make it easy to update the variable value and thus change the data being displayed in your dashboard.

For example, if you needed 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:

Variable drop-down open and two values selected

Variables allow you to create more interactive dashboards. Instead of hard-coding things like server, application, and sensor names in your metric queries, you can use variables in their place. They’re useful for administrators who want to allow Grafana viewers to adjust visualizations without giving them full editing permissions.

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 simplifies maintenance and upkeep enormously.

You can use variables in:

To work with variables, you typically do the following:

  1. Choose the type of variable that matches the value you want viewers to control.
  2. Add the variable to the dashboard.
  3. Use the variable in queries, titles, links, or other dashboard fields.
  4. Manage the variable order, dependencies, and URL behavior as the dashboard grows.

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.

Give it a try using Grafana Play
Give it a try using Grafana Play

With Grafana Play, you can explore and see how it works, learning from practical examples to accelerate your development. This feature can be seen on Templating - Interactive dashboard.

Variables you create and global variables

Grafana supports two broad kinds of variables:

  • Variables you create: Dashboard-specific variables that you define and configure. Use these variables when viewers need to choose values such as a server, data source, region, environment, or interval. For configuration steps, refer to Add variables.
  • Global variables: Built-in variables that Grafana provides automatically, such as the current time range, dashboard name, organization, or signed-in user. For the full reference, refer to Global variables.

Choose a variable type

Choose a variable type based on how you want Grafana to get the value and how users interact with it.

Variable typeUse when
QueryThe list of values comes from a data source query, such as server names, metric names, label values, or data centers.
CustomYou want to define a fixed list of values manually.
Text boxViewers need to enter a free-form value.
ConstantA dashboard needs a reusable value that viewers don’t change.
Data sourceViewers need to switch a dashboard or query between data source instances.
IntervalViewers need to change the time grouping or aggregation interval in queries.
FiltersViewers need dashboard-wide key/value filters for supported data sources.
SwitchViewers need to toggle between two configured values.

For the complete list of variable settings and type-specific steps, refer to Add variables.

Template 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:

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.

text
groupByNode(movingAverage(apps.$app.$server.counters.requests.count, 10), 2, 'sum')

The following image shows a panel in edit mode using the query:

A panel using a template query

Variables in URLs

By default, variable values are synced to the URL using query parameter syntax, var-<varname>=value. For example:

text
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.

Note

Constant variables have skipUrlSync set to true by default, since their value is fixed and not intended to be changed through the URL.

For more information about URL variables, shared links, time ranges, and filters, refer to Create dashboard URL variables.

Additional examples

The following dashboards in Grafana Play provide examples of template variables:

Next steps

Use the following topics based on what you want to do next:

  • To create dashboard-specific variables, refer to Add variables.
  • To use built-in variables for time ranges, users, organizations, or query intervals, refer to Global variables.
  • To configure chained variables, multi-property variables, or regular expression filtering, refer to Advanced variable usage.
  • To control how Grafana interpolates variable values, refer to Variable syntax.
  • To reorder, clone, delete, or inspect variable dependencies, refer to Manage and inspect variables.

The following pages contain the full variables documentation: