> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cognite.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create templates with global variables

> Use variables to create templates in Grafana dashboards.

## Before you begin

Create a dashboard and add the <span translate="no">Cognite Data Source for Grafana</span> as a data source.

## Create a variable from a query

Use the `$variable` or `[[variable]]` syntax to create templates.

<Steps>
  <Step title="Open variables settings">
    Navigate to your dashboard's settings, and select <span class="ui-element">Variables</span> from the left-hand side.
  </Step>

  <Step title="Configure the data source">
    Set <span class="ui-element">Type</span> to <span class="ui-element">Query</span>, and select your <span translate="no">Cognite Data Source</span> as the <span class="ui-element">Data source</span>.
  </Step>

  <Step title="Enter the query">
    In the <span class="ui-element">Query</span> field, enter the query to fetch and filter assets from <span translate="no">Cognite Data Fusion (CDF)</span>.

    For example, this query requests assets with the parameter `parentIds=[123]` and filters the results by `name` that matches the `test-.*` regular expression:

    ```text theme={"languages":{"custom":["/_languages/kuiper.json","../_languages/kuiper.json"]}}
    assets{parentIds=[123], name=~"test-.*"}
    ```

    <Tip>
      For a list of **valid parameters**, see [the API documentation](/api-reference/concepts/20230101/assets).

      See [Filter time series in custom queries](/cdf/dashboards/guides/grafana/timeseries_legacy#filtering) to learn how to **filter** the results.

      You can **format variables** to fit into the query, for instance, if you have a multi-value variable like `ts{assetIds=[${variable:csv}]}`.

      If you select several assets from the dropdown, the variable is serialized to comma-separated values. Learn more about the variable format options in the [<span translate="no">Grafana</span> documentation](https://grafana.com/docs/grafana/latest/variables/advanced-variable-format-options).
    </Tip>
  </Step>

  <Step title="Save the variable">
    Select <span class="ui-element">Update</span>.
  </Step>
</Steps>

## Use data models in variables

Use GraphQL queries against data models to populate variable values from CDF.

<Steps>
  <Step title="Open the Data models tab">
    In the variable query editor, select the <span class="ui-element">Data models</span> tab.
  </Step>

  <Step title="Select the data model and version">
    Choose the <span class="ui-element">data model</span> and <span class="ui-element">version</span> you want to query.
  </Step>

  <Step title="Write the GraphQL query and select a field">
    Enter a GraphQL query and select the field to use as the variable values. The selected field should be a scalar value such as an external ID or name. The value field list is populated from your query once it is valid.
  </Step>
</Steps>

<Tip>
  If your query returns nested structures, select the field from the list items you want to use as variable values. You can also use GraphQL variable interpolation to reference existing Grafana variables in your query.
</Tip>

The query supports both list results and connection-style results, so you can use queries that return items or edges. The data models variable query is backward compatible with existing asset queries in the default tab.

<a id="nested-chained-variables" />

## Nested (chained) variables

With nested variables, you can create a template to visualize time series related to a branch of the asset subtree and let users navigate the subtree.

<Steps>
  <Step title="Create nested variables">
    Create the necessary nested variables.

    <Frame>
      <img src="https://apps-cdn.cogniteapp.com/@cognite/docs-portal-images/1.0.0/images/cdf/dashboards/grafana/nested_variables_0.png" alt="Link variables" width="80%" />
    </Frame>
  </Step>

  <Step title="Use the variable as a filter">
    Use the variable as the asset filter on the dashboard.

    <Frame>
      <img src="https://apps-cdn.cogniteapp.com/@cognite/docs-portal-images/1.0.0/images/cdf/dashboards/grafana/nested_variables_1.png" alt="Use the variable on the dashboard" width="80%" />
    </Frame>

    <Check>
      Users can choose assets on each level of the hierarchy.

      <Frame>
        <img src="https://apps-cdn.cogniteapp.com/@cognite/docs-portal-images/1.0.0/images/cdf/dashboards/grafana/nested_variables_2.png" alt="Select assets from the dropdown" width="80%" />
      </Frame>
    </Check>
  </Step>
</Steps>
