IFNULL

Use IFNULL to replace NULL values in your data with a more suitable or meaningful value. This can be helpful when you want to avoid displaying NULLs in your charts or when you need a specific value for further calculations.

Sample usage

IFNULL(Discount, 0)

Syntax

IFNULL( input_expression, null_result )

Parameters

  • input_expression -- The expression to evaluate. You can use any valid expression as the input_expression.

  • null_result -- The value to return if input_expression is null. If input_expression is not null, IFNULL returns the input_expression. You can use any valid expression as the null_result.

Examples

The following examples show how to use IFNULL in aggregations and arithmetic calculations.

Include null rows in an average

Calculate the average discount given to your customers, including orders that received no discount. However, your application sometimes represents "no discount" as null rather than 0.

The formula AVG(Discount) won't include null discounts. You'll need to convert those nulls to zero. To do this, create a new field called, for example, Discount No Nulls:

IFNULL(Discount, 0)

This formula can be read, "If the Discount field is null, return 0, otherwise return Discount."

You can then calculate the average discount including orders with no discount:

AVG(Discount No Nulls)

Treat null values as zero in arithmetic

In Data Studio, when an arithmetic expression includes a NULL operand, the expression returns NULL, which charts display as missing data such as null, -, or No data. For example, the formula MetricA + MetricB returns NULL for any row where either MetricA or MetricB is NULL.

To treat NULL values as 0 so that the calculation returns a numeric result, wrap each nullable field in IFNULL:

IFNULL(MetricA, 0) + IFNULL(MetricB, 0)

To change only how final NULL results appear in a chart without changing how formulas are evaluated, in the chart's Properties panel, on the Style tab, set Missing data to an option such as Show "0". For more information, see Null values and missing data.