React Embedded AnalyticsExpressions

Expressions allow new columns to be generated off of the source data.

const expressionFields = [
    {
        id: 'revenue',
        isMeasure: false,
        expression: {
            operator: 'multiply',
            inputs: [
                { id: 'sales.unitPrice' },
                { id: 'sales.quantity' },
            ],
        },
    },
];

Expression fields are defined on the Data Source as an array of AgExpressionFieldDefinitions. Each expression field consists of an field definition (similar to a normal Field Definition), along with the expression itself.

See Below for the expression field API.

Calculated Columns & Measures Copy Link

An Expression Field can produce two different outputs:

  1. Calculated Column - For example Profit = Revenue - Cost.
  2. Measure - For example Total Profit = SUM(Revenue - Cost).

Calculated Columns Copy Link

Calculated columns produce multiple outputs for multiple inputs, and can therefore be aggregated in the UI.

You must specify isMeasure: false for Calculated Columns.

const expressionFields = [
    {
        id: 'profit',
        isMeasure: false,
        expression: {
            operator: 'multiply',
            inputs: [
                {
                    operator: 'subtract',
                    inputs: [
                        { id: 'sales.unitPrice' },
                        { id: 'sales.unitCost' },
                    ],
                },
                { id: 'sales.quantity' },
            ],
        },
    },
    // ...
];

Measures Copy Link

Measure columns produces a single output when given multiple inputs and therefore cannot be aggregated in the UI.

You must specify isMeasure: true for Measures.

const expressionFields = [
    {
        id: 'totalProfit',
        isMeasure: true,
        expression: {
            operator: 'subtract',
            inputs: [
                { id: 'sales.unitPrice', aggregation: 'sum' },
                { id: 'sales.unitCost', aggregation: 'sum' },
            ],
        },
    },
    // ...
]

Expression Types Copy Link

An expression is of one of three types:

  1. Function - A function applies an operator to one or more inputs.
  2. Value - A value is a fixed value (e.g. number, string, etc.).
  3. Field - A field refers to another field (either in the source data, or another expression field).

Function Expression Copy Link

const functionExpression = {
    operator: 'multiply',
    inputs: [
        { id: 'sales.unitPrice' },
        { value: 100 },
    ],
};
functionExpressionCopy Link
AgFunctionExpression
A function expression applies an operator to one or more inputs. Each input is another expression - a function, value or field.

See Below for the list of function expression operators.

Value Expression Copy Link

const valueExpression = {
    type: 'number',
    value: 100,
};
valueExpressionCopy Link
AgValueExpression
A value expression is a fixed value (e.g. number, string, etc.).

Field Expression Copy Link

const fieldExpression = {
    id: 'sale.profit',
    aggregation: 'sum,
};
fieldExpressionCopy Link
AgFieldExpression
A field expression refers to another field (either in the source data, or another expression field).

API Copy Link

Expression Field Definition Copy Link

formatCopy Link
TFormat
The format type of the field (provides default formatting, etc.). If not provided, will be inferred from the expression.
isMeasureCopy Link
boolean
Whether this expression creates a Measure or a Calculated Column. A Calculated Column produces a list of values, e.g. quantity × cost, even without a grouping. A Measure produces a single value e.g. SUM(quantity).
expressionCopy Link
AgExpression<TExpressionOperatorTypes>
The expression for the field.
string
Field ID.
string
Display name.
descriptionCopy Link
string
Field description. Displayed in the Field Panel
boolean
Set to true to hide from being selected in the UI. Field can still be used for joins.
serializerCopy Link
AgFieldSerializer<InferDataTypeFromFormat<TFormat, TFormats>>
Optional. How the field values will be serialized into state. Defaults to format serializer.
deserializerCopy Link
AgFieldDeserializer<InferDataTypeFromFormat<TFormat, TFormats>>
Optional. How the field values will be deserialized from state. Defaults to format deserializer.
valueFormatterCopy Link
AgFieldValueFormatter<InferDataTypeFromFormat<TFormat, TFormats>, TOptions>
Optional. How the field values will be displayed. Defaults to format value formatter.
blankValueCopy Link
string
Optional. How blank values will be displayed. Defaults to format blank value.
optionsCopy Link
TOptions
Optional. Will be passed to the value formatter.

Function Expression Operators Copy Link

AgFunctionExpression<this, 'add'>
Add two values together.
  • ['number', 'number'] => 'number'
  • ['string', 'string'] => 'string'
  • subtractCopy Link
    AgFunctionExpression<this, 'subtract'>
    Subtract the second value from the first.
  • ['number', 'number'] => 'number'
  • multiplyCopy Link
    AgFunctionExpression<this, 'multiply'>
    Multiply two values together.
  • ['number', 'number'] => 'number'
  • divideCopy Link
    AgFunctionExpression<this, 'divide'>
    Divide the first value by the second.
  • ['number', 'number'] => 'number'
  • moduloCopy Link
    AgFunctionExpression<this, 'modulo'>
    Remainder of the first value divided by the second.
  • ['number', 'number'] => 'number'
  • equalsCopy Link
    AgFunctionExpression<this, 'equals'>
    Are the two values equal?
  • ['number', 'number'] => 'boolean'
  • ['date', 'date'] => 'boolean'
  • ['datetime', 'datetime'] => 'boolean'
  • ['boolean', 'boolean'] => 'boolean'
  • ['string', 'string'] => 'boolean'
  • notEqualCopy Link
    AgFunctionExpression<this, 'notEqual'>
    Are the two values not equal?
  • ['number', 'number'] => 'boolean'
  • ['date', 'date'] => 'boolean'
  • ['datetime', 'datetime'] => 'boolean'
  • ['boolean', 'boolean'] => 'boolean'
  • ['string', 'string'] => 'boolean'
  • lessThanCopy Link
    AgFunctionExpression<this, 'lessThan'>
    Is the first value less than the second?
  • ['number', 'number'] => 'boolean'
  • greaterThanCopy Link
    AgFunctionExpression<this, 'greaterThan'>
    Is the first value greater than the second?
  • ['number', 'number'] => 'boolean'
  • lessThanOrEqualCopy Link
    AgFunctionExpression<this, 'lessThanOrEqual'>
    Is the first value less than or equal to the second?
  • ['number', 'number'] => 'boolean'
  • greaterThanOrEqualCopy Link
    AgFunctionExpression<this, 'greaterThanOrEqual'>
    Is the first value greater than or equal to the second?
  • ['number', 'number'] => 'boolean'
  • AgFunctionExpression<this, 'and'>
    Are both the values true?
  • ['boolean', 'boolean'] => 'boolean'
  • AgFunctionExpression<this, 'or'>
    Are either of the values true?
  • ['boolean', 'boolean'] => 'boolean'
  • AgFunctionExpression<this, 'not'>
    Negates the value.
  • ['boolean'] => 'boolean'
  • negateCopy Link
    AgFunctionExpression<this, 'negate'>
    Negates the value.
  • ['number'] => 'number'
  • AgFunctionExpression<this, 'if'>
    If the first value is true then return the second value, else return the third value.
  • ['boolean', 'string', 'string'] => 'string'
  • ['boolean', 'number', 'number'] => 'number'
  • ['boolean', 'boolean', 'boolean'] => 'boolean'
  • ['boolean', 'date', 'date'] => 'date'
  • ['boolean', 'datetime', 'datetime'] => 'datetime'
  • AgFunctionExpression<this, 'in'>
    Is the first value in any of the subsequent values?
  • ['string', ...'string'] => 'boolean'
  • ['number', ...'number'] => 'boolean'
  • ['boolean', ...'boolean'] => 'boolean'
  • ['date', ...'date'] => 'boolean'
  • ['datetime', ...'datetime'] => 'boolean'
  • isTrueCopy Link
    AgFunctionExpression<this, 'isTrue'>
    Is the value true?
  • ['boolean'] => 'boolean'
  • isFalseCopy Link
    AgFunctionExpression<this, 'isFalse'>
    Is the value false?
  • ['boolean'] => 'boolean'
  • isNullCopy Link
    AgFunctionExpression<this, 'isNull'>
    Is the value null?
  • ['string'] => 'boolean'
  • ['number'] => 'boolean'
  • ['boolean'] => 'boolean'
  • ['date'] => 'boolean'
  • ['datetime'] => 'boolean'
  • isNotNullCopy Link
    AgFunctionExpression<this, 'isNotNull'>
    Is the value not null?
  • ['string'] => 'boolean'
  • ['number'] => 'boolean'
  • ['boolean'] => 'boolean'
  • ['date'] => 'boolean'
  • ['datetime'] => 'boolean'
  • datediffCopy Link
    AgFunctionExpression<this, 'datediff'>
    Return the number of units defined by the first value that are between the second and third values.
  • ['string', 'date', 'date'] => number
  • ['string', 'datetime', 'datetime'] => number
  • Supported units:
  • Milliseconds - 'millisecond' | 'ms'
  • Seconds - 'second' | 'ss' | 's'
  • Minutes - 'minute' | 'mi' | 'n'
  • Hours - 'hour' | 'hh'
  • Days - 'day' | 'dy' | 'y'
  • Weeks - 'week' | 'ww' | 'wk'
  • Weekdays - 'weekday' | 'dw' | 'w'
  • Months - 'month' | 'mm' | 'm'
  • Quarters -'quarter' | 'qq' | 'q'
  • Years - 'year' | 'yyyy' | 'yy'
  • Days of year - 'dayofyear'