Working with conditional formula (IF)
The IF function is one of the most popular Calconic functions. Testing whether conditions are true or false and making logical comparisons between expressions are common to many tasks.
You can write conditional formulas using our Basic Formula Editor or Rich Formula Editor. The Rich Formula Editor lets you write your conditional formula in a few simple clicks. On the other hand, the Basic Formula Editor allows you to write more complex conditional formulas; however, you’ll need to know and understand the syntax of the conditional formula in question.
This tutorial explains:
- how to write conditional formulas using the Rich Formula Editor,
- how to understand the syntax of conditional formulas and write them manually,
- and how to check a few conditions at a time (for example, applying pricing tiers on a final price).
Writing a conditional formula using the Rich Formula Editor:
- Select the Formula field you want to apply a conditional formula to.
- Click the Open Rich Text Editor button below the Basic Formula Editor (input field for formula line).
- In the formula editor window, type in IF to use this function or click on the IF button located below the input field.
- Type your condition into the IF input field.
- Type what happens if the condition is met in the THEN input field.
- Type what happens if the condition is not met in the ELSE input field.
- You can also use results from previous formulas or values entered by the user. You can see the available options below the formula input field.
Understanding the syntax of a conditional formula and writing it manually
The syntax of a conditional formula is the following:
((IF)?(THEN):(ELSE))
For example:
((#1 - #2 < 500)?(10):(20))
Translating that to English, it would read as follows: IF result of #1 - #2 is less than 500, THEN return 10, or ELSE return 20.
The Logical operators that can be used in a conditional formula:
You can also use the and & or operators to create your conditional formulas. Here’s how it works:
Let’s say you have a calculator consisting of two input fields (ID #1 and #2) and one formula field (ID #3), and you want to check if the values entered to both input fields are not less than 10. And only if the values are not less than 10, only then do you want your formula fields to show, let’s say, 20. And if at least one of the entered values is smaller, then your calculator should show 0. In this case, your formula would look like the following:
((#1 > 10 and #2 > 10)?(20):(0))
Translating that to English, it would read: IF value entered to input field #1 is bigger than 10 and value entered to input field #2 is bigger than 10, THEN return 20, or ELSE return 0.
And if you want to check if at least one condition is met, you can use the OR operator.
The comparison operators that can be used in a conditional formula:
== - Both numbers or inputs are equal.
!= - The value is not equal to the value it is compered to
> - The value is greater than the value it is being compared to.
< - The value is less than the value it is being compared to.
>= - The value is greater or equal to the value it is being compared to.
<= - The value is less than or equal to the value it is being compared to.
A workaround for the ELSEIF function, or checking a few conditions in one formula
Sometimes you might need to check a few conditions for the same input field (when applying differed pricing tiers for different quantities and so on). Although Calconic doesn’t support ELSEIF or IF nesting functions, you can achieve the same result using multiple conditional formulas multiplied with each other.
For example:
((#1 < 10)?(150):(1)) * ((#1 >= 10 and #1 < 20)?(250):(1)) *
((#1 >= 30)?(350):(1))
How this formula reads:
- IF the number entered to input field ID #1 is lower than 10 THEN return 150 or ELSE return 1. Then we multiply this conditional formula by another condition formula that reads:
- IF the number entered to input field ID #1 is greater or equal to 10 and lower than 20 THEN return 250 or ELSE return 1. This formula is also multiplied by another conditional formula:
- IF the number entered to input field ID #1 is greater than 30 THEN return 350 or ELSE return 1.
All these conditional formulas return 1 when the condition is not met, which doesn't influence the final result, and the result is shown for the conditional formulas where the condition is met.