Calculate New Data Fields
You can combine data from one or more fields in your collection into a single calculated field. For example, you can:
Convert a field in hours to seconds or in degrees Farenheit to Celsius
Multiply a price field by a quantity field to create a total
Combine multiple line items within an array to calculate a total
Considerations
You can only create calculated fields from data within the same document.
You can't convert the type of a calculated field through the Atlas Charts interface. However, you can use Type Expression Operators in a calculated field's definition.
Once you create a calculated field, you can modify its definition but not its name. However, you can remove and recreate the field if you need to rename it.
Create a Calculated Field
You create a calculated field by combining the data of existing fields through simple expressions or MQL Expression Operators.
Note
The definition of a calculated field can contain either simple expression language or operator expression language. You can't use both simple and operator expression language in the same definition.
To create a calculated field:
In the corner of the Fields pane, click Add Field.
Select Calculated.
Enter the Field Name of the calculated field you want to define. You can specify a nested field by using dot notation. For example,
metadata.target
.Enter the Value Expression using simple expression language or Expression Operators.
Click Save Field.
The calculated field appears in italics in the Fields pane.
Simple Expressions
You can use the following simple expression language in a calculated field.
Expression Language | Example |
---|---|
Field names | orderTotal 'orderTotal' "orderTotal" |
Literal numbers | 2 0.5 |
Mathematical operators | + - * / |
Brackets | ( ) |
Whitespace that is outside of quoted strings is not included in the expression.
Example
The following examples are valid simple expressions to define a calculated field.
Add the bathrooms
field to the bedrooms
field:
bathrooms + bedrooms
Multiply the total amount
field by 1.1:
'total amount' * 1.1
Subtract 32
from the sensor.temp
field, then multiply by
5
and divide by 9
:
(sensor.temp - 32)*5/9
Operator Expressions
You can use Expression Operators to define more complex calculated fields.
Example
The following examples are valid operator expressions to define a calculated field.
Multiply the price
field by 0.075
:
{ $multiply: [ "$price", 0.075 ] }
Combine multiple line items fields in an array to calculate a total:
{ $reduce: { input: '$items', initialValue: 0, in: { $sum : ["$$value", { $multiply: ["$$this.price", "$$this.quantity"] } ] } } }
Tip
You can use a calculated field in the definition of another calculated field.
For example, if you create a salesTax
calculated field with the
following definition:
{ $multiply: [ "$price", 0.075 ] }
You can then create a totalCost
calculated field that uses the
salesTax
field in its definition:
{ $sum: ["$price", "$salesTax"] }
Literal Strings
You can set a calculated field's value to a string using the $literal
operator.
Example
The following field definition assigns a value of my string
:
{ $literal: "my string" }
Edit a Calculated Field
You can modify the definition of a calculated field.
In the Fields pane, click the Ellipsis (...) next to the name of the calculated field you want to modify.
Select Modify field.
Update the Value Expression.
Click Save Field.
If you are using the calculated field in a chart, the chart refreshes to reflect the new calculated field definition.
Remove a Calculated Field
When you remove a calculated field, Atlas Charts resamples the data source, updates the Fields pane, and refreshes your chart. If the calculated field was used in encodings or filters, it remains in the chart even after the field is removed from the Fields pane.
To remove a calculated field:
In the Fields pane, click the Ellipsis (...) next to the name of the calculated field you want to remove.
Select Remove field.