Conditional Logic
Conditional logic allows your formulas to produce different results based on the data being processed. Precision Bridge provides several ways to express conditions, from inline ternary expressions to dedicated helper functions.
Inline If (Ternary Expression)
The most common conditional pattern uses Python's inline if/else syntax:
value_if_true if condition else value_if_false
The condition is evaluated first. If it is truthy, the expression before if is returned; otherwise, the expression after else is returned.
Examples
Map numeric priority to a label:
'High' if int([priority]) <= 2 else 'Normal'
Returns 'High' for priorities 1 and 2, 'Normal' for everything else.
Use a fallback when a field is null:
[fallback_email] if [email] is None else [email]
Returns the primary email if present, otherwise falls back to the alternative.
Conditional arithmetic:
[priority] + 1 if [priority] < 4 else [priority]
Increments priority unless it is already at 4 or above.
Truncate long text:
[description][:100] + '...' if len(str([description])) > 100 else [description]
Cuts the description to 100 characters and adds an ellipsis if it exceeds that length.
The if() Function
Precision Bridge provides a built-in if() function as an alternative to the ternary expression:
if(condition, value_if_true, value_if_false)
This can be more readable for simple conditions, especially when nested.
Examples
Map status to a boolean string:
if([status] == 'active', 'Yes', 'No')
Truncate with the if() function:
if(len([description]) > 100, [description][:100] + '...', [description])
Nested if() for multiple conditions:
if([priority] == 1, 'Critical', if([priority] == 2, 'High', if([priority] == 3, 'Medium', 'Low')))
Maps numeric priority values to labels through nested calls.
The if_null() Function
The if_null() function provides a concise way to handle null values:
if_null(test_value, value_if_null, value_if_not_null)
The first argument is tested. If it is None (null), the second argument is returned. Otherwise, the third argument is returned.
Examples
Provide a default for a null field:
if_null([assigned_to], 'Unassigned', [assigned_to])
Returns 'Unassigned' when the field is null, or the actual value when it is populated.
Use a fallback email:
if_null([email], [fallback_email], [email])
Falls back to an alternative email address when the primary is null.
Format a nullable field:
if_null([middle_name], '', ' ' + [middle_name] + ' ')
Returns an empty string for null, or wraps the middle name with spaces when present.
The first_with_value() Function
The first_with_value() function accepts any number of arguments and returns the first one that is not null and not an empty string:
first_with_value(value1, value2, value3, ...)
This is particularly useful for fallback chains.
Examples
Email fallback chain:
first_with_value([work_email], [personal_email], [fallback_email])
Tries each email in order, returning the first non-empty one.
Phone number fallback:
first_with_value([mobile_phone], [work_phone], [home_phone], 'No phone on file')
Falls through multiple phone fields, ending with a default string.
Description with fallback:
first_with_value([detailed_description], [short_description], 'No description')
Logical Operators
Use and, or, and not to combine multiple conditions:
| Operator | Description |
|---|---|
and |
True only if both sides are true |
or |
True if at least one side is true |
not |
Negates the condition |
Examples
Combined condition in a ternary:
'VIP' if [priority] == 1 and [category] == 'production' else 'Standard'
Returns 'VIP' only when both conditions are met.
Using or for alternative conditions:
'Urgent' if [priority] == 1 or [severity] == 'critical' else 'Normal'
Returns 'Urgent' if either condition is true.
Negation with not:
'Needs Review' if not [approved] else 'Approved'
Returns 'Needs Review' when approved is falsy.
Membership Testing with in
The in and not in operators check whether a value exists within a collection:
'Critical' if [priority] in [1, 2] else 'Normal'
Returns 'Critical' if priority is 1 or 2.
'Excluded' if [status] in ['closed', 'cancelled', 'resolved'] else 'Included'
Checks status against a list of values.
'Active' if [state] not in ['retired', 'decommissioned'] else 'Inactive'
Returns 'Active' for any state not in the exclusion list.
Chaining Conditions
For more complex logic, you can chain inline if/else expressions:
'Critical' if [priority] == 1 else 'High' if [priority] == 2 else 'Medium' if [priority] == 3 else 'Low'
This evaluates each condition in order and returns the first matching result. For deeply nested conditions, consider using the if() function or breaking the logic into variables with the Assign Variables step.
Truthiness
In conditions, the following values are considered falsy (evaluate to False):
- None / Null
- False
- 0 (integer zero)
- '' (empty string)
- [] (empty list)
- {} (empty dictionary)
Everything else is truthy. This means you can write concise checks like:
'Has description' if [description] else 'No description'
This returns 'No description' if the field is null, empty, or zero.
Next Steps
- Learn about collections in Lists, Dictionaries, and Indexing
- Explore comprehensions in Comprehensions
- See all available built-in functions in Available Built-in Functions
Comments
0 comments
Please sign in to leave a comment.