Have a fiscal quarter column, and I want to add a column that displays the previous quarter in the same row. I've done this successfully in tableau with logic like below.
IF RIGHT([current_qtr], 1) = "1"
THEN LEFT([current_qtr],2)+ STR(INT(MID([current_qtr], 3,2)) -1) +"Q4"
ELSE LEFT([current_qtr],5)+ STR(INT(RIGHT([current_qtr],1)) -1)
END
Example of expected output in snowflake
| current_qtr | previous_qtr | other columns |
|---|---|---|
| FY21Q1 | FY20Q4 | etc |
| FY21Q2 | FY21Q1 | etc |
When I try to do this in snowflake though I keep getting this "Numeric value '' is not recognized error instead. Any ideas how to fix? Below is what I attempted.
SELECT *,
CASE
WHEN SUBSTR(p.current_qtr,5,1) = 1
THEN SUBSTR(p.current_qtr,1,2) || TRY_CAST(SUBSTR(p.current_qtr,3,2) as INTEGER)-1 || 'Q4'
ELSE SUBSTR(p.current_qtr,5,1) || TRY_CAST(SUBSTR(p.current_qtr,3,2) as INTEGER)-1
End As prev_qtr
FROM table as p