Skip to main content

Plan for corrections

A usage event cannot be edited or deleted. To correct usage later, give each usage event an identity and a revision from the start. A billable metric can then keep only the latest revision of each usage event.

Prerequisites

Add the correction fields

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 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 version of a usage event.
    See Create a usage-event collection or 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.
  • 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.

Send the corrected usage event

A corrected usage event with a different timestamp can fall in another 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.
  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. If it uses the visual builder, move it first. See 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:
  3. Preview the billable metric for a period with a corrected usage event. See Preview a billable metric.
  4. Click SAVE CHANGES.
Charges that Zenskar already calculated change only when they are recalculated. See 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.

Offset a sum

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.
  2. Send 1 in units with every usage event.
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.
  1. Change the billable metric to use the calculation type SUM over units. See Edit a billable metric.
  2. 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 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.

Correct a remote data source

  1. Fix the data in your system.
Zenskar queries a remote data source each time it calculates. The next preview or calculation therefore uses the fix.

Correct a synced data source

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

Recalculate charges

A corrected usage event or a fixed data source changes a quantity only when Zenskar calculates it again. Zenskar calculates the quantity of a billing period when it builds the invoice of that period.

Prerequisites

Recalculate the invoices of a contract

  1. For each contract that the correction affects, regenerate its invoices. See Regenerate the invoices of a contract.
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 of the contract. See Invoicing configs: increases after approval.