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

# Correct historical usage

## Plan for corrections

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

### Prerequisites

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

### Add the correction fields

> [!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](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#data-field) to the [data schema](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#data-schema) of the [usage-event collection](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#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 version of a usage event.

   See [Create a usage-event collection](https://zenskar.com/docs/20260301/product-modules/usage/how-to-guides/usage-events.md#create-a-usage-event-collection) or [Add a data field](https://zenskar.com/docs/20260301/product-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`.

## Replace usage events

Send a corrected version of a usage event. Build the billable metric so that it keeps only the latest revision of each usage event. This method works for every calculation.

### Prerequisites

* The usage-event collection has the correction fields. See [Plan for corrections](#plan-for-corrections).
* You need a role with the **Can Create Usage Event** permission to send usage events. You need the **Can Update Billable Metric** permission to change the billable metric. Both also need their read permissions. See [Permissions](https://zenskar.com/docs/20260301/product-modules/usage/reference/billable-metrics.md#permissions).

### Send the corrected usage event

> [!WARNING]
>
> A corrected usage event with a different `timestamp` can fall in another [billing period](https://zenskar.com/docs/20260301/product-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/product-modules/usage/how-to-guides/usage-events.md#send-usage-events-through-the-api).
2. Put the corrected values in the other data fields.

### Keep the latest revision in the billable metric

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

2. Change the query so that it numbers the revisions of each `event_id`, newest first. The query then 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
   ```

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

4. Click **SAVE CHANGES**.

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

## Offset usage events

Send a usage event that cancels out part of the usage that was recorded wrongly. This method works for billable metrics that add values up: sums, and counts that you rebuild as sums. It needs no change to a billable metric that already sums.

### Prerequisites

* You need a role with the **Can Create Usage Event** and **Can Read Usage Event** permissions. See [Permissions](https://zenskar.com/docs/20260301/product-modules/usage/reference/usage-events.md#permissions).

### Offset a sum

> [!WARNING]
>
> An offsetting usage event with a `timestamp` in another billing period changes that 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 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.

### Offset a count

A count cannot go down by adding usage events. Count with a sum of a number instead.

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

> [!WARNING]
>
> Usage events that arrived before you added `units` have no value for it. A sum over `units` leaves them out. Change the billable metric only from the first billing period in which every usage event has `units`.

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

## Correct usage in a data source

Correct usage that a [data source](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#data-source) reads by fixing the data in your system. When Zenskar picks up the fix depends on the access of the data source.

### Prerequisites

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

### Correct a remote data source

1. Fix the data in your system.

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

### Correct a synced data source

> [!WARNING]
>
> In a table with a [cursor column](https://zenskar.com/docs/20260301/product-modules/usage/glossary.md#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. The old row stays. Correct such a table by adding new rows with the revision method. You can also sync it without a cursor column.

1. Fix the data in your system.
2. Wait for the next scheduled sync, or start one. See [Sync now, pause, or resume syncing](https://zenskar.com/docs/20260301/product-modules/usage/how-to-guides/data-sources.md#sync-now-pause-or-resume-syncing).

In a table without a cursor column, each sync replaces the whole table in the synced usage data. The next sync therefore picks up the fix.

The revision method also works for the rows of a table of synced usage data. See [Replace usage events](#replace-usage-events).

## Recalculate charges

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

### Prerequisites

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

### Recalculate the invoices of a contract

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

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


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