This function allows to choose one out of two expressions based on a condition. It provides a functionality similar to the ... ? ... : ... operator known from some programming languages.

Unlike a case or match statement, the if() can be embedded into other functions and expressions.

ParameterTypeRequiredDefault ValueDescription
asstringoptional[a]_if The name of the output destination field.
condition[b]expressionrequired  The conditional expression to evaluate.
elseexpressionrequired  The alternative statement to execute.
thenexpressionrequired  The consequent statement to execute.

[a] Optional parameters use their default value unless explicitly set.

[b] The parameter name condition can be omitted.

Hide omitted argument names for this function

Show omitted argument names for this function

The behavior of:

logscale
if(cond, then=onTrue, else=onFalse, as=y)

is equivalent to a case expression:

logscale
case {
  test(cond)
| y := onTrue;
  y := onFalse
}

In particular:

  • The interpretation of condition is the same as the expression argument of test().

  • The evaluation of then / else arguments is the same as the evaluation of the right-hand side of :=.

The if() function:

  • is a simplified version of the case / match based alternative.

  • can be used as an expression inside other expressions.

if() Examples

Click + next to an example below to get the full details.

Add a Field Based on Values of Another Field - Example 1

Add a Field Based on Values of Another Field - Example 2

Add a Field Based on Values of Another Field - Example 3

Calculate a Percentage of Successful Status Codes Over Time

Categorize Errors in Log Levels

Categorize errors in log levels using the in() function in combination with if()

Convert Timestamps Based on Accuracy

Determine a Score Based on Field Value

Set a Field Value Based on Tag Value

Use Multiple if() Functions