Formula Traits
Formula Traits let you combine existing traits using arithmetic and logical expressions. Instead of querying the warehouse directly, formula traits operate on the materialized results of other traits — making them fast to evaluate and easy to compose.
When to Use Formula Traits
Formula Traits are ideal when you want to:
- Compute a ratio from two existing traits (e.g.,
average_order_value = total_revenue / order_count) - Create a boolean flag based on thresholds (e.g.,
is_high_value = lifetime_value > 1000) - Combine multiple traits into a composite score
- Apply arithmetic transformations (e.g., convert cents to dollars, normalize a metric)
If the base metrics don’t exist as traits yet, create them first as SQL Traits or Aggregation Traits, then use a formula trait to combine them.
Creating a Formula Trait
- Navigate to Segment > Traits and click Create Trait
- Select Formula Trait as the type
- Choose the entity type this trait applies to (e.g., User)
- Write your expression in the formula editor
- Set the output data type (number or boolean)
- Configure the evaluation schedule
- Click Save
Formula Syntax
Formulas are written as expressions that reference other traits by their slug name. The formula editor provides autocomplete for available trait slugs.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | trait_a + trait_b |
- | Subtraction | trait_a - trait_b |
* | Multiplication | trait_a * 1.1 |
/ | Division | total_revenue / order_count |
% | Modulo | order_count % 2 |
Comparison Operators
| Operator | Description | Example |
|---|---|---|
> | Greater than | lifetime_value > 1000 |
>= | Greater than or equal | order_count >= 5 |
< | Less than | days_since_last_purchase < 30 |
<= | Less than or equal | avg_session_minutes <= 2 |
== | Equal to | order_count == 0 |
!= | Not equal to | top_category != 'unknown' |
Logical Operators
| Operator | Description | Example |
|---|---|---|
AND | Logical AND | lifetime_value > 1000 AND order_count > 5 |
OR | Logical OR | days_since_last_purchase > 90 OR order_count == 0 |
NOT | Logical NOT | NOT is_enterprise |
Functions
| Function | Description | Example |
|---|---|---|
IF(condition, then, else) | Conditional expression | IF(order_count > 0, total_revenue / order_count, 0) |
COALESCE(a, b) | Return first non-null value | COALESCE(lifetime_value, 0) |
ABS(x) | Absolute value | ABS(revenue_change) |
ROUND(x, n) | Round to n decimal places | ROUND(avg_order_value, 2) |
MIN(a, b) | Smaller of two values | MIN(score_a, score_b) |
MAX(a, b) | Larger of two values | MAX(score_a, score_b) |
Parentheses
Use parentheses to control evaluation order:
(total_revenue - total_refunds) / order_countExamples
Average Order Value
Divide total revenue by order count, handling the division-by-zero case.
IF(order_count > 0, total_revenue / order_count, 0)- Input traits:
total_revenue(number),order_count(number) - Output type: Number
Is High-Value Customer
Flag customers whose lifetime value exceeds a threshold and who have made multiple purchases.
lifetime_value > 1000 AND order_count >= 3- Input traits:
lifetime_value(number),order_count(number) - Output type: Boolean
Engagement Score
Combine multiple behavioral metrics into a weighted composite score.
ROUND((order_count * 10) + (days_active * 2) + (referral_count * 25), 0)- Input traits:
order_count(number),days_active(number),referral_count(number) - Output type: Number
Revenue Per Day
Calculate revenue efficiency by dividing lifetime value by account age.
IF(days_since_signup > 0, ROUND(lifetime_value / days_since_signup, 2), 0)- Input traits:
lifetime_value(number),days_since_signup(number) - Output type: Number
Churn Risk Flag
Identify customers who haven’t purchased recently and have declining engagement.
days_since_last_purchase > 60 AND avg_session_minutes < 2 AND order_count > 0- Input traits:
days_since_last_purchase(number),avg_session_minutes(number),order_count(number) - Output type: Boolean
Normalized Spend (0-100 Scale)
Normalize a trait value to a 0-100 range using min-max scaling. This example assumes you have pre-computed min_ltv and max_ltv traits at the account or global level.
IF(max_ltv > min_ltv, ROUND((lifetime_value - min_ltv) / (max_ltv - min_ltv) * 100, 0), 50)- Input traits:
lifetime_value(number),min_ltv(number),max_ltv(number) - Output type: Number
Dependency Management
Formula traits create dependencies on the traits they reference. SignalSmith tracks these dependencies and ensures:
- Evaluation order — Referenced traits are always evaluated before the formula trait that depends on them
- Cascade updates — When a referenced trait is re-evaluated, dependent formula traits are automatically queued for re-evaluation
- Circular dependency detection — If you create a formula that would form a circular reference (A depends on B, B depends on A), SignalSmith rejects the formula with an error
You can view the dependency graph for any trait in its detail page under the Dependencies tab.
Null Handling
If any referenced trait has a null value for a given entity, the formula result will also be null unless you explicitly handle it with COALESCE:
-- Without null handling: returns null if either trait is null
total_revenue / order_count
-- With null handling: returns 0 if either trait is null
COALESCE(total_revenue, 0) / IF(COALESCE(order_count, 0) > 0, order_count, 1)Limitations
- Formula traits can only reference traits on the same entity type. You cannot reference a trait from the
Accountentity in a formula for theUserentity. - Formulas do not support string operations (concatenation, substring, etc.). For string manipulation, use a SQL Trait.
- The maximum formula length is 1,000 characters.
- Formula traits cannot reference other formula traits more than 5 levels deep.
Next Steps
- SQL Traits — Write custom SQL for computations that don’t fit formulas
- Aggregation Traits — Visual builder for base metrics
- Trait Evaluation — How dependency ordering and scheduling works