Search code examples
looker-studio

google data studio conditional statement on calculated field


I am using google data studio and I am trying to create a calculated field using a case statement, but it is not working. The metric is sales. Bellow is the sample line:

CASE WHEN SUM(sales) > 350000 THEN 1 ELSE 0 END

The formula above gives me an error. If I do the formula bellow instead it doesn't give an error but the result is not what I expect:

CASE WHEN sales > 350000 THEN 1 ELSE 0 END

Any idea what could be wrong ? I tried to use if as well without success.


Solution

  • I would create two calculated fields. Using the first calculated field in the next one with the CASE statement:

    Calc Field 1: Summed Sales

    SUM(sales)  
    

    Calc Field 2: MyCaseField

    CASE WHEN Summed Sales > 350000 THEN 1 ELSE 0 END  
    

    From the Data Studio Docs:

    ...It's not possible to do math or use calculated field functions in a WHEN condition... ...You can work around this by first creating a calculated field with the desired formula. Then use that field in the CASE statement.

    Reference