A single Grafana dashboard can show several views of the same metrics. A dashboard variable adds a dropdown for changing a query filter without editing the panels each time.

Variables are saved with the dashboard that defines them. Panel queries and titles refer to a variable by its name, such as $mode or ${mode}. A dropdown only affects panels that reference it; adding the control does not automatically filter every query.

A Custom variable suits a short, stable list such as CPU modes, while a Query variable can discover changing hostnames from a data source. The CPU panel needs an existing Prometheus data source with node_exporter metrics and permission to edit its dashboard.

Steps to add a variable to a Grafana dashboard:

  1. Open the Grafana dashboard containing your CPU panel.
  2. Click Edit in the dashboard toolbar.

    The sidebar editor shown here is from Grafana 13.3. View-only users can change existing variable values but cannot save a new variable.

  3. Click the plus button above the panels at the left of the dashboard toolbar.
  4. Choose Variable from the menu.
  5. Select Custom as the variable type.
  6. Set Name to mode.

    The name is the identifier used in queries. The optional Label changes the dropdown's display text without changing that identifier. The default Display setting is Above dashboard.

  7. Click Open variable editor lower in the variable sidebar.

    Multi-value and Include All value remain off for the single-value query used here.

  8. Enter idle,system,user in the CSV field.

    These values match the node_exporter CPU mode labels. A custom list is maintained manually rather than fetched from Prometheus.

  9. Click Apply when Preview of values shows idle, system, and user.
  10. Select the CPU panel on the dashboard.
  11. Set the panel's Title to CPU $mode by instance.

    The title will display the selected mode, making it clear whether the panel shows idle, system, or user CPU time.

  12. Click Edit visualization in the panel sidebar.
  13. Replace the panel's Prometheus Code query with the following expression.
    avg by (instance) (rate(node_cpu_seconds_total{mode="$mode"}[5m])) * 100

    The mode="$mode" matcher uses the dropdown's selected value. The query averages each instance's CPU time across its CPUs over five minutes and converts the rate to a percentage. If the editor opens in Builder mode, Code exposes the expression field.

  14. Click Run queries to display CPU time for the selected mode.
  15. Click Back to return to the dashboard editor.

    Saving updates the shared dashboard's variable and panel definition for other viewers. The underlying Prometheus metrics are unchanged.

  16. Click Save in the dashboard toolbar.
  17. Click Save in the Save dashboard pane.
  18. Click Exit edit after the dashboard is saved.
  19. Open the mode dropdown above the panels.
  20. Select system from the list.
  21. Confirm that the panel title shows CPU system by instance and the graph displays system CPU time.

    The plotted values should change from the idle selection; exact percentages depend on the host's workload. If No data appears, a misspelled mode, missing CPU metrics, or the dashboard time range may be responsible.
    Related: How to use Grafana query inspector

Share this guide