3 Replies Latest reply on Apr 30, 2018 4:46 AM by Kapil Dks

Need help on SQL Query Logic into Tableau Calculation?

Hi,

I need to achieve following SQL logic in Tableau Calculation.

SQL : CASE WHEN Product IN ('Samsung') THEN SUM(CASE WHEN Summary= 'AA' AND SummaryStatus= 'BB' THEN Qty ELSE 0 END) ELSE Count(DISTINCT ID) END  As "Result"

This is part of my query. Whole query returns following output.

ProductDistinct Qty

Sum Of Qty

Result

Sony5105
Samsung1400400
Airtel121

• 1. Re: Need help on SQL Query Logic into Tableau Calculation?

Hi all,

I found answer myself. It's a simple one.

Step 1 : We have to add the Summary= 'AA' AND SummaryStatus= 'BB' in filter. (Which is in my SQL 'where' condition)

Step 2 : Following Tableau calculation works for me,

CASE ATTR(Product) When 'Samsung'

THEN Sum([Qty])

ELSE COUNTD([ID])

END

• 2. Re: Need help on SQL Query Logic into Tableau Calculation?

Hi, Kapil

Without seeing your underlying data, it is not easy to give you a proper answer, but try below calculation and see if it meets your requirement, if not, please provide some dummy data or a sample workbook.

If Min([Product]) = 'Samsung' Then

sum({Fixed [Product]: sum (If [Summary] = 'AA' And [SummaryStatus] = 'BB' Then [Qty] else 0 end)})

else

Countd([ID])

END

ZZ

• 3. Re: Need help on SQL Query Logic into Tableau Calculation?

Yes you are right. I would have give more information. But i found the way.

Thanks for your effort Zhouyi Zhang