+

# Calculated values

Calculated values give you the option to add measures that were missing in the original data. They can be saved and restored within the report. This feature is available for `"json"`, `"csv"` and `"api"` data source types. Each calculated measure is defined inside the `measures` property of the SliceObject.

This guide contains the following sections:

## Calculated measure properties

The calculated measure has the following properties:

## Examples

The example below illustrates how to define a calculated measure with the minimum price for each color:

```slice: {
rows: [
// Fields
],
measures: [
// Other measures
{
formula: "min('Price')",
uniqueName: "Min Price",
},
],
}```

See the example on JSFiddle.

### Using multiple measures in a formula

The next example shows how to define a formula with two measures:

```slice: {
rows: [
// Fields
],
measures: [
// Other measures
{
uniqueName: "Total Price",
formula: "sum('Price') * sum('Quantity')",
},
{
uniqueName: "Top Category",
formula: "average('Price') < 4000 and sum('Quantity') > 100",
},
],
}```

Try a live sample on JSFiddle.

### Applying conditional formatting to calculated values

You can apply conditional formatting to your calculated measure. For example:

```report: {
slice: {
rows: [
// Fields
],
measures: [
// Other measures
{
uniqueName: "Top Category",
formula: "average('Price') < 4000 and sum('Quantity') > 100",
},
],
},
conditions: [
{
formula: "#value = 1",
measure: "Top Category",
format: {
backgroundColor: "#66FF99",
color: "#000000",
fontFamily: "Arial",
fontSize: "12px",
},
},
],
}```

See a live demo on JSFiddle.

### Using negative numbers in a formula

To use a negative number in a formula, enclose the number in parentheses. For example:

```slice: {
rows: [
// Fields
],
measures: [
// Other measures
{
formula: "(-1) * sum('Price')",
uniqueName: "Inverted Price",
},
],
}```

See the full code on JSFiddle.

### Specifying number formatting for calculated values

You can specify number formatting for your calculated measure. For example:

```report: {
slice: {
rows: [
// Fields
],
measures: [
// Other measures
{
uniqueName: "% of Total Price",
formula: "percent('Price') / 100",
format: "price",
},
],
},
formats: [
{
name: "price",
decimalPlaces: 2,
isPercent: 2,
},
],
}```

Open the example on JSFiddle.

### Using the individual property

With the `individual` property, the formula is calculated using raw values. For example, the formula `sum('Price') * sum('Amount')` will be calculated like this:

• If the `individual` is `true`: 174 * 36 + 225 * 44
• If the `individual` is `false`: (174 + 225) * (36 + 44)

The following code snippet demonstrates how to use the `individual` property:

```slice: {
rows: [
// Fields
],
measures: [
// Other measures
{
uniqueName: "Overall price",
formula: "sum('Price') * sum('Amount')",
individual: true,
},
],
}```

Check out a live sample on JSFiddle.

For more examples of adding calculated values, see the Examples page.

## The full list of operators and functions for calculated values

Below is a list of all operators and functions supported in `formula`:

• `+` - arithmetic addition operator. Syntax: `a + b`.
• `-` - arithmetic subtraction operator. Syntax: `a - b`.
• `*` - arithmetic multiplication operator. Syntax: `a * b`.
• `/` - arithmetic division operator. Syntax: `a / b`.
• `^` - arithmetic power operator. Syntax: `a^2`.
• `<` - comparison less than operator. Syntax: `a < b`.
• `<=` - comparison less than or equal operator. Syntax: `a <= b`.
• `>` - comparison greater than operator. Syntax: `a > b`.
• `>=` - comparison greater than or equal operator. Syntax: `a >= b`.
• `==` - comparison equal operator. Syntax: `a == b`.
• `!=` - comparison not equal operator. Syntax: `a != b`.
• `or` - logical OR operator. Syntax: `a or b`.
• `and` - logical AND operator. Syntax: `a and b`.
• `if` - conditional operator. If the `else` statement is not specified, cells that do not meet the `condition` will be empty. Syntax: `if(condition, then, else?)`.
• `abs` - function that returns the absolute value of a number. Syntax: `abs(number)`.
• `round` – function that rounds the value to the specified number of decimal places. If the number of decimals is not specified, the value is rounded to the nearest integer. Syntax: `round(number, decimals?)`. See a live demo on JSFiddle.
• `min` - function that returns the minimum value. Syntax: `min(number1, number2)`.
• `max` - function that returns the maximum value. Syntax: `max(number1, number2)`.
• `isNaN` - function that checks whether the value is not a number. Syntax: `isNaN(value)`.
• `!isNaN` - function that checks whether the value is a number. Syntax: `!isNaN(value)`.

## Add calculated values using the Field List

Use `Add calculated value` in the Field List to add the calculated measure at runtime.

## Calculated values via API

Calculated measures can be defined within the report or added via the addCalculatedMeasure() API call. To remove a calculated measure use the removeCalculatedMeasure() API call. removeAllCalculatedMeasures() removes all calculated measures.