SegmentTraitsFormula Traits

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

  1. Navigate to Segment > Traits and click Create Trait
  2. Select Formula Trait as the type
  3. Choose the entity type this trait applies to (e.g., User)
  4. Write your expression in the formula editor
  5. Set the output data type (number or boolean)
  6. Configure the evaluation schedule
  7. 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

OperatorDescriptionExample
+Additiontrait_a + trait_b
-Subtractiontrait_a - trait_b
*Multiplicationtrait_a * 1.1
/Divisiontotal_revenue / order_count
%Moduloorder_count % 2

Comparison Operators

OperatorDescriptionExample
>Greater thanlifetime_value > 1000
>=Greater than or equalorder_count >= 5
<Less thandays_since_last_purchase < 30
<=Less than or equalavg_session_minutes <= 2
==Equal toorder_count == 0
!=Not equal totop_category != 'unknown'

Logical Operators

OperatorDescriptionExample
ANDLogical ANDlifetime_value > 1000 AND order_count > 5
ORLogical ORdays_since_last_purchase > 90 OR order_count == 0
NOTLogical NOTNOT is_enterprise

Functions

FunctionDescriptionExample
IF(condition, then, else)Conditional expressionIF(order_count > 0, total_revenue / order_count, 0)
COALESCE(a, b)Return first non-null valueCOALESCE(lifetime_value, 0)
ABS(x)Absolute valueABS(revenue_change)
ROUND(x, n)Round to n decimal placesROUND(avg_order_value, 2)
MIN(a, b)Smaller of two valuesMIN(score_a, score_b)
MAX(a, b)Larger of two valuesMAX(score_a, score_b)

Parentheses

Use parentheses to control evaluation order:

(total_revenue - total_refunds) / order_count

Examples

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 Account entity in a formula for the User entity.
  • 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