Create query-based variables

Query-based variables dynamically populate filter options by querying your data sources, which ensures that variable options always reflect your current infrastructure and data. Unlike static variable lists that require manual updates, query-based variables leverage your data to generate filter options.

This approach eliminates manual maintenance when entities change, because the variable automatically discovers new values through queries. With query-based variables, your dashboard filters stay synchronized with reality every time you add a new server, deploy to a new region, or introduce new services.

To create a query-based variable, complete the following steps:

  1. Open a dashboard and click Edit in the top-right corner.

  2. In the sidebar on the right, click the Dashboard options icon.

  3. In the edit pane that opens, click Settings.

  4. On the Settings page, go to the Variables tab.

  5. Click Add variable.

  6. In the Variable type drop-down list, choose Query.

  7. In the General section, enter the name of the variable. This is the name that you’ll use later in queries.

    For example, environment.

  8. Under the Query options section of the page, select a target data source in the Data source drop-down list.

    For example, select grafanacloud-<YOUR-INSTANCE-NAME>-prom, the first default data source for a Grafana Cloud stack.

  9. In the Query field, enter a query that returns the list of values.

    For example, select the query type Classic query and enter label_values(up, environment) to retrieve all unique environment labels.

  10. Under Refresh, select when the variable values should update.

    For example, select On dashboard load to refresh values when users open the dashboard.

  11. In the Preview of values section, Grafana displays a list of the current variable values. Review them to ensure they match what you expect.

  12. Click Save.

  13. (Optional) Enter a description of the changes you’ve made.

  14. Click Save and Back to list at the bottom of the page.

The dashboard displays the query variable at the top as an interactive drop-down list.

In the next milestone, you learn how to create custom variables with manually defined options.


page 3 of 9