Nested variables let you create dynamic, dependent relationships between variables on your dashboards. This allows you to:
- Reuse dashboard variables and dynamically change what's shown, especially for filtering based on other variables' data.
- Create parent/child relationships in variables, forming a tree structure where you can select different levels of filters.
Use case
Imagine you've created an Env
variable to select an environment (for example, production and staging). You want to update a Machine
variable with a list of machines based on the selected environment. Directly embedding the Env variable in a WHERE
condition for Machine
isn't supported in a standard setup.
Example:
Variable Env
=production
orstaging
Variable Machine
=SELECT uniques(machine) FROM Table WHERE env in ({{Env}})
This direct approach isn't supported without nested variables. Nested variables solve this by allowing your Machine
variable's query to dynamically reference the value of your Env
variable.
How to use nested variables
To implement nested variables, follow these steps:
- Go to a dashboard you can edit.
- Create your parent variable (for example,
env
) by using theAdd Variable
function:FROM TransactionSELECT uniques(environment) - Create another variable (for example,
variable_cluster
) and, when defining it, place{{env}}
inside the textbox:FROM TableSELECT uniques(machine)WHERE env IN ({{env}}) - Save both variables.
- Go to your dashboard and interact with the variable's values. They should automatically change based on the nested variable value selection.
Now you can use your child variable inside any widget on your dashboard. When your variable_cluster
changes, your widget will also change!
Limitations
When you use nested variables, be aware of the following limitations:
- Parent variable selection required for child definition: Make sure your
env
variable has selected values when you define yourmachine
variable. Otherwise, themachine
query will return no results. - Variable selection required for widget usage: Ensure the variable has selected values when you use it inside widgets. Otherwise, the widget's query will return no results.
- Deletion impact: If your
env
variable is deleted, yourmachine
variable may show errors when loading. - Cleared values: If your
env
variable's values are cleared, yourmachine
variable will continue to show values based onenv
's previous selections.