Logic Functions
Spectre logic functions control conditional branching in expressions. These functions can be used in all Spectre file types.
Note that case() and switch() can be used to code neater calculations. See Expression Tips.
case(condition1 : boolean, result1 : any,
condition2 : boolean, result2 : any,
... fallback : any) : any
For example:
`case(false, "good", true, "ok", "bad")` returns ok
`case(calc("Value") < 1500, "Low", calc("Value") < 1650, "Medium-Low", calc("Value") < 1800, "Medium-High", "High")` when Value is 1550, result is Medium-Low
add "Age Group" ```case(value("Age") < 1, "Infant", value("Age") < 5, "Toddler", value("Age") < 13, "Child", value("Age") < 20, "Teen", value("Age") < 30, "Young Adult", value("Age") < 65, "Adult", "Older Adult")```
adds the Age Group text column to the cBase based on the Age
For an application, see Binning Data in
if(condition : boolean,
if_true : any,
if_false : any) : any
For example:
`if(true, 20, 30)` returns 20
`if(false, 50, 100)` returns 100
`if(is_null(value("Last Well Visit Date")), null, date_number(today()) - date_number(value("Last Well Visit Date")))` returns null if the last well visit date is null, otherwise returns the number of days between today and the last well visit date.
TIP: A deeply nested if (that is an if within an if within an if and so on) can be replaced with a switch() expression for a more efficient operation.
match(target : any, value1 : any, value2 : any,... : any) : integer
For example:
`match("alice", "alice", "bob", "charlie")` returns 1
`match("fred", "alice", "bob", "charlie")` returns 0
`match("charlie", "alice", "bob", "charlie")` returns 3
switch(value : any,
candidate1 : any, result1 : any,
candidate2 : any, result2 : any,...
fallback : any) : any
For example:
`switch(value("thing"), 1, "foo", 2, "bar", 4, "baz", "unknown")`
result is `if(value("thing")=1, "foo", if(value("thing")=2, "bar", if(value("thing")=4, "baz", "unknown")))`.
`switch(4, 1, "foo", 2, "bar", 4, "baz", "unknown")`
result is "baz".
calc "Timeframe_anchor" ```
switch(param("Timeframe"),"7 Days",
offset(calc("the_anchor"),"day",-7),"14 Days",
offset(calc("the_anchor"),"day",-14),"21 Days",
offset(calc("the_anchor"),"day",-21),
offset(calc("the_anchor"),"day",-30))```
sets the Timeframe_anchor using an offset based on the value of the Timeframe parameter.
See also: Alphabetized Functions for Spectre.