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

# How-to guides: Correct historical usage

> This document shows how to correct usage that Zenskar already recorded or read, so that billable metrics give the right number, and how to recalculate the charges that it affects.

## Plan for corrections

A [usage event](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event) cannot be edited or deleted. To correct [usage](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage) later, give each usage event an identity and a revision from the start.

### Prerequisites

* To create a [usage-event collection](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event-collection), you need a role with the **Can Create Usage Event** and **Can Read Usage Event** permissions.
* To add [data fields](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-field) to an existing usage-event collection, you need a role with the **Can Update Usage Event** and **Can Read Usage Event** permissions.

### Steps

> [!WARNING]
>
> Usage events that arrive before you add the correction fields have no value for them. Add the fields before you send usage events.

1. Add two data fields to the [data schema](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-schema) of the usage-event collection, when you create the usage-event collection or later:

   * `event_id`, with the data type String. It holds an identifier that you choose for each usage event.
   * `revision`, with the data type Int64. It holds 1 for the first revision of a usage event.

   See [Create a usage-event collection](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/usage-events.md#create-a-usage-event-collection) or [Add a data field](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/usage-events.md#add-a-data-field).

2. In every usage event that your systems send, fill in `event_id` and `revision`.

A [billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#billable-metric) can then keep only the latest revision of each usage event. See [Replace usage events](#replace-usage-events).

## Replace usage events

This method works for every calculation.

### Prerequisites

* The usage-event collection has the correction fields. See [Plan for corrections](#plan-for-corrections).
* To send usage events, you need a role with the **Can Create Usage Event** and **Can Read Usage Event** permissions.
* To change the billable metric, you need a role with the **Can Update Billable Metric** and **Can Read Billable Metric** permissions.

### Steps

> [!WARNING]
>
> A corrected [usage event](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event) with a different `timestamp` can fall in another [billing period](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#billing-period). Zenskar then counts it next to the original. Keep the original `timestamp`.

1. Send a new usage event with the same `event_id`, `customer_id`, and `timestamp` as the usage event that it corrects. Set `revision` one higher. See [Send usage events through the API](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/usage-events.md#send-usage-events-through-the-api).

2. Put the corrected values in the other [data fields](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-field).

3. Open the [billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#billable-metric) in the [SQL builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#sql-builder). If it uses the [visual builder](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#visual-builder), move it first. 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).

4. Change the query so that it numbers the revisions of each `event_id`, newest first, and keeps only the first row of each `event_id`.

   The following query counts API calls with only the latest revision of each usage event:

   ```sql theme={null}
   SELECT COUNT(*) AS quantity
   FROM (
     SELECT
       *,
       ROW_NUMBER() OVER (PARTITION BY data.event_id ORDER BY data.revision DESC) AS row_number
     FROM "raw_metric_api_calls"
     WHERE customer_id = {{customer.external_id}}
       AND DATE(timestamp) >= DATE({{start_date}})
       AND DATE(timestamp) <= DATE({{end_date}})
   )
   WHERE row_number = 1
   ```

5. Preview the billable metric for a period with a corrected usage event. See [Preview a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#preview-a-billable-metric).

6. Click **SAVE CHANGES**.

The billable metric now keeps only the latest revision of each usage event. Charges that Zenskar already calculated change only when they are recalculated. See [Recalculate charges](#recalculate-charges).

## Offset usage in a sum

Send a [usage event](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event) that cancels out part of the usage that was recorded wrongly. A [billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#billable-metric) that already sums needs no change.

### Prerequisites

* You need a role with the **Can Create Usage Event** and **Can Read Usage Event** permissions.

### Steps

> [!WARNING]
>
> An offsetting usage event with a `timestamp` in another [billing period](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#billing-period) changes that billing period instead. Use a `timestamp` in the same billing period as the usage that it corrects.

1. Send a usage event with the same `customer_id`, a `timestamp` in the same billing period, and a negative value in the [data field](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-field) that the billable metric sums. For example, to remove 40 GB that were recorded twice, send `-40` in the field.

The sum of the billing period then includes the negative value. Charges that Zenskar already calculated change only when they are recalculated. See [Recalculate charges](#recalculate-charges).

## Offset usage in a count

A count cannot go down by adding [usage events](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event). Count with a sum of a number instead.

### Prerequisites

* To add a [data field](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-field), you need a role with the **Can Update Usage Event** and **Can Read Usage Event** permissions.
* To send usage events, you need a role with the **Can Create Usage Event** and **Can Read Usage Event** permissions.
* To change the [billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#billable-metric), you need a role with the **Can Update Billable Metric** and **Can Read Billable Metric** permissions.

### Steps

1. Add an Int64 data field, such as `units`, to the [usage-event collection](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event-collection). See [Add a data field](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/usage-events.md#add-a-data-field).

2. Send `1` in `units` with every usage event.

3. Change the billable metric to use the [calculation type](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#calculation-type) **SUM** over `units`. See [Edit a billable metric](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/billable-metrics.md#edit-a-billable-metric).

   > [!WARNING]
   >
   > Usage events that arrived before you added `units` have no value for it, and a sum over `units` leaves them out. Change the billable metric only from the first [billing period](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#billing-period) in which every usage event has `units`.

4. To remove a usage event that was recorded wrongly, send a usage event with `-1` in `units`, with the same `customer_id` and a `timestamp` in the same billing period.

Charges that Zenskar already calculated change only when they are recalculated. See [Recalculate charges](#recalculate-charges).

## Correct usage in a data source

### Prerequisites

* You need access to the system that the [data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-source) connects to.
* To sync a [synced data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#synced-data-source) right away, you need a role with the **Can Read Data Source** permission.

### Steps

1. Fix the data in your system.

   Zenskar queries a [remote data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#remote-data-source) each time it calculates. For a remote data source, the next preview or calculation uses the fix.

2. For a synced data source, wait for the next scheduled sync, or start one. See [Sync now, pause, or resume syncing](https://zenskar.com/docs/20260301/zenskar-modules/usage/how-to-guides/data-sources.md#sync-now-pause-or-resume-syncing).

   In a table without a [cursor column](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#cursor-column), each sync replaces the whole table in the [synced usage data](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#synced-usage-data). The next sync therefore picks up the fix.

   > [!WARNING]
   >
   > In a table with a cursor column, a sync copies a changed row only when its cursor value goes up. The sync then adds it next to the old row, and the old row stays. Correct such a table by adding new rows with the revision method, as in [Replace usage events](#replace-usage-events). You can also sync the table without a cursor column.

Charges that Zenskar already calculated change only when they are recalculated. See [Recalculate charges](#recalculate-charges).

## Recalculate charges

A corrected [usage event](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#usage-event) or a fixed [data source](https://zenskar.com/docs/20260301/zenskar-modules/usage/glossary.md#data-source) changes a [quantity](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#quantity) only when Zenskar calculates it again. Zenskar calculates the quantity of a [billing period](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#billing-period) when it builds the invoice of that billing period.

### Prerequisites

* The correction is in place, through usage events or a data source. See [Replace usage events](#replace-usage-events), [Offset usage in a sum](#offset-usage-in-a-sum), [Offset usage in a count](#offset-usage-in-a-count), or [Correct usage in a data source](#correct-usage-in-a-data-source).

### Steps

1. For each [contract](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#contract) that the correction affects, regenerate its invoices. See [Regenerate the invoices of a contract](https://zenskar.com/docs/20260301/zenskar-modules/contracts/how-to-guides/invoices.md#regenerate-the-invoices-of-a-contract).

Invoice regeneration recalculates the draft and upcoming invoices of an [active contract](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#active-contract). A change to a billing period whose invoice is already approved follows the [invoicing configuration](https://zenskar.com/docs/20260301/zenskar-modules/contracts/glossary.md#invoicing-configuration) of the contract. See [Invoicing configurations: increases after approval](https://zenskar.com/docs/20260301/zenskar-modules/contracts/concepts/contracts.md#invoicing-configurations-increases-after-approval).


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