Add a query variable

Templates and variablesVariable typesAdd query variable

Query variables allow you to write a data source query that can return a list of metric names, tag values, or keys. For example, a query variable might return a list of server names, sensor IDs, or data centers. The variable values change as they dynamically fetch options with a data source query.

Query expressions can contain references to other variables and in effect create linked variables. Grafana detects this and automatically refreshes a variable when one of its linked variables change.

Query expressions

Query expressions are different for each data source. For more information, refer to the documentation for your data source.

Enter General options

  1. Navigate to the dashboard you want to make a variable for and then click the Dashboard settings (gear) icon at the top of the page.
  2. On the Variables tab, click New.
  3. Enter a Name for your variable.
  4. In the Type list, select Query.
  5. (optional) In Label, enter the display name of the variable dropdown. If you don’t enter a display name, then the dropdown label will be the variable name.
  6. Choose a Hide option:
    • No selection (blank) - The variable dropdown displays the variable Name or Label value. This is the default.
    • Label - The variable dropdown only displays the selected variable value and a down arrow.
    • Variable - No variable dropdown is displayed on the dashboard.

Enter Query Options

  1. In the Data source list, select the target data source for the query. For more information about data sources, refer to Add a data source.
  2. In the Refresh list, select when the variable should update options.
    • Never - Variables queries are cached and values are not updated. This is fine if the values never change, but problematic if they are dynamic and change a lot.
    • On Dashboard Load - Queries the data source every time the dashboard loads. This slows down dashboard loading, because the variable query needs to be completed before dashboard can be initialized.
    • On Time Range Change - Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.
  3. In the Query field, enter a query.
    • The query field varies according to your data source. Some data sources have custom query editors.
    • If you need more room in a single input field query editor, then hover your cursor over the lines in the lower right corner of the field and drag downward to expand.
  4. (optional) In the Regex field, type a regex expression to filter or capture specific parts of the names returned by your data source query. To see examples, refer to Filter variables with regex.
  5. In the Sort list, select the sort order for values to be displayed in the dropdown list. The default option, Disabled, means that the order of options returned by your data source query will be used.
  6. (optional) Enter Selection Options.
  7. (optional) Enter Value groups/tags.
  8. In Preview of values, Grafana displays a list of the current variable values. Review them to ensure they match what you expect.
  9. Click Add to add the variable to the dashboard.