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

# Concepts: How billable metrics work

> This document explains billable metrics, the aggregates of usage for each customer and period, and how the visual builder and the SQL builder build them.

## Billable metrics

> [accordion] Walkthroughs
>
> You can start with a walkthrough instead of reading this document first. A walkthrough is the practical extension of the concepts in this document. It puts them into practice through one example with concrete values. Each part of the walkthrough explains the concepts that its steps apply and links to the sections that cover them.
>
> | Walkthrough | What it shows |
> | - | - |
> | [End-to-end walkthrough: How usage works](https://zenskar.com/docs/20260301/zenskar-modules/usage/walkthroughs/usage.md) | This example-based document shows how the API calls of one customer become usage events, and how a billable metric turns those usage events into one result for each billing period. |
> | [Single-concept walkthrough: How the SQL builder works](https://zenskar.com/docs/20260301/zenskar-modules/usage/walkthroughs/sql-builder.md) | This example-based document shows how a billable metric moves from the visual builder to the SQL builder, and how its SQL query gives one result for each region of one customer. |

> [accordion] Map of concepts
>
> | Concept | What it is | What it is used for |
> | - | - | - |
> | [Billable metric](#billable-metrics-usage-aggregated-for-billing) | An aggregate of usage for each customer and period that Zenskar can use for billing | A billable metric reads usage events, the synced usage data of a synced data source, or a remote data source. |
> | [Visual builder](#visual-builder-a-billable-metric-in-four-steps) | The wizard that builds a billable metric from a table, filters, a customer column, a date column, and a calculation type | If a billable metric is built in the visual builder, it aggregates the usage of each customer and period into one number. |
> | [Calculation type](#calculation-types-how-the-visual-builder-aggregates) | The aggregate function that a billable metric built in the visual builder applies to one column: count, sum, maximum, average, minimum, or unique count | A billable metric built in the visual builder has one calculation type. |
> | [SQL builder](#sql-builder-a-billable-metric-as-an-sql-query) | The editor that builds a billable metric from an SQL query | If a billable metric is built in the SQL builder, its query aggregates the usage of each customer and period into one number, or into one number for each combination of dimension values. |
> | [Zenskar SQL variable](#zenskar-sql-variables-values-that-zenskar-fills-in) | A placeholder in an SQL query that Zenskar replaces with a value of the customer, the contract, the product, or the period | A query in the SQL builder can contain Zenskar SQL variables. |

> [accordion] Related modules
>
> Usage uses the following modules:
>
> | Module | Details |
> | - | - |
> | [Customers](https://zenskar.com/docs/20260301/zenskar-modules/customers/concepts.md#customers-who-you-bill) | A billable metric matches each row to a customer by email, external ID, or name. |
> | [Custom attributes](https://zenskar.com/docs/20260301/zenskar-modules/settings/custom-attributes/concepts.md#custom-attributes-details-that-you-add-to-records) | A billable metric can match each row to a customer by a custom attribute of the customer. A query in the SQL builder can use the custom attributes of the customer, the contract, and the product as Zenskar SQL variables. |
>
> The following modules use Usage:
>
> | Module | Details |
> | - | - |
> | [Products](https://zenskar.com/docs/20260301/zenskar-modules/products/concepts/products.md#metered-usage-quantity-from-a-billable-metric) | A pricing preset with metered usage has one billable metric. |
> | [Contracts](https://zenskar.com/docs/20260301/zenskar-modules/contracts/concepts/line-items/quantity.md#metered-usage) | A quantity version with metered usage gets its quantity from a billable metric. |
> | [Pricing models](https://zenskar.com/docs/20260301/pricing-models/concepts/dimensional.md) | Under the dimensional pricing model, a billable metric reports the quantity of each combination. |

## Billable metrics: usage aggregated for billing

A **billable metric** is an [aggregate](https://en.wikipedia.org/wiki/Aggregate_function) of [usage](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage) for each customer and period that Zenskar can use for billing.

A billable metric reads [usage events](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event), the [synced usage data](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#synced-usage-data) of a [synced data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#synced-data-source), or a [remote data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#remote-data-source).

For example, you send usage events to the [usage-event collection](https://zenskar.com/docs/20260301/zenskar-modules/usage/concepts/usage-events.md#usage-events-as-rows-and-columns) `api_calls`. Another Zenskar customer, Roadrunner Inc, syncs a MongoDB collection, which has no tables. A third, Coyote Labs, syncs a PostgreSQL table. Each one ends as a table with one column for each field.

```mermaid theme={null}
flowchart LR
  subgraph Y["You"]
    direction LR
    A1["Usage events<br/>with root fields and data fields"] -->|"Checked against the data schema,<br/>stored as one row each"| A2["Usage-event collection: table api_calls<br/>One column for each field"]
    A2 -->|"Reads its columns"| A3["Billable metric"]
  end
  subgraph R["Roadrunner Inc"]
    direction LR
    B1["Data source:<br/>MongoDB collection orders"] -->|"Data-source connector<br/>shows it as a table"| B2["Synced usage data: table orders<br/>One column for each field"]
    B2 -->|"Reads its columns"| B3["Billable metric"]
  end
  subgraph C["Coyote Labs"]
    direction LR
    C1["Data source:<br/>PostgreSQL table api_calls"] -->|"Data-source connector<br/>copies the table"| C2["Synced usage data: table api_calls<br/>One column for each field"]
    C2 -->|"Reads its columns"| C3["Billable metric"]
  end
```

This conceptual diagram shows how usage events and two synced data sources each become a table with one column for each field.

> [!NOTE]
>
> A usage-event collection is the same [row-and-column](https://en.wikipedia.org/wiki/Table_\(database\)) representation as the synced usage data of a synced data source, and a billable metric reads both in the same way.

To aggregate the usage, a billable metric takes four steps:

1. It filters the rows.
2. It matches each row to a customer.
3. It keeps the rows of the period.
4. It aggregates the rows, such as with a count or a sum.

For example, a billable metric that counts the API calls in the usage-event collection `api_calls` aggregates the usage of one customer in October into 12,000.

How Zenskar charges for the result depends on the pricing of the [product](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#product). See [Metered usage: quantity from a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/products/concepts/products.md#metered-usage-quantity-from-a-billable-metric) and [Metered usage](https://zenskar.com/docs/20260301/zenskar-modules/contracts/concepts/line-items/quantity.md#metered-usage).

A billable metric is built in the [visual builder](#visual-builder-a-billable-metric-in-four-steps) or the [SQL builder](#sql-builder-a-billable-metric-as-an-sql-query):

| | Visual builder | SQL builder |
| - | - | - |
| What it reads | One usage-event collection, or one table of synced usage data | A usage-event collection, synced usage data, or a remote data source |
| What it aggregates the usage into | One number for each customer and period | One number for each customer and period, or one for each combination of [dimension values](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#dimension-value) |
| How it aggregates | With one [calculation type](#calculation-types-how-the-visual-builder-aggregates) on one column | With an SQL query that can contain [Zenskar SQL variables](#zenskar-sql-variables-values-that-zenskar-fills-in) |
| What a billable metric can use | Six calculation types and filters on one table | Any SQL that the source supports, such as expressions, functions, and joins of tables |

The name of a billable metric can change. A billable metric can be deleted, even when a [pricing preset](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#pricing-preset) uses it. Changing or deleting a billable metric can affect [contracts](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#contract) and invoices.

See [Create a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#create-a-billable-metric).

## Visual builder: a billable metric in four steps

The **visual builder** is the [wizard](https://en.wikipedia.org/wiki/Wizard_\(software\)) that builds a billable metric from a table, filters, a customer column, a date column, and a calculation type.

Each step of the visual builder sets one part of the billable metric:

| Step | What it sets |
| - | - |
| Table and filters | The table that the billable metric reads, such as the usage-event collection `api_calls`, and the filters that keep only some rows. The table is a usage-event collection or a table of synced usage data. Each filter is a column, a condition, and a value, such as `region` equals `eu-west`. Filters form groups, and the groups combine with AND or OR. |
| Customer column | The column that identifies the customer of each row. Zenskar matches its value to the email, the external ID, or the name of a customer, or to a [custom attribute](https://zenskar.com/docs/20260301/zenskar-modules/settings/glossary.md#custom-attribute) of the customer whose [custom attribute type](https://zenskar.com/docs/20260301/zenskar-modules/settings/glossary.md#custom-attribute-type) is Text, Whole number, or Dropdown. By default, `customer_id` is matched to the external ID of the customer, or to the ID that Zenskar assigns to the customer. |
| Date column | The column that decides which rows fall in the period. By default, it is `timestamp`. Any other column needs its date format. |
| Calculation type | The calculation type, and the column that it applies to, such as the sum of `size_kb`. |

Zenskar converts the settings of the visual builder into an SQL query. Zenskar runs that query each time it runs the billable metric. The query follows the settings, so you change it only through the settings.

A billable metric built in the visual builder can move to the [SQL builder](#sql-builder-a-billable-metric-as-an-sql-query), where you change the query itself.

> [!CAUTION]
>
> The move is one way: after the billable metric is saved as a query, it cannot return to the visual builder. Move a billable metric only when it needs a query that the visual builder cannot build, such as one with dimensions.

See [Move a billable metric to the SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#move-a-billable-metric-to-the-sql-builder).

### Calculation types: how the visual builder aggregates

A **calculation type** is the aggregate function that a billable metric built in the visual builder applies to one column: count, sum, maximum, average, minimum, or unique count.

| Calculation type | Result |
| - | - |
| Count | The number of rows that have a value in the column. It is the default. |
| Sum | The total of the values in the column |
| Maximum | The largest value in the column |
| Average | The average of the values in the column |
| Minimum | The smallest value in the column |
| Unique count | The number of different values in the column |

Each calculation type applies to the rows that the filters, the customer column, and the date column keep.

See [Create a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#create-a-billable-metric).

## SQL builder: a billable metric as an SQL query

The **SQL builder** is the editor that builds a billable metric from an SQL query.

The number of the billable metric depends on what the query returns:

| What the query returns | Number of the billable metric |
| - | - |
| One value: one row with one column | That value, whatever its column is called |
| More than one column, such as one for each [dimension](https://en.wikipedia.org/wiki/Dimension_\(data_warehouse\)) | The values of the column named `quantity`, one for each combination of dimension values. Each other column is a dimension. |

The [visual builder](#visual-builder-a-billable-metric-in-four-steps) always builds a query that returns one value, named `quantity`. A query in the SQL builder can return more columns, so the SQL builder covers what the visual builder cannot, such as a billable metric for the [dimensional pricing model](https://zenskar.com/docs/20260301/pricing-models/concepts/dimensional.md).

> [!WARNING]
>
> A query that returns more than one column, without a column named `quantity`, gives 0 for the period or fails. Name the column of the number `quantity`.

A query names the tables that it reads:

| Source | Table in a query |
| - | - |
| A usage-event collection | `raw_metric_` followed by its [API slug](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#api-slug), such as `raw_metric_api_calls` |
| Synced usage data | A table whose name starts with `datasource_` |
| A remote data source | A table of the [data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-source) itself |

A query on a usage-event collection or on synced usage data uses [ClickHouse SQL](https://clickhouse.com/docs/sql-reference). A query on a remote data source uses the SQL of that data source, and a BigQuery data source can need fully qualified table names. See [Fully qualified table names](https://zenskar.com/docs/20260301/third-party-integrations/data-sources/bigquery.md#fully-qualified-table-names).

### Zenskar SQL variables: values that Zenskar fills in

A **Zenskar SQL variable** is a placeholder in an SQL query that Zenskar replaces with a value of the customer, the [contract](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#contract), the [product](https://zenskar.com/docs/20260301/zenskar-modules/products/glossary.md#product), or the period.

| Zenskar SQL variable | Value |
| - | - |
| `{{customer.customer_name}}` | The name of the customer |
| `{{customer.email}}` | The email of the customer |
| `{{customer.external_id}}` | The external ID of the customer |
| `{{start_date}}` | The first day of the period |
| `{{end_date}}` | The last day of the period |
| `{{contract.<field>}}` | A field of the contract |
| `{{product.<field>}}` | A field of the product |
| `{{customer.custom_attributes.<key>}}` | A custom attribute of the customer |
| `{{contract.custom_attributes.<key>}}` | A custom attribute of the contract |
| `{{product.custom_attributes.<key>}}` | A custom attribute of the product |

Zenskar quotes each text value when it fills in a Zenskar SQL variable, so a Zenskar SQL variable needs no quotes in the query. Both dates are part of the period. The following query counts the API calls of a customer in a period:

```sql theme={null}
SELECT COUNT(*) AS quantity
FROM "raw_metric_api_calls"
WHERE customer_id = {{customer.external_id}}
  AND DATE(timestamp) >= DATE({{start_date}})
  AND DATE(timestamp) <= DATE({{end_date}})
```

## Results for each customer and period

Zenskar runs a billable metric for one customer and one period at a time. Each run gives one number, one number for each combination of dimension values, or no value.

A run can give no value when no rows match. The visual builder gives 0 in place of no value. In the SQL builder, a sum over a month without usage events gives no value, and a count over the same month gives 0. A query can give 0 in place of no value, such as with `COALESCE(SUM(<column>), 0)`.

No value and 0 can lead to different charges. See [Quantity strategies: consumption or allocation](https://zenskar.com/docs/20260301/zenskar-modules/products/concepts/products.md#quantity-strategies-consumption-or-allocation).

See [Preview a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#preview-a-billable-metric).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.