Billable metrics
Walkthroughs
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.
Map of concepts
Map of concepts
Billable metrics: usage aggregated for billing
A billable metric is 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. For example, you send usage events to the usage-event collectionapi_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.
This conceptual diagram shows how usage events and two synced data sources each become a table with one column for each field.
A usage-event collection is the same row-and-column representation as the synced usage data of a synced data source, and a billable metric reads both in the same way.
- It filters the rows.
- It matches each row to a customer.
- It keeps the rows of the period.
- It aggregates the rows, such as with a count or a sum.
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. See Metered usage: quantity from a billable metric and Metered usage.
A billable metric is built in the visual builder or the SQL builder:
The name of a billable metric can change. A billable metric can be deleted, even when a pricing preset uses it. Changing or deleting a billable metric can affect contracts and invoices.
See Create a billable metric.
Visual builder: a billable metric in four steps
The visual builder is the wizard 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:
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, where you change the query itself.
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.
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.
Each calculation type applies to the rows that the filters, the customer column, and the date column keep.
See 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:
The visual builder 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.
A query names the tables that it reads:
A query on a usage-event collection or on synced usage data uses ClickHouse SQL. 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.
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, the product, or the period.
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:
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 withCOALESCE(SUM(<column>), 0).
No value and 0 can lead to different charges. See Quantity strategies: consumption or allocation.
See Preview a billable metric.