The Weighted Sum aggregation type adds up the value of a defined event property and prorates it based on time used per period. This is especially handy for automated GB/second calculations, for instance.

Currently, prorating is available per second per period. Feel free to reach out if you need additional proration options, such as hourly rates.

WEIGHTED SUM calculation method

WEIGHTED SUM billable metric

Here is how you can create a weighted sum aggregation from the UI:

  1. Access the “Billable metrics” section via the side menu;
  2. Create a new billable metric;
  3. Define a name, a code and an optional description;
  4. Select “weighted sum” as the aggregation type;
  5. Define it this metric is metered or recurring;
  6. Define the property to aggregate;
  7. Define the weighted_interval used for time proration. Currently, Lago only supports seconds.
  8. Apply dimension groups if any; and
  9. Click “Add billable metric”.

This billable metric can be both metered or recurring.

Calculation example

Lago calculates the SUM(events.properties.property_name) / (DATEDIFF(SECOND, timestamp.event_1, timestamp.event_2) + 1) * (DATEDIFF(SECOND, 'period_start', 'period_end') + 1) for the two following events received.

//Event received #1
{
    "transaction_id": "transaction_1",
    "external_customer_id": "1",
    "timestamp": "2022-03-16T00:00:00Z",
    "code": "gb/s",
    "properties": {
        "gb": 20
    }
}

//Event received #2
{
    "transaction_id": "transaction_2",
    "external_customer_id": "1",
    "timestamp": "2022-03-17T00:00:00Z",
    "code": "gb/s",
    "properties": {
        "gb": 10
    }
}

In that case, with this aggregation type, Lago adds up the values of the gb property in the event payloads, but prorates it based on time used during the current period. Keep in mind that the entire month comprises 2,678,400 seconds. The first 20 GB has been used for a day (86,400 seconds), and the 30 GB (20 + 10) has been used for 15 days (1,296,000 seconds till the end of the month):

  • (20 / 2678400) x 86400 = 0.64516129032 GB/s
  • (30 / 2678400) x 1296000 = 14.5161290323 GB/s
  • 0.64516129032 + 14.5161290323 = 15.1612903226 GB/s