---
title: "Use variables in queries | Grafana Labs"
description: "Learn how to reference variables in panel queries to create dynamic filtering."
---

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

# Use variables in queries

Now that you’ve created variables, you can use them in panel queries to create dynamic filtering. When users change a variable selection, the panel automatically updates to show filtered results based on that selection.

Variables use a basic syntax that works across different data sources. Understanding how to reference variables correctly for your data source ensures your panels respond dynamically to user interactions.

To use variables in panel queries, complete the following steps:

01. Open a dashboard that already has a variable (for example, `environment`) and a panel that queries your metrics data source.
02. If you are not already editing, click **Edit** in the top-right corner.
03. Hover over the panel that you want to update, click the panel menu (⋮), then click **Edit**.
    
    The panel editor opens.
04. In the **Queries** tab, update your query to reference the variable with `$variableName` or `${variableName}`.
    
    For example, change `up{environment="production"}` to `up{environment="$environment"}`.
    
    For multi-value variables, use regular expression syntax: `up{environment=~"$environment"}`.
    
    If the label does not exist on your series, the panel shows **No data** — that confirms the filter is applied. Use a label that exists on your metrics for live filtered results.
05. Click **Run queries** (or **Refresh**) to test the query with the current variable value.
06. Verify the query returns filtered results based on the variable selection.
07. Click **Save**.
08. Click **Back to dashboard** to leave the panel editor.
09. Click **Exit edit** to leave dashboard edit mode.
10. When users change the variable selection in the dashboard, Grafana updates the panel data to show filtered results.

## Variable syntax examples

Different data sources use different syntax for referencing variables in queries.

**Prometheus queries**

- Single value: `metric_name{label="$variable"}`
- Multi-value: `metric_name{label=~"$variable"}`

**Loki queries**

- Single value: `{environment="$environment"}`
- Multi-value: `{environment=~"$environment"}`

**SQL queries**

- Single value: `SELECT * FROM table WHERE column = '$variable'`
- Multi-value: `SELECT * FROM table WHERE column IN ($variable:csv)`

In the next milestone, you learn how to create dependent variables that cascade based on other variable selections.

Were you successful?

![No](/media/images/svg/thumbs-up.svg) No

![Yes](/media/images/svg/thumbs-up.svg) Yes

Super! You can move to the next step.

* * *

### Troubleshooting options

Explore the following troubleshooting topic if you need help:

- [Review variable syntax](/docs/grafana/latest/visualizations/dashboards/variables/variable-syntax/)

Please tell us what didn't work:

Submit

Submitting...

There was an error with your submission.

Thank you!

Need more support? Check out these [helpful resources.](/help/)
