How do i avoid the divide by 0 error?
# suiteanalytics
j
How do i avoid the divide by 0 error?
g
You could see if NULLIF() works for your use case.
j
i tired it couldnt figure out where exactly to add nullif. i kept getting invalid expression
g
Could try to wrap the entire denominator under the
/
with NULLIF:
Copy code
(
  CASE
    WHEN TO_CHAR({trandate},'MM/YY')=TO_CHAR({today},'MM/YY')
    THEN {amount} 
    ELSE 0
  END
)
/
NULLIF(
        ( 
          ( 
            CASE
              WHEN TO_CHAR({trandate},'MM/YY')=TO_CHAR(TO_DATE(ADD_MONTHS({today},-1)),'MM/YY')
              THEN {amount}
              ELSE 0
            END
          )
          +
          ( 
            CASE
              WHEN TO_CHAR({trandate},'MM/YY')=TO_CHAR(TO_DATE(ADD_MONTHS({today},-2)),'MM/YY')
              THEN {amount}
              ELSE 0
            END
          )
          +
          (
            CASE
              WHEN TO_CHAR({trandate},'MM/YY')=TO_CHAR(TO_DATE(ADD_MONTHS({today},-3)),'MM/YY')
              THEN {amount}
              ELSE 0
            END
          )
        )
        /3
,0)
Could also try changing the
ELSE 0
in the
CASE
statements in the denominator under the
/
to
ELSE NULL
but I’m not exactly sure if that will allow for all the addition. I can’t remember if
NULL + 5
would equal 5 or NULL. 🤣
s
this makes me want to cry