Conditional Logic

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.

Purpose

Use the case function when you need to:

  • Assign different values based on performance tiers (e.g., set different bid multipliers for high, medium, or low ACOS).
  • Categorize items based on criteria (e.g., label campaigns as "High Performer", "Under Review", "Poor Performer").
  • Return default values when specific conditions aren't met.
  • Define complex calculations within let statements where the calculation method depends on certain conditions.

Syntax

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.
  • Arguments are separated by commas ,.

Evaluation Order

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.

Return Types

The case function can return values of different Data Types:

  • Number: All value arguments (including defaultValue) should resolve to numbers.
  • String: All value arguments should resolve to strings (enclosed in double quotes).
  • List: All value arguments should resolve to lists (less common, syntax depends on list implementation).

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.

PREVIOUS ARTICLE
NEXT ARTICLE