Logic in Expressions
Summary
- AdaptableQL supports Logic in Expressions - this comes in 2 formats:
IF- uses a ternary operatorCASEstatements (designed when using multiple clauses)
Expressions can also include custom Logic.
This can be provided in 2 ways:
- via ternary logic (essentially an IF function)
- using CASE Statements
IF Function
Expressions can include conditional logic via the IF function.
This wraps a ternary operator (which uses the ? sign) and is used for writing conditional expressions.
It is used in Logical Expressions and is made up of 3 parts
- a Boolean Expression to evaluate followed by a question mark (
?) - the return value if the expression is true, followed by a colon (
:) - the return value if the Expression is false
// Return 'Big' if more than 100 Comments, otherwise 'Small'
[Comments] > 100 ? 'Big' : 'Small'There is no limit on the number of Ternary Operators in an Expression - which enables multiple conditions:
// Return 'Big' if more than 100 Comments, 'Medium' if more than 50, otherwise 'Small'
[Comments] > 100 ? 'Big' : [Comments] > 50 ? 'Medium' : 'Small'- This demo contains a Calculated Column which is evaluated using Ternary Logic
Popularityexamines the number of stars inGithub Starsand returns an appropriate string value using the following logic:- Greater than 100,000 - "Very Popular"
- Greater than 30,000 - "Popular"
- Others - "Trending"
CASE Statements
Sometimes the logic required in an Expression will make the Ternary Operator feel verbose or unsuitable.
For this reason, AdapTableQL also offers switch-type logic common in many languages.
It is provided by CASE statements which take this form:
CASE [case_value]
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
ENDAs can be seen they are made up of (up to) 4 parts:
CASEkeyword followed - optionally - by the value being evaluated- Any number of conditions which each take the form
WHENxTHENy - A final, optional,
ELSEto return the value if no conditions are met - An
ENDstatement which wraps up the section
CASE [day] WHEN 'Saturday' THEN 'weekend' WHEN 'Sunday' THEN 'weekend' ELSE 'workday' ENDAs noted, the initial evaluation after CASE is optional and can be omitted:
CASE WHEN [price] < 10 THEN 'low' WHEN [price] < 50 THEN 'medium' ELSE 'high' END- This demo contains 2 Calculated Columns which are evaluated using Case Statements
Popularityexamines the number of stars inGithub Starsand returns an appropriate string value using the following logic:- Greater than 100,000 - "Very High"
- Greater than 40,000 - "Quite High"
- Greater than 20,000 - "Low"
- Else - "None"
Filereturns the correct file type for theLanguagecolumn using the following logic:JavaScript- ".js"TypeScript- ".ts"- Else - ".html"