---
title: Variables in notebooks: build dynamic, repeatable runbooks
source: https://docs.newrelic.com/docs/query-your-data/explore-query-data/notebooks/notebook-variables
---

With notebook variables, you can set up dynamic parameters that cascade through multiple queries across query blocks. Unlike [dashboard template variables](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/dashboard-template-variables), which filter a static view, notebook variables act as injected parameters that drive the underlying logic of your entire document.

Go to **[one.newrelic.com](https://one.newrelic.com) > All capabilities > Notebooks**.

## Why use variables in notebooks? [#why-use-variables]

Notebook variables let you build interactive experiences such as:

-   **Parameterized runbooks** where an engineer inputs a specific `hostName` or `transactionId` to instantly generate a targeted system health report.
-   **Post-mortem templates** that can be updated with specific timestamps, regions, or application names to analyze an incident.
-   **Interactive thresholds** where users can tweak variables like `errorThreshold` or `latencyTarget` to see how different parameters affect the visualized data.

The key benefits include:

-   **Creating executable runbooks**: Variables turn your notebooks from static pages into interactive tools. You can build adaptable workflows for alert event investigations, performance analysis, and team collaboration.
-   **Standardizing knowledge sharing**: As a saved document, you can share analytical workflows with colleagues. Users don't need to rewrite complex NRQL queries — they simply change to a new variable value from your pre-defined variables and execute the workflow.

## Define a notebook variable [#define-variable]

Define a variable that you'll inject into your NRQL query blocks to parameterize your data retrieval and visualizations.

To define a variable:

1.  From an open notebook, locate the variables bar at the top of the interface.
2.  Click **+ Add variable**.
3.  Configure your variable type (for example, text input, dropdown, or numeric) and provide a default value.

### Variable naming rules [#naming-rules]

When naming your variable, keep the following in mind:

-   The name is the exact string you will use in your notebook blocks, surrounded by `{{...}}`. For example, if you name the variable `targetApp`, you reference it in your query blocks as `{{targetApp}}`.
-   Variable names are case-sensitive and must be unique within a notebook.
-   Choose descriptive names that make your runbook self-explanatory to other users (for example, `failingHost` rather than `var1`).

## Use a variable in a query block [#use-variable]

Once defined, reference your variable anywhere in a NRQL query using the `{{variableName}}` syntax:

```sql
SELECT average(cpuPercent) FROM SystemSample WHERE hostname = {{failingHost}} TIMESERIES
```

```sql
SELECT latest(cpuPercent) FROM ProcessSample WHERE hostname = {{failingHost}} FACET processDisplayName
```

When you run the query, the variable value you have set in the variables bar is substituted into the query at runtime. Changing the variable value and re-running the query instantly updates all blocks that reference that variable.

## Automate incident response with runbooks [#runbook-use-case]

The most powerful application for notebook variables is creating executable runbooks for incident response. By combining notebook variables with [New Relic Workflows](https://docs.newrelic.com/docs/alerts/get-notified/incident-workflows), you can automate the handoff between an alert triggering and the investigation beginning.

### Structure your runbook [#structure-runbook]

A well-structured parameterized runbook typically follows this pattern:

| Block | Type     | Purpose                                                                                                                                                       |
| ----- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1     | Markdown | Instructions on how to use the runbook and architectural context of the service                                                                               |
| 2     | NRQL     | High-level system check using the injected variable — for example: `SELECT average(cpuPercent) FROM SystemSample WHERE hostname = {{failingHost}} TIMESERIES` |
| 3     | Markdown | Conditional guidance — for example: "If the chart above shows CPU > 90%, run the next query to identify the top processes."                                   |
| 4     | NRQL     | Deeper investigative query — for example: `SELECT latest(cpuPercent) FROM ProcessSample WHERE hostname = {{failingHost}} FACET processDisplayName`            |

## What's next? [#whats-next]

-   [Get started with notebooks](https://docs.newrelic.com/docs/query-your-data/explore-query-data/notebooks/introduction-notebooks) for an overview of the notebooks feature
-   [Blocks in notebook](https://docs.newrelic.com/docs/query-your-data/explore-query-data/notebooks/add-queries-to-notebooks) to learn more about query and text blocks
-   [Blob Storage API for notebooks](https://docs.newrelic.com/docs/query-your-data/explore-query-data/notebooks/blob-storage-api-for-notebooks) to manage notebooks programmatically
-   [Template variables: dynamically filter dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/dashboard-template-variables) for the equivalent dashboard feature
