r/tableau • • 3d ago

Convert SQL code to Tableau calculated field

Can someone please help me convert this case statement in SQL to something that will work in a Tableau calculated field?

NEW_FIELD = CASE WHEN OPENEDDATE BETWEEN CAST(DATEADD(DD,-90,@FROM) AS DATE) AND CAST(DATEADD(DD,-90,@TO) AS DATE) THEN '90 DAYS'

WHEN OPENEDDATE BETWEEN CAST(DATEADD(DD,-120,@FROM) AS DATE) AND CAST(DATEADD(DD,-120,@TO) AS DATE) THEN '120 DAYS' END

I created FROM and TO parameters to be used instead of the @FROM and @TO. Converting case statements to calculated fields is the last part of Tableau I'm still struggling with. Any help is appreciated.

3 Upvotes

3 comments sorted by

4

u/SantaCruzHostel 3d ago

NEW_FIELD =  IF [OPENEDDATE] >= DATEADD(day,-90,@FROM) AND [OPENDATE] <= DATEADD(day,-90,@TO) THEN '90 DAYS' ELSEIF [OPENEDDATE] >= DATEADD(day,-120,@FROM) AND [OPENDATE] <= DATEADD(day,-120,@TO) THEN '120 DAYS' ELSE 'Other' END

Written on my phone....syntax may not be 100%. Main difference here is that between is not a function in tableau so you need to do two comparisons to check if a date is bigger than X and smaller than Y.

1

u/Bstylee 3d ago

Couple of if/elif statements for the conditions should do fine. Whole calc is a bit weird… are you looking for orders within those windows from random date ranges that you are setting with parameters?

-3

u/WallStreetBoners 3d ago

Try using any AI tool