Features
The case
function provides powerful if/then/else style logic directly within your Merch Jar Formulas. It allows you to evaluate a series of conditions and return a specific value based on the first condition that evaluates to true.
Use the case function when you need to:
The case
function takes multiple arguments, primarily pairs of conditions and resulting values, followed by a required else clause.
Standard Syntax:
case(when condition1 then value1, when condition2 then value2, ..., else defaultValue)
Shorthand Syntax
case(condition1 => value1, condition2 => value2, ..., else defaultValue)
Components:
case(...)
: The function call.when condition then value
: A pair where condition is a boolean expression (evaluates to true
/false
) and value is the result returned if that condition is the first one to be true. You can have multiple when/then pairs.condition => value
: An alternative, shorter syntax for when condition then value
.else defaultValue
: Required. This is the value returned if none of the preceding when
conditions evaluate to true.,
.The when
conditions are evaluated sequentially from left to right. The case
function stops and returns the corresponding value as soon as it encounters the first true condition. If no when
conditions are true, the defaultValue
from the else
clause is returned.
The case
function can return values of different Data Types:
defaultValue
) should resolve to numbers.Important: You generally cannot mix return types within a single case statement (e.g., return a Number in one then and a String in another). Furthermore, case typically cannot directly return Time Period values. Assign Time Period values directly or use let
if needed.
Examples
// --- Tiered Target ACOS based on current ROAS ---
let new_target_acos = case(
roas(30d) > 5.0 => 20%, // Excellent ROAS, allow lower Target ACOS
roas(30d) > 3.0 => 30%, // Good ROAS
roas(30d) > 1.5 => 40%, // Okay ROAS
else 50% // Poor ROAS, higher Target ACOS needed
);
// --- Assign a Performance Label (String) ---
let performance_tag = case(
orders(14d) > 5 and acos(14d) < 25% => "Top Performer",
orders(14d) = 0 and clicks(14d) > 10 => "Check Placement",
state = "paused" => "Paused",
else "Standard"
);
The case
function is essential for building nuanced logic and dynamic calculations within your Formulas.
© Merch Jar LLC