1
votes

I am trying to replicate the Excel formula IF(AND('cost'> 0, 'orders' = 0),"TRUE") in Google Data Studio using the CASE statement:

CASE
  WHEN COST > 0 AND ORDERS = 0 THEN "TRUE"
END

However, I receive the following error:

Only numeric literals are allowed if aggregated fields are used in CASE

Is there a way I can fix this?

3

3 Answers

2
votes

Summary

Use Either #1 or #2.
To elaborate on the difference between using "True" and TRUE, the Semantic Type of:

  • #1 is Boolean (TRUE or FALSE);
  • #2 is a Text field (note the quotes around "TRUE" or "FALSE"):

1) IF

The Conditional Function, IF, was introduced in the 11 Mar 2021 Update; thus, tweaking the IF function in the question now works as expected in Google Data Studio.
Note: The Calculated Field below is a Boolean field; thus, to create a Text field, replace TRUE with "TRUE" and replace FALSE with "FALSE":

IF(Cost > 0 AND Orders = 0, TRUE, FALSE)

2) CASE

Alternatively, the CASE statement proposed in the question should work as expected. That said, the CASE statement below explicitly states the ELSE clause.
Note: The Calculated Field below is a Text field; thus, to create a Boolean field, replace "TRUE" with TRUE and replace "FALSE" with FALSE:

CASE
  WHEN Cost > 0 AND Orders = 0 THEN "TRUE"
  ELSE "FALSE"
END

Editable Google Data Studio Report and a GIF to elaborate:

-1
votes

The Google Data Studio team released a new IF function and a more convenient way to use CASE.

The update: Release notes (11 Mar 2021)

IF(Actual Sales > Forecast Sales, Bonus * 1.2, Bonus)

The documentation is in IF.

If it's not working, then use the below CASE documentation link. It is supposed to appear on the sidebar just where the two CASE support articles are.

CASE Premium Status
    WHEN "Platinum" THEN CONCAT(Site URL, "platinum_welcome.html")
    WHEN "Gold" THEN CONCAT(Site URL, "gold_welcome.html")
    WHEN "Silver" THEN CONCAT(Site URL, "silver_welcome.html")
    ELSE CONCAT(Site URL, "welcome.html")
END

CASE (simple)

-1
votes

I don't think there is an IF statement in Google Data Studio; however, your CASE WHEN should work. I tried to reproduce your problem:

  1. I created a Google Sheet with two columns, cost and orders, and filled in some numbers
  2. I created a data source connected to this sheet
  3. I made a table in a Google Data Studio report using this data source, and added a calculated field with your exact formula
  4. It didn't give me any error messages and the field values are as expected

From the error message it feels like this is related to where your data source comes from.