Standard Expressions

Summary

  • A Standard Expression is evaluated against each row in isolation and returns a single value.
  • The return value of a Standard Expression can be of 2 types:
    • any (i.e. scalar) data type - used in the Calculated Column Module.
    • boolean (i.e. true / false ) - most common type of Expression, available in many Modules (e.g. Alerts, Grid Filter)
  • Although Expressions have potential to be extremely complex, essentially they consist of just (up to) 4 elements:
    • Columns - e.g. BloombergBid, MarkitBid
    • Expression Functions (e.g. MIN) - some of which can be displayed as Operators (e.g. <)
    • Logical Operators - (enable multiple clauses via AND or OR)
    • Input Values - e.g. 50

A Standard Expression is evaluated against each row in isolation and returns a single value.

The return value can be (conceptually) divided in 2 groups:

  • scalar (i.e. any data type)
  • boolean (ie. true / false) - returns true for each row which passes all conditions in the Expression

Standard Expressions are used in these Modules:

ModuleUsageType
AlertsTriggers an Alert when data change matches a RuleBoolean
Calculated ColumnEvaluates the value displayed in the columnScalar
ExportCustom Reports export only those rows returned by the QueryBoolean
Flashing CellWhether to Flash the Cell (or Row)Boolean
Format ColumnSets whether or not to show the Format ColumnBoolean
Grid FilterReturns rows which match the true / false conditionBoolean
Plus MinusManages evaluation of Custom Nudge ValuesBoolean

Find Out More

AdapTableQL provides more complex Aggregated Expressions where the evaluation includes multiple rows

Elements in Standard Expressions

Expressions can be very complex but essentially they are made of up 4 distinct elements:

  1. Columns (or Fields)
  2. Expression Functions (and Operators)
  3. AND / OR Logical Operators
  4. Input Values

Hint

  • An Expression can include as many functions, operators, columns and values as are required
  • Not every element is present in every Expression

In this page we will analyse each of these 4 Elements in turn, marking each type of Element with the relevant number when providing code example.

Note

  • As we will see, strictly speaking there are really only 2 Elements: Expression Functions and Input Values
  • Columns & Logical Operators are also Expressions Functions, but treated separately here for easier comprehension

Let's start with a small Example which shows all 4 elements of an Expression.

Imagine we want a Grid Filter so that AdapTable only displays rows where either of 2 clauses is true:

  • whichever value was the smaller in the BloombergBid and MarkitBid columns, was greater than 50
  • the Currency column has a value of Dollar

That could be written with the following (boolean) Expression:

 MIN([BloombergBid], [MarkitBid]) > 50 OR [Currency] = 'USD'
AdapTableQL: Standard Expressions
Fork
  • This demo shows a number of different Standard Expressions:
  • We have 4 Boolean Expressions applied to different AdapTable Modules:
    • A Grid Filter is filtering the grid for rows where Language is JavaScript
    • The Export module contains an MIT Report that exports rows where License is MIT License
    • A Format Column Condition is applied to rows where Github Stars > 50,000
    • An Alert fires when the Rule is triggered that Open Issues > 50
  • Additionally it contains a Scalar Expression applied to a Calculated Column - Full Github

Now lets at look of each of these 4 Elements in turn.

1. Columns

This refers to any Adaptable Column whose value is required in the Expression.

Note

  • A Column is optional in Standard Expressions - but one is generally used
  • Some Observable and Aggregation Expressions require a Column in order for the Expression to be valid

When evaluating the Expression, AdapTableQL automatically retrieves the value for the column for each row.

Columns are typically referenced by providing the columnId in square brackets:

 // Reference Price Column (in Scalar Expression)
 [Price] * 100
 // Reference Currency Column (in Boolean Expression)
 [Currency] = 'USD'

There is no limit to the amount of Columns that can be included in an Expression:

 // Multiply 3 columns together
 MUL([OrderCost] , [ItemCost], [PackageCost])

Columns can be used in Scalar Expressions:

 // Order Change (minus combined total of PackageCost and OrderCost and Invoiced Cost)
 [OrderChange] - ([PackageCost] + [OrderCost] + [InvoicedCost])

Or in Boolean Expressions:

 // Order Change (minus combined total of PackageCost and OrderCost) differs to Invoiced Cost
 [OrderChange] - ([PackageCost] + [OrderCost]) != [InvoicedCost]

Using COL Function

Columns are actually specialised Expression Functions made more readable for user convenience.

Therefore they can alternatively be referenced using the COL function with the Column name in parentheses:

 // Reference Price Column using COL keyword
 COL("Price") * 100
 // Reference Currency Column using COL keyword
 COL("Currency") = 'USD'
Deep Dive

Referencing Columns in Expressions

Using Column Friendly Names

By default, wherever an Expression is referenced in AdapTable but is not being directly edited - e.g. when displayed in the Settings Panel - the Column's Header is always used.

Hint

If this is not required behaviour, set displayColumnFriendlyNamesForExpressions to false in Expression Options

displayColumnFriendlyNamesForExpressions

Default: true
Boolean
Reference a Column's Header in all Expression overviews (instead of ColumnId)

Fields

It is possible, if required, to reference a Field instead of a Column.

A Field is a row data item which is present in the data source, but which is not also a Column.

 // use FIELD keyword to reference Price item in data source, even though its not a Column
 FIELD('Price') * 100

Note

With Fields you have to explicitly use the FIELD Expression Function - the square brackets shortcut is not available

2. Expression Functions

At the core of each Expression are Expression Functions.

These are useful functions that are shipped with AdapTableQL which cover a multitude of use cases.

Find Out More

Feedback

If an Expression Function you would like to use is missing, please email Adaptable Tools Support with suggested details

Almost every Expression includes an Expression Function - though sometimes, as we will see below - they can be look like simple Operators.

Caution

Observable and Aggregation Expressions require Expression Functions in order to be valid

There is no limit on the number of Expression Functions allowed in an Expression.

For example this basic Boolean Expression contains 2 Expression Functions:

(It also contains 2 Columns, the OR Logical Operator, the > Operator and 2 Input Values)

  • STARTS_WITH Boolean Expression Function
  • MIN Scalar Expression Function
 STARTS_WITH([col1], 'ABC') OR MIN([col2], [col3]) > 100

Expression Functions are capitalised and some will require one or more arguments for evaluation.

For example to return the average from 3 numbers you can write:

 AVG(5, 12, 21)

Typically Function arguments are dynamic, receiving a current grid cell value (via a Column) as seen above:

 AVG([bloombergBid], [markitBid], [indicativeBid])

There are many String manipulation functions available:

 // Return the first non-null column value in a list
 COALESCE([bloombergPrice], [indicativePrice], [markitPrice])
 // Concatenate employee's name after changing case
 CONCAT( LOWER([employee_first_name]), UPPER([employee_last_name]) )

And numeric functions:

 // Return highest of 4 columns
 MAX ([ItemCost], [OrderCost], [InvoicedCost], ([PackageCost]*10))

And date functions:

 // Use Day diffing and adding functions
 DIFF_DAYS(CURRENT_DAY(), ADD_DAYS([OrderDate],5) ) > [ChangeLastOrder]

 // Combine Date functions with Ternary Logic
 [ShippedDate] > ADD_DAYS([OrderDate] , 21) ? 'Delayed' : 'On time'

Expression Functions can be combined with Logical Operators:

 // Country contains 'United' OR Currency is Dollar
 CONTAINS([Country], 'United') OR [Currency] = 'USD'

Expression Function arguments can themselves be Expression Functions

Caution

However, you must make sure that the Expression Function returns the correct Data Type

So to run a Scalar Expression which will return a date 5 days from now you can:

  • use the ADD_DAYS function which takes 2 arguments (a date and a number)
  • use the CURRENT_DAY function as the 1st argument
  • use an input (or Column) value as the 2nd argument
 // Return the Date, 5 days from now
 ADD_DAYS(CURRENT_DAY(), 5)

And to turn this into a Boolean Expression:

 // Is TradeDate in next 5 days
 ADD_DAYS(CURRENT_DAY(), 5) < [TradeDate]

Hint

In addition to System Expressions Functions, Developers can provide Custom Expression Functions if required

Operator Shortcuts

AdapTable provides a number of of operator shortcuts for the most commonly used Expression Functions.

Note

  • Similar to Columns, Operators are actually convenient wrappers around Expression Functions
  • For instance + is a short-hand for the ADD function and > is a convenient way of using GT

These can conceptually be divided into 2 groups:

  • Boolean operators - typically used to compare the 2 parts of an Expression's clause

    The most commonly boolean operators used are: =, !=, >, >=, < and <=.

    // Use Price Column with GreaterThan Operator
    [Price] > 100
    // Use Currency Column with Equals Operator
    [Currency] = 'USD'
  • Scalar operators - typically used to perform maths on 2 (or more) elements in a clause

    The most commonly used scalar operators are: +, -, *, /, and ^.

    // PackageCost plus OrderCost
    [PackageCost] + [OrderCost]
    // OrderChange minus PackageCost 
    [OrderChange] - [PackageCost]

Because operators are convenient wrappers around Expression Functions, the 4 Expressions above can be rewritten using Functions in place of the operator:

 GT([Price], 100)
 EQ([Currency], 'USD')
 ADD([PackageCost], [OrderCost])
 SUB([OrderChange], [PackageCost])

Expression Functions can therefore be combined with Operators if required.

 // Return any row where the Greater of Price and Cost is over 100
 MAX([Price], [Cost]) > 100

Advanced Expression Functions

AdapTable provides some Expression Functions which are designed to be used in more advanced use cases:

  • QUERY - enables Named Queries to be referenced in an Expression:

     // Reference the "Big Orders" Named Query
     QUERY("Big Orders")
  • VAR - supports custom values in Expressions:

     // Reference the VAR named 'VAT' and pass in the Country column as an argument
     VAR("VAT",[country]) > 1
  • IF - enables ternary logic to be used in Expressions:

    // Return 'Big' if more than 100 Comments, otherwise 'Small'
     [Comments] > 100 ? 'Big' : 'Small'
  • CASE - faciliates more complex logic in Expressions:

    // If the Day column contains "Saturday" then return "weekend", otherwise return "workday"
      CASE [day] WHEN 'Saturday' THEN 'weekend' ELSE 'workday' END
    
    

Find Out More

Read more about each of these more complex Expression Functions in the Guide to Advanced Expressions

3. AND / OR Logical Operators

All the examples so far have contained one clause to be evaluated by AdapTableQL.

However multiple clauses can be provided - most typically in a Boolean Expression - via logical operators.

Note

Logical operators are themselves Expression Functions that we treat separately here for comprehension purposes

There are 2 logical operators available when connecting clauses in Expressions:

  • The AND logical operator - requires that both clauses in the Expression are true:

    // Check Price is over 100 AND the Currency is Dollar
     [Price] > 100 AND [Currency] = 'USD'
  • The OR logical operator - requires that either clause in the Expression is true:

    // Check Price is over 100 OR the Currency is Dollar
     [Price] > 100 OR [Currency] = 'USD'

Using Parentheses

There is no limit on the number of clauses in an Expression, nor the number of AND or OR functions.

Where there are multiple clauses it is often advisable to provide parentheses.

Hint

  • Depending on the complexity of the Expression parentheses might be required
  • In any case it is good practice always to use them for (human) readability purposes
 // Check Price is over 100 AND either Currency is Dollar or Package Cost is less than Order Cost
 ([Price] > 100) AND (([Currency] = 'USD') OR ([ItemCost] < [OrderCost]))
AdapTableQL: Multi-Clause Boolean Expressions
Fork
  • This example includes similar Boolean Expressions to those in the demo above but they all contain multiple clauses joined by AND or OR
    • A Grid Filter is filtering the grid for rows where Language is JavaScript and either Github Watchers < 20500 or Github Stars > 8500
    • A custom report in Export - MIT JavaScript - exports rows where License is MIT License and Language is JavaScript
    • A Format Column Condition is applied to rows where Github Stars > 50,000 or License is MIT License
    • An Alert fires when the Rule is triggered that Open Issues > 50 and Language is JavaScript

Expand to see the multi-clause Boolean Expressions being used

4. Input Values

Input Values are hard-coded values provided by the User (rather than being dynamically derived by AdapTableQL from a Grid cell when the Expression evaluates).

 // Use Price Column with an Input Value
 [Price] > 100
 // Use Currency Column with an Input Value
 [Currency] = 'USD'

There is no requirement to use a Input value - two Columns can be compared instead:

 // Compare 2 columns (with no Input Value)
 [tradeDate] > [settlementDate]

There is no limit on the number of Input Values which can be used in an Expression:

 // Check Price is over 100 OR the Currency is Dollar
 [Price] > 100 AND ([Currency] = 'USD' OR [PackageCost] < 125)