The values of a Derived Field are not raw usage data values but always the result of a calculation or function performed on Meter Data Fields, Custom Fields, or Timestamp Fields as source fields. Derived Fields add a very powerful extra dimension of flexibility for setting up the data on which your usage-based pricing plans will be based.
This topic offers some examples of Derived Field calculations. Please run through the first example as a starter, which shows both the Console setup for the field and the JSON body schema to create the field using a Create Meter API call. Other examples simply give the calculation for the Derived Field:
In addition to concrete examples, the final section in this topic:
Links you out to a topic on supported syntax and functions for your Derived Field calculations.
Explains how to reference Custom Fields and system timestamp fields in your Derived Field calculations.
Tip: String Derived Fields? Yes, you can also create non-numeric, string Derived Fields which reference Data Fields. See Example 5 below for some examples of operations to create string Derived Fields.
Important: Updating the Calculation? When you use a Derived Field, the calculation is performed at the time the data is ingested and is persisted in the platform at the resultant values. This means that if you update the calculation at a later date:
Any calculated values for data ingested before the update remain the same.
Any calculated values for data ingested after the update will be the result of the new calculation.
Suppose you run a cloud service which offers your end customers computing capabilities. To implement this service, you want to charge for Products that your end customers consume on the basis of a usage measure in Gigabyte-seconds of processing. To set this up, you can first create two Data Fields of type Measure on a Product Meter that ingest raw data measures:
A Memory MB field, which represents the amount of memory in megabytes allocated to the compute function.
A Duration field, which represents the length of time the compute function ran for in milliseconds.
You can then create a Derived Field of type Measure on your Product Meter that:
Converts the input to Memory MB, which is in Megabytes (unit: MiBy), to Gigabytes simply by dividing by 1024.
Converts the input to Duration, which is in milliseconds (unit: ms), to seconds simply by dividing by 1000.
Multiplies the memory allocation in GiBy by the duration value in secs to give a measure in Gigabyte-seconds (unit: GiBy.s):
You can now use this Meter's GB Second Derived Field as the basis for setting up an Aggregation for pricing your Product Plans.
Important! Note that when you reference Data Fields in a Derived Field calculation, you must use the Code of the referenced fields.
If you want to create this Meter with the same Data and Derived Fields using an API call, please see our API Reference Create Meter page. Here's the JSON using this call for the example:
1{2"data": [3{4"id": "3ddfea4b-XXXX-467d-XXXX-b6YYYYYYYYYf",5"version": 1,6"productId": "1b364e59-e32b-4fbf-bc1e-91b5fc4872e5",7"name": "Compute Execution",8"code": "compute_execution",9"dataFields": [10{11"category": "MEASURE",12"code": "memory_mb",13"name": "Memory MB",14"unit": "MiBy"15},16{17"category": "MEASURE",18"code": "duration_ms",19"name": "Duration",20"unit": "ms"21}22],23"derivedFields": [24{25"category": "MEASURE",26"code": "gb_second",27"name": "GB second",28"unit": "GiBy.s",29"calculation": "(memory_mb/1024)*(duration_ms/1000)"30}31]32}33]34}
If you have a Data Field on your Meter that measures gigabytes stored - field code is gigabytes_stored
- and you require a megabytes stored measure, then you can create a Measure Derived Field and use:
Calculation:
gigabytes_stored*1024
Suppose you have a Meter with two Measure Data Fields for:
Gigabytes stored: gigabytes_stored
Kilobytes stored: kilobytes_stored
If you require a megabytes stored measure that combines these two, then you can create a Measure Derived Field and use:
Calculation:
(gigabytes_stored*1024) + (kilobytes_stored/1024)
Suppose you have a Meter with two Measure Data Fields for:
Stored volume of data in megabytes at point of last backup: lastbackup_size
Duration since last backup was made in minutes: lastbackup_duration
If you require a MB-mins measure, you can create a Measure Derived Field and use:
Calculation:
lastbackup_size * lastbackup_duration
You can create string Derived Fields which reference Data Fields. Here's some examples.
Suppose you have two string Data Fields on a Meter:
Location, which is a Where field used to identify the country of residence of your customers.
Type, which is a What field used to identify the type of data check you are performing on your customers.
However, you want a field that combines both these string data types - Location Type. To do this, you can use the +
operator in a Derived Field calculation to concatenate the two string fields:
If you need to cast a numeric Data Field as a string to give a string Derived field, you can do this:
Tip: String Manipulation Functions? You can also use String functions in your Derived Field calculations - see String Functions for a listing and explanations.
Suppose you offer a service to manage and process orders and deliveries for online retailers. Your basic pricing model uses a tiered structure on number of orders/deliveries you handle per billing period. You also want to include some product add-ons, which customers can select for on an order-by-order basis. Pricing for an add-on will use a simple unit price model - each billing period customers are charged a fixed price per add-on consumed.
To meet this pricing use case, you can create a Derived Field that uses the 3-argument ? :
ternary operator. This operator evaluates a boolean statement as first argument and yields one value if the statement is TRUE
, another value if the statement is FALSE
.
In this example:
We have a string Data Field on a Meter of type What called packaging_design.
We then create a Derived Field called package_addon
of type Measure, which uses a calculation that references this Data Field:
Calculation:
packaging_design=="yes"?1:0
When an order is placed for processing that takes the add-on, yes
is entered in the packaging
_
design
field.
The calculation evaluates the first argument to return TRUE
or FALSE
:
If the value of packaging_design
is yes
, then the statement is TRUE
and the value of package_addon
is 1
.
If the value of packaging_design
is not yes
, then the statement is FALSE
and the value of the package_addon
is 0
.
For pricing, we can now set up an Aggregation that uses the package_addon
Derived Field as its target field, and then select SUM for the Aggregation method.
We can extend this example to show how Derived Fields can be used for use cases where you want to price for a combination of product add-ons. Suppose you want to price when an order you process has opted for an express package delivery and gift packaging as a combined product add-on. You can set up two string Data Fields on a Meter of type What:
packaging_express
packaging_gift
You can then use a calculation for a Derived Field of type Measure called package_addon2
that references these two Data Fields and employs a nested ternary operator:
Calculation:
packaging_express=="yes"?(packaging_gift=="yes"?1:0):0
How does this work?
The nested expression in brackets is evaluated, which will result in 1
only if packaging_gift
has a value of yes
.
The main expression is then evaluated and results in 1
only if packaging_express
has a value of yes
and the nested expression has returned a value of 1
. If the nested expression has returned 0
, then the main expression will return 0
, regardless of whether packaging_express
has a value of yes
or any other value - that is, is either TRUE
or FALSE
.
We can therefore see how this form of calculation is useful when we want to price for multiple product addons as a bundled addon - only if all referenced Data Fields have a value of yes
does the Derived Field have a value of 1
returned.
Suppose you want to implement usage-based pricing against an end-customer Account which accommodates changes in the number of users on that Account during the billing period. This kind of billing use case is often referred to as "seat-based pricing" and can adjust billing charges in the following way:
If members are added to the Account at any time within the billing cycle, the Account will be charged a prorated amount based on the percentage of the billing cycle left at the time each member was added.
If members are removed from the Account at any time within the billing cycle, the Account will be credited in a similar, prorated way.
You can implement seat-based pricing using a Derived Field calculation that uses the 3-argument ? :
ternary operator. This operator evaluates a boolean statement as first argument and yields one value if the statement is TRUE
, another value if the statement is FALSE
.
Taking a worked example to illustrate how this calculation works, let's suppose you've put an end-customer Account on a Product Plan for monthly billing and you want to charge for seat-based pricing for September, a 30-day month:
The Account starts the month at 30 seats.
1 seat is removed after 8 days.
1 seat was added back in after 20 days.
This means the seat count is back at 30 at the end of the month.
The charge rate is $10 per month per seat on the Account.
For this example:
The seat-adjustments
Data Field is used to log either the removal or addition of a single seat and sends in an appropriate value for these events throughout the billing period:
A value of -1 when a seat is removed.
A value of +1 when a seat is added.
We then create a Derived Field called seat_proration
of type Measure that references the seat_adjustments
field and exploits system ts
(timestamp) fields:
Calculation:
seat_adjustments * ((ts <= ts.startOfMonth) ? 1 : (ts <= ts.endOfMonth) ? 1 * (((ts.endOfMonth - ts))/(ts.endOfMonth - ts.startOfMonth)) : 0)
For our worked example, we can first manually calculate what the charge amount should be:
We have an unchanged seat count of 29 for the full month that results in a charge due of: 29 x $10 = $290
We have a seat count of one that will be charged at the prorate of $10/30 per day for:
8 days worth: 8 x $10/30 = $2.67
10 days worth: 10 x $10/30 = $3.33
Total charge: $290 + $2.67 + $3.33 = $296.
We can now breakdown the seat_proration
Derived Field calculation in the context of the worked example and see how the calculation is evaluated to give a correct prorated seat count value for the month:
(ts <= ts.startOfMonth) ? 1
If the ts
(timestamp) for the seat_adjustments
event is less than or equal to the start of the month (ts.startOfMonth)
, the result is 1.
(ts <= ts.endOfMonth) ? 1 * (((ts.endOfMonth - ts))/(ts.endOfMonth - ts.startOfMonth)) : 0
In our example, the first seat_adjustments
event was sent at the end of the 8th day of the Month and this expression checks whether ts
is less than or equal to the end of the month (ts.endOfMonth)
which it is. If true, it calculates a weighted value based on the position of ts
within the month:
For the first seat_adjustment
event, -1 was sent at the end of the 8th day so the prorated value returned would be -0.73, which represents the reduction in charge due for the 22 days of the month remaining with one seat removed.
For the second seat_adjustment
event, +1 was sent at the start of the 21st day so the prorated value returned would be 0.33, which represents the additional charge due for the 10 days of the month remaining with one seat added back in.
This results in a net adjustment of: -0.73 + 0.33 = -0.4
To implement pricing on the Account, we create another Data Field on another Meter: start_seatcount
. This sends a measure of the number of seats registered for the Account at the start of each billing period - in our example, the start of each month. With these two Data Fields in place, we can now add Aggregations to arrive at a pricing metric that accommodates seat count adjustments:
A simple SUM Aggregation targeting the start_seatcount
Data Field.
A simple SUM Aggregation targeting the seat_proration
Derived Field.
A Compound Aggregation: adjusted_seatcount
, that references these two fields and adds them together them using the following calculation:
Calculation:
(aggregation.start
_
seatcount
+ aggregation.seat_proration)
We can now use the adjusted_seatcount
Compound Aggregation to price up Product Plans for those Accounts to which we want to apply seat-based pricing.
In our concrete example, the result of the calculation used for the adjusted_seatcount
Compound Aggregation will be: 30 - 0.4 = 29.60. Therefore the charges for the Account will be: $10 x 29.60 = $296, which agrees with what we calculated manually.
In summary, the Derived Field for implementing seat-base pricing calculates a prorated value based on the position of a timestamp (ts) within a month provided by the remove/add usage event.
You are not restricted to referencing Meter Data Fields in your Derived Field calculations. You can also reference Custom Fields and Timestamp Fields, which greatly extends the scope of measures available as the basis for Aggregations used for pricing your Product Plans.
For details of the syntax and supported functions and operators in calculations, see Calculations Syntax and Supported Functions/Operators.
The values of Data Fields in the measurement are available as variables, and can be referenced by the Data Field code, for example:
myDataField
Any Custom Fields you have defined at either the Organizational Level or at the level of an individual item can be referenced in a Derived Field calculation. The default value defined at the Organization level will be used if there is not a specific value defined on the relevant item. For example, the Custom Field value defined on the specific meter referenced by the measurement will be used if it exists, otherwise the default value defined for a Meter at the Organizational level will be used:
organization.<fieldName>
meter.<fieldName>
product.<fieldName>
account.<fieldName>
accountPlan.<fieldName>
plan.<fieldName>
planTemplate.<fieldName>
Tip: Global vs. Product Meters? Note that Global Meters - those not associated with a Product - cannot resolve Product-scoped Custom Fields, therefore the default will be used. Meters that are associated to a Product can use the Product-specific Custom Field value if it is defined.
You can reference system variable Timestamp Fields in your Derived Field calculations. All timestamps are numeric values representing the appropriate date/times in Epoch milliseconds:
ts
ts.startOfMonth
ts.endOfMonth
ts.startOfMonthUTC
ts.endOfMonthUTC
If the “ets” field - End Timestamp - is specified in the measurement, the following are also available:
ets
ets.startOfMonth
ets.endOfMonth
ets.startOfMonthUTC
ets.endOfMonthUTC
Next: Usage Data: Aggregations
Login to the Support portal for additional help and to send questions to our Support team.