Docs Menu

Docs HomeView & Analyze DataAtlas Charts

Calculate New Data Fields

On this page

  • Considerations
  • Create a Calculated Field
  • Edit a Calculated Field
  • Remove a Calculated Field

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

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

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:

  1. In the corner of the Fields pane, click Add Field.

  2. Select Calculated.

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

  4. Enter the Value Expression using simple expression language or Expression Operators.

  5. Click Save Field.

The calculated field appears in italics in the Fields pane.

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.

Click "Add Field", enter a field name and simple expressions definition, then click "Save Field".
click to enlarge

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

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"] }

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" }

You can modify the definition of a calculated field.

  1. In the Fields pane, click the Ellipsis (...) next to the name of the calculated field you want to modify.

  2. Select Modify field.

  3. Update the Value Expression.

  4. Click Save Field.

If you are using the calculated field in a chart, the chart refreshes to reflect the new calculated field definition.

Click the ellipsis next to the field name, then click "Modify field".
click to enlarge

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:

  1. In the Fields pane, click the Ellipsis (...) next to the name of the calculated field you want to remove.

  2. Select Remove field.

Click the ellipsis next to the field name, then click "Remove field".
click to enlarge
←  Reshape Your DataConvert Field Data Types →