---
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 that uses a Prometheus or metrics data source.
02. Click **Edit** in the top-right corner.
03. In the right sidebar, click **Dashboard options** (gear icon).
04. In the **Variables** section of the pane, click **Add variable**.
05. Click **Query** to choose the query variable type.
06. In the **Name** field, enter the variable name you’ll use in queries.
    
    For example, enter `environment`.
07. Under **Query options**, click **Open variable editor**.
08. In the **Query Variable** editor, select your Grafana Cloud Prometheus data source.
09. In the **Query** field, enter a query that returns the list of values.
    
    For example, select **Classic query** and enter `label_values(up, environment)` to retrieve unique environment labels.
10. Under **Refresh**, select when variable values should update.
    
    For example, select **On dashboard load**.
11. Click **Preview** to review the list of values.
12. Click **Close** to finish configuring the query.
13. Click **Save** to save the dashboard with your new variable.
14. 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.
