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
- Navigate to Sources > Computed Attributes and click Create Computed Attribute
- Select Formula Computed Attribute as the type
- Choose the entity type this computed attribute 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 computed attributes by their slug name. The formula editor provides autocomplete for available computed attribute 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 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
Accountentity in a formula for theUserentity. - 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
- SQL Computed Attributes — Write custom SQL for computations that don’t fit formulas
- Aggregation Computed Attributes — Visual builder for base metrics
- Computed Attribute Evaluation — How dependency ordering and scheduling works