---
title: "Create query-based variables | Grafana Labs"
description: "Learn how to create variables populated dynamically from data source queries."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

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

01. Open a dashboard and click **Edit** in the top-right corner.
02. In the sidebar on the right, click the **Dashboard options** icon.
03. In the edit pane that opens, click **Settings**.
04. On the **Settings** page, go to the **Variables** tab.
05. Click **Add variable**.
06. In the **Variable type** drop-down list, choose **Query**.
07. In the **General** section, enter the name of the variable. This is the name that you’ll use later in queries.
    
    For example, `environment`.
08. 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.
09. 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.
