Enterprise Grafana Cloud
Last reviewed: July 28, 2026

Azure Cosmos DB query editor

The Azure Cosmos DB query editor lets you create and run Azure Cosmos DB for NoSQL queries in Grafana.

Before you begin

Before you use the query editor, configure the Azure Cosmos DB data source.

Key concepts

If you’re new to Azure Cosmos DB, these terms are used throughout the query editor:

TermDescription
DatabaseA logical container for one or more Azure Cosmos DB containers.
ContainerThe unit that stores your items (documents) and is partitioned across physical partitions.
Partition keyThe property path Azure Cosmos DB uses to distribute items across partitions (for example, /deviceId). In the query editor, the PartitionKey field takes a value for that property, not the path itself.
Single-partition queryA query scoped to one partition by entering a value in the PartitionKey field.
Multi-partition queryA query that runs across all partitions when the PartitionKey field is empty. This query type has keyword limitations.

Build a query

Use the query editor header to scope the query, then write your NoSQL query in the editor:

FieldDescription
DatabaseSelect a database.
ContainerAfter you select a database, select a container.
PartitionKeyEnter the partition key value (for example, device-01), not the partition key path or property name (for example, /deviceId). Leave this field empty to run a multi-partition query, which has limitations.
QueryEnter an Azure Cosmos DB for NoSQL query. Refer to Queries in Azure Cosmos DB for NoSQL for more information about writing queries.

To create a query:

  1. Select a Database.
  2. Select a Container.
  3. Optionally, enter a value in the PartitionKey field to run a single-partition query.
  4. Enter your query in the Query editor.
  5. Click outside the editor or press the run shortcut to run the query.

Multi-partition queries don’t support the TOP, ORDER BY, OFFSET, LIMIT, Aggregates, DISTINCT, and GROUP BY keywords. To use these keywords, enter a value in the PartitionKey field to run a single-partition query.

To visualize results as a time series, return a timestamp field and one or more numeric fields, and filter the timestamp with a time macro.

Query examples

The following examples use the Azure Cosmos DB for NoSQL query language. The alias c refers to the items in the selected container.

Return recent telemetry for a device, scoped to the dashboard time range:

SQL
SELECT c.timestamp, c.temperature, c.humidity
FROM c
WHERE c.deviceId = "device-01" AND $__timeFilter(c.timestamp)

Count items by status over the time range. Aggregations and GROUP BY require a single-partition query, so set the PartitionKey field:

SQL
SELECT COUNT(1) AS total, c.status
FROM c
WHERE $__timeFilter(c.createdAt)
GROUP BY c.status

Return the most recent items using $__timeFrom to filter from the start of the time range:

SQL
SELECT c.timestamp, c.orderId, c.amount
FROM c
WHERE $__timeFrom(c.timestamp)

Use cases

Use the query editor to support scenarios such as:

  • Monitor IoT telemetry: Chart sensor readings such as temperature or humidity over time by returning a timestamp and numeric fields, filtered with $__timeFilter.
  • Track application events: Count events, orders, or errors by category with a single-partition GROUP BY query to power stat and bar chart panels.
  • Audit recent activity: Return the latest records within the dashboard time range for table panels, using $__timeFrom to limit results to the current window.

Macros

To simplify syntax and allow for dynamic parts, such as date range filters, a query can contain macros.

The following example uses a macro that applies the Grafana time range filter:

SQL
SELECT c.date_time, c.data_stuff
FROM c
WHERE $__timeFilter(c.date_time)

The query editor supports the following macros:

MacroDescription
$__timeFilter(column)Replaced by a condition that filters the data, using the provided column, based on the panel time range. Output example: column >= '2024-05-10T16:00:00Z' AND column <= '2024-05-10T17:00:00Z'.
$__timeFrom(column)Replaced by a condition that filters the data, using the provided column, based on the panel’s from time. Output example: column >= '2024-05-10T16:00:00Z'.
$__timeTo(column)Replaced by a condition that filters the data, using the provided column, based on the panel’s to time. Output example: column <= '2024-05-10T17:00:00Z'.

Next steps