1. API reference
Connecting to Data Source
Configuring the component
  • Documentation for older versions
  • Table of contents

    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:

    Property/Type Description
    uniqueName
    String
    The measure’s unique name. This property will be used as an identifier for the measure inside Flexmonster and as an identifier to remove the measure via API.
    formula
    String
    Represents the formula. It can contain:
    • Operators and functions. Check out the full list.
    • Numbers. Negative numbers can be used as well (see an example).
    • Measures. A measure can be addressed using its unique name and an aggregation function. For example, sum("Price") or max("Order"). See a list of supported aggregations in Flexmonster's technical specifications.
    • Calculated measures. To address another calculated measure, specify its unique name. For example, "Formula #1".
    caption
    String
    optional The measure’s caption.
    grandTotalCaption
    String
    optional The measure’s grand total caption.
    active
    Boolean
    optional Indicates whether the measure will be selected for the report (true) or not (false). active: false can be useful if the measure has non-default properties, but should not be selected for the grid or the chart.
    Default value: true.
    individual
    Boolean
    optional Defines whether the formula is calculated using raw values (true) or using aggregated values (false). Only for "csv" and "json" data source types.
    Default value: false.
    calculateNaN
    Boolean
    optional Defines whether the formula is calculated using NaN values (true) or using null values (false).
    Default value: true.
    format
    String
    optional The name of the number formatting that will be applied to the measure. Measure values can be formatted according to the number formatting defined in the report. All available number formattings are stored in the formats array in the report. More information about the number formatting part of the report can be found in the number formatting article.

    Examples

    Adding a calculated measure

    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.

    add calculated value

    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.