Skip to Content

Formula Computed Attributes

Formula Computed Attributes let you combine existing computed attributes using arithmetic and logical expressions. Instead of querying the warehouse directly, formula computed attributes operate on the materialized results of other computed attributes — making them fast to evaluate and easy to compose.

When to Use Formula Computed Attributes

Formula Computed Attributes are ideal when you want to:

  • Compute a ratio from two existing computed attributes (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 computed attributes into a composite score
  • Apply arithmetic transformations (e.g., convert cents to dollars, normalize a metric)

If the base metrics don’t exist as computed attributes yet, create them first as SQL Computed Attributes or Aggregation Computed Attributes, then use a formula computed attribute to combine them.

Creating a Formula Computed Attribute

  1. Navigate to Sources > Computed Attributes and click Create Computed Attribute
  2. Select Formula Computed Attribute as the type
  3. Choose the entity type this computed attribute 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 computed attributes by their slug name. The formula editor provides autocomplete for available computed attribute 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 computed attributes: 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 computed attributes: 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 computed attributes: 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 computed attributes: 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 computed attributes: days_since_last_purchase (number), avg_session_minutes (number), order_count (number)
  • Output type: Boolean

Normalized Spend (0-100 Scale)

Normalize a computed attribute value to a 0-100 range using min-max scaling. This example assumes you have pre-computed min_ltv and max_ltv computed attributes at the account or global level.

IF(max_ltv > min_ltv, ROUND((lifetime_value - min_ltv) / (max_ltv - min_ltv) * 100, 0), 50)
  • Input computed attributes: lifetime_value (number), min_ltv (number), max_ltv (number)
  • Output type: Number

Dependency Management

Formula computed attributes create dependencies on the computed attributes they reference. Zeotap tracks these dependencies and ensures:

  • Evaluation order — Referenced computed attributes are always evaluated before the formula computed attribute that depends on them
  • Cascade updates — When a referenced computed attribute is re-evaluated, dependent formula computed attributes 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), Zeotap rejects the formula with an error

You can view the dependency graph for any computed attribute in its detail page under the Dependencies tab.

Null Handling

If any referenced computed attribute 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 computed attribute is null total_revenue / order_count -- With null handling: returns 0 if either computed attribute is null COALESCE(total_revenue, 0) / IF(COALESCE(order_count, 0) > 0, order_count, 1)

Limitations

  • Formula computed attributes can only reference computed attributes on the same entity type. You cannot reference a computed attribute 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 Computed Attribute.
  • The maximum formula length is 1,000 characters.
  • Formula computed attributes cannot reference other formula computed attributes more than 5 levels deep.

Next Steps

Last updated on