> ## 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.

# Billable metrics

## Billable metrics

This document explains what a billable metric is, how the visual builder and the SQL builder build it, and what a billable metric gives for each customer and period.

> [accordion] Map of concepts
>
> | Concept | Details |
> | - | - |
> | [Visual builder](#visual-builder-a-billable-metric-in-four-steps) | If a billable metric is built in the visual builder, it gives one number for each customer and period. |
> | | [Calculation type](#calculation-types): A billable metric built in the visual builder has one calculation type. |
> | [SQL builder](#sql-builder-a-billable-metric-as-an-sql-query) | If a billable metric is built in the SQL builder, its query gives one number, or one number for each combination of dimension values. |
> | | [Zenskar SQL variable](#zenskar-sql-variables): A query in the SQL builder can contain Zenskar SQL variables. |

> [accordion] Related modules
>
> Billable metrics use the following modules:
>
> | Module | Details |
> | - | - |
> | [Usage events](https://zenskar.com/docs/20260301/product-modules/usage/concepts/usage-events.md#usage-events-records-of-product-consumption) | A billable metric reads the usage events of one usage-event collection. |
> | [Data sources](https://zenskar.com/docs/20260301/product-modules/usage/concepts/data-sources.md#source-access-synced-or-remote) | A billable metric reads synced usage data, or queries a remote data source. |
> | [Customers](https://zenskar.com/docs/20260301/product-modules/customers/concepts/customers.md) | A billable metric matches each row to a customer by email, external ID, name, or a custom attribute of the customer. |
> | [Custom attributes](https://zenskar.com/docs/20260301/product-modules/settings/custom-attributes.md) | 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 billable metrics:
>
> | Module | Details |
> | - | - |
> | [Products](https://zenskar.com/docs/20260301/product-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/product-modules/contracts/concepts/line-items/quantity.md#metered-usage) | A quantity version with metered usage gets its quantity from the 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 collapsed into one number

A **billable metric** is a measure of [usage](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#usage) that Zenskar calculates for each customer and each period.

A [pricing model](https://zenskar.com/docs/20260301/product-modules/products/glossary.md#pricing-model) calculates an amount from one number. Pricing models differ in how many [dimensions](https://en.wikipedia.org/wiki/Dimension_\(data_warehouse\)) they have.

The [per-unit pricing model](https://zenskar.com/docs/20260301/product-modules/products/glossary.md#per-unit-pricing-model) has one dimension: the price per unit. For example, each API call costs \$0.010:

| Price per call |
| - |
| \$0.010 |

The [volume pricing model](https://zenskar.com/docs/20260301/product-modules/products/glossary.md#volume-pricing-model) has two dimensions: the [tier](https://zenskar.com/docs/20260301/product-modules/products/glossary.md#tier), and the price per unit of each tier. For example:

| Calls in a month | Price per call |
| - | - |
| Up to 10,000 | \$0.010 |
| Above 10,000 | \$0.008 |

Both pricing models need the same one number: the number of calls of the customer in the month. The per-unit pricing model multiplies the number by the price per call. The volume pricing model first uses the number to select a tier. It then multiplies the number by the price per call of that tier.

A billable metric collapses the usage into this one number. For one customer in October, a billable metric that counts calls returns 12,000. Under the per-unit pricing model, the amount is 12,000 × \$0.010 = \$120. Under the volume pricing model, 12,000 calls select the second tier, and the amount is 12,000 × \$0.008 = \$96. See [Metered usage](https://zenskar.com/docs/20260301/product-modules/products/concepts/products.md#metered-usage-quantity-from-a-billable-metric) and [Line item: quantity](https://zenskar.com/docs/20260301/product-modules/contracts/concepts/line-items/quantity.md#metered-usage).

A billable metric returns a number.

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

To calculate the number, 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 applies a calculation, such as a count or a sum.

The result is named `quantity`. A billable metric that groups its rows by [dimensions](https://en.wikipedia.org/wiki/Dimension_\(data_warehouse\)) gives one number for each [combination](https://zenskar.com/docs/20260301/product-modules/products/glossary.md#combination) of [dimension values](https://zenskar.com/docs/20260301/product-modules/products/glossary.md#dimension-value). See [Dimensional pricing model](https://zenskar.com/docs/20260301/pricing-models/concepts/dimensional.md).

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 | It reads one usage-event collection or one table of synced usage data. | It reads usage-event collections and synced usage data, or one remote data source. |
| What it gives | It gives one number for each customer and period. | It gives one number for each customer and period, or one number for each combination of dimension values. |
| How it calculates | It applies one [calculation type](#calculation-types) to one column. | It runs an SQL query that can contain [Zenskar SQL variables](#zenskar-sql-variables). |
| Moving between them | A billable metric can move from the visual builder to the SQL builder. | A billable metric cannot move back to the visual builder. |

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

> [card] [Create a billable metric](https://zenskar.com/docs/20260301/product-modules/usage/how-to-guides/billable-metrics.md#create-a-billable-metric)
>
> This guide shows how to create a billable metric with the visual builder or the SQL builder.

## 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 | This step sets the table that the billable metric reads, 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. Filters form groups. The groups combine with AND or OR. |
| Customer column | This step sets the column that identifies the customer of each row. Zenskar matches its value to the email, the external ID, the name, or a custom attribute of a customer. The custom attribute must have the data type 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 | This step sets the column that decides which rows fall in the period. By default, it is `timestamp`. Any other column needs its date format. |
| Calculation type | This step sets the calculation type and the column that it applies to. |

A billable metric from the visual builder gives one number for each customer and period. It cannot group rows by dimensions. A remote data source cannot be used in the visual builder.

### Calculation types

A **calculation type** is the operation 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 | Count gives the number of rows that have a value in the column. It is the default. |
| Sum | Sum gives the total of the values in the column. |
| Maximum | Maximum gives the largest value in the column. |
| Average | Average gives the average of the values in the column. |
| Minimum | Minimum gives the smallest value in the column. |
| Unique count | Unique count gives 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.

## 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 query returns a column named `quantity`. Each other column that the query returns is a dimension. The query gives one number for each combination of dimension values. The SQL builder therefore 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).

A query reads either usage-event collections and synced usage data, or one remote data source. A usage-event collection is the table `raw_metric_` followed by its [API slug](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#api-slug). A table of synced usage data starts with `datasource_`. A query on usage-event collections and synced usage data uses [ClickHouse SQL](https://clickhouse.com/docs/sql-reference).

A query of a BigQuery [data source](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#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

A **Zenskar SQL variable** is a placeholder in an SQL query that Zenskar replaces with a value of the customer, the contract, the [product](https://zenskar.com/docs/20260301/product-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. 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}})
```

### From the visual builder to the SQL builder

A billable metric built in the visual builder can move to the SQL builder. Zenskar converts the settings of the visual builder into a query. The move is one way. After the billable metric is saved as a query, it cannot return to the visual builder.

> [card] [Move a billable metric to the SQL builder](https://zenskar.com/docs/20260301/product-modules/usage/how-to-guides/billable-metrics.md#move-a-billable-metric-to-the-sql-builder)
>
> This guide shows how to move a billable metric from the visual builder to the SQL builder.

## One number for each customer and period

Zenskar runs a billable metric for one customer and one period at a time. Each run gives one number, or no value. A run gives no value when, for example, no rows match and the query does not replace an empty result with 0.

A query preview runs the same calculation for a customer and a period that you choose.

> [card] [Preview a billable metric](https://zenskar.com/docs/20260301/product-modules/usage/how-to-guides/billable-metrics.md#preview-a-billable-metric)
>
> This guide shows how to run a billable metric for a customer and a period before you save it.


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