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 described inside a measure
object. It can have the following parameters:
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 the following operators: +
, -
, *
, /
and the following functions: isNaN()
, !isNaN()
(check a full list). Negative numbers can be used if enclosed in parentheses (check out the example). Other measures can be addressed using the measure’s unique name and aggregation function, for example sum("Price")
or max("Order")
. To see the list of supported aggregation functions for each data source type, refer to Flexmonster’s technical specifications.caption
optional – String. The measure’s caption.grandTotalCaption
optional – String. The measure’s grand total caption.active
optional – Boolean. 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.individual
optional – Boolean. Only for "csv"
and "json"
data source types. Defines whether the formula is calculated using raw values (true
) or using aggregated values (false
). Default value: false
. calculateNaN
optional – Boolean. Defines whether the formula is calculated using NaN values (true
) or using null values (false
). Default value: true
.format
optional – String. 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.This example on JSFiddle illustrates how to define a calculated measure with the minimum price for each color. The slice is defined like this:
slice: { rows: [ { uniqueName: "Color" } ], measures: [ { uniqueName: "Price", aggregation: "sum" }, { formula: 'min("Price")', uniqueName: "Min Price", caption: "Min Price", active: true } ] }
The next example illustrates how to define a calculated measure with a more complex formula. To highlight the values you can add conditional formatting for the Top Category
measure:
slice: { rows: [ { uniqueName: "Color" } ], measures: [ { uniqueName: "Price", aggregation: "sum" }, { uniqueName: "Top Category", formula: 'average("Price") < 4000 and sum("Quantity") > 100', caption: "Top Category", active: true } ] }, conditions: [ { formula: "#value = 1", measure: "Top Category", format: { backgroundColor: "#66FF99", color: "#000000", fontFamily: "Arial", fontSize: "12px" } } ]
A calculated measure can contain negative numbers in its formula. Enclose the numeric value in parentheses so that Flexmonster can process it. This example illustrates how negative numbers should be specified inside the formula
:
slice: { rows: [ { uniqueName: "Color" } ], measures: [ { uniqueName: "Price", aggregation: "sum", caption: "Price", }, { formula: "(-1) * sum('Price')", uniqueName: "Inverted Price", caption: "Inverted Price", active: true } ] }
See the full code on JSFiddle.
This JSFiddle example shows how to specify the number formatting for your calculated measure.
The individual
property allows the formula to be calculated using raw values. In the example the formula sum('Price') * sum('Amount')
will be calculated like this:
set individual: true
: 174 * 36 + 225 * 44
set individual: false
: (174 + 225) * (36 + 44)
The following report illustrates how to use the individual
property:
report: { dataSource: { data: [ { "Country" : "Canada", "Amount" : 36, "Price" : 174 }, { "Country" : "Canada", "Amount" : 44, "Price" : 225 } ] }, slice: { rows: [ { uniqueName: "Country" } ], measures: [ { uniqueName: "Price", aggregation: "sum", active: true }, { uniqueName: "Overall price", formula: "sum('Price') * sum('Amount')", individual: true, caption: "Overall price", active: true } ] } }
For more examples of adding calculated values, see the Examples page.
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)
.Use Add calculated value
in the Field List to add the calculated measure at runtime.
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.