News & Updates

How to Set a Grafana Template Variable from a Panel – Step‑by‑Step

By Julian Ashford 10 min read 4751 views

How to Set a Grafana Template Variable from a Panel – Step‑by‑Step

Template variables are the secret sauce that makes a Grafana dashboard feel alive. Instead of hard‑coding a server name or a time range, you expose a drop‑down that lets viewers explore data on their own terms. Sometimes you want that flexibility to go one step further: clicking a bar, a node, or a row should instantly rewrite the variable and refresh the whole view. Below is a practical, step‑by‑step walk‑through that shows exactly how to bind a panel click to a template variable value.

Why Hook a Panel to a Template Variable?

Imagine a heat‑map of HTTP response codes. By clicking the “500” bar you could instantly filter a time‑series panel to show only the offending endpoints. The same pattern works for host‑level dashboards, service‑level overviews, or even custom “region” selectors. The result is a cleaner UI—no extra dropdowns, no extra clicks—just an intuitive visual drill‑down.

Preparing Your Dashboard

  • Make sure you run a recent Grafana version (9.0+). Data links that “Set a variable” were introduced in the 8.x series but became more stable in 9.
  • Identify the panel that will act as the trigger. Typical candidates are bar charts, tables, or node graphs where each row or bar represents a distinct value you want to capture.
  • Decide what kind of variable you need: Query (populated from a data source), Custom (a static list), or Constant (single fixed value). The steps below work for any type, as long as the variable accepts the incoming value.

Creating the Template Variable

1. Open the dashboard settings (gear icon) and choose Variables → New.

2. Give the variable a clear name, e.g., host.

3. Choose the appropriate Type. For a list of hosts you might select Query and write something like label_values(node_cpu_seconds_total{job="node"}, instance).

4. In the Refresh dropdown, pick On Dashboard Load or On Time Range Change depending on how dynamic you need the list to be.

5. Click Save. The new variable now appears as a drop‑down at the top of the dashboard.

Adding a Data Link to Your Panel

The magic happens in the panel editor.

1. Click the panel title, choose Edit, and navigate to the Field tab.

2. Scroll to the Data links section and click Add data link.

3. Set a friendly Title like “Filter by this host”.

4. Under Link URL select Set a variable value from the dropdown menu.

Now you’ll see a small form: choose the variable you created earlier (host) and specify the value you want to feed it. Most often you’ll use a built‑in placeholder such as $__cell (the cell’s raw value) or $__value.raw for numeric series. For a table, $__row.name might be the right choice.

5. Optionally enable “Open in new tab” if you prefer the link to keep the current view untouched.

6. Click Apply and then Save the dashboard.

Using the Variable in Other Panels

With the variable wired up, any panel that references $host will react as soon as a user clicks the linked panel. For example, a time‑series query could look like:

node_cpu_seconds_total{instance="$host"}

When the user clicks a bar showing “server‑01”, the variable becomes “server‑01”, the query rewrites, and the time‑series panel instantly refreshes to display that host’s CPU usage.

Testing the Interaction

1. Return to the dashboard view.

2. Click a data point in the source panel.

3. Observe the variable dropdown at the top—it should now display the selected value.

4. Verify that any dependent panels update without a manual refresh.

If nothing happens, double‑check the placeholder you used in the data link. Grafana’s tooltip (hover over the link icon) often hints at the correct variable, like $__cell for tables or $__value.raw for graphs.

Common Pitfalls and How to Fix Them

  • Variable doesn’t accept the value – Ensure the variable’s Include All option is disabled if you only need single values, and verify that the incoming value matches the variable’s format (string vs. number).
  • Panel doesn’t refresh – Set the variable’s Refresh behavior to “On Dashboard Load” or “On Time Range Change”, then manually trigger a refresh to see if the binding works.
  • Multiple values needed – If you want a multi‑select, enable “Multi-value” on the variable and use a data link that passes a comma‑separated list (e.g., ${__cell:csv}).

FAQ

Can I set a variable from a panel without using data links?

Yes. Older Grafana versions supported “Panel links” that could call a URL with var-name=value parameters. Data links are now the preferred method because they stay inside the dashboard UI and require no extra server configuration.

Do data links work on dashboards that are embedded elsewhere?

They do, as long as the embedding page respects Grafana’s internal routing. If you lock the dashboard in “view only” mode, clicks will still set the variable, but the viewer won’t be able to edit the dashboard.

Is it possible to set multiple variables with a single click?

Absolutely. Add multiple data links to the same panel, each targeting a different variable, or use a single link that passes a JSON payload and parse it with Grafana’s variables API. The UI approach is to add separate links for clarity.

What if my panel uses a time‑range that’s different from the rest of the dashboard?

Variables are independent of time ranges. If you need the time range to sync, enable “Sync to dashboard time range” on the panel, or use a dashboard-level time picker that all panels share.

How to dynamically set Grafana data source HTTP header based on the ...
Select only one value from list of values from a Template Variable in ...
I want to execute the single metrics query for multiple variable values ...
Setup | Grafana 360 viewer

Written by Julian Ashford

Julian Ashford is a Chief Correspondent with more than a decade of experience reporting on public affairs, global events, and developing stories. His coverage emphasizes careful sourcing and practical context, giving readers a clearer understanding of significant events and the forces driving them.


You Might Like