Convert sql statemet to tableau calculated field - tableau-api

Sql :
sum(case when invoicestatus in ('a','b','c') THEN amount ELSE 0 END)
Tableau parameter : pivoicestatus = 'a','b','c'
Tableau calculated field:?
I want to use pinvoicestatus (parameter) instead of hardcoding the value inside Tableau calculated field

Tableau Calculated Field:
IF [pivoicestatus]='a'
THEN amount
ELSE IF [pivoicestatus]='b'
THEN amount
ELSE IF [pivoicestatus]='c'
THEN amount
ELSE 0
END

Related

Calculating total revenue lost in returns with Superstore dataset

I am attempting to create a parameter that allows me to custom filter for Sales, Units, Profit, Orders, Returns, and Return Units. I'm running into an issue when creating the calculated field that calculates return $ and return units. The result I am getting doesn't seem to be accurate.
I have created a Parameter with the above list called [Choose KPI].
I thought I would be able to create calculated fields for [Returned $] and [Returned Units]:
IF [Returned] = 'Yes' THEN [Sales] END
and
IF [Returned] = 'Yes' THEN [Quantity] END
Which would make my calculated field for [Choose KPI]:
CASE [Parameters].[Choose KPI]
WHEN "Sales" THEN SUM([Sales])
WHEN "Units" THEN SUM([Quantity])
WHEN "Profit" THEN SUM([Profit])
WHEN "Orders" THEN COUNT([Order ID])
WHEN "Returns" THEN SUM([Returns $])
WHEN "Return Units" THEN SUM([Returned Units])
END
Rather than returns being associated with product ID, they appear to be linked only with the order ID, which creates duplicate values when there were multiple items on an order (are we assuming all items in the order were returned when [Returned] = 'Yes'?), causing the sum of [Returns $] to be inflated.
How can I create a filter that uses the distinct Order ID to calculate total returns and returned units?
In Tableau, you can use the "Group" option in the "Analysis" menu to create a new field that groups your data by the "Order ID" field. Then, you can use the "COUNTD" function to count the number of unique "Order ID" values, which will give you the total number of returned orders.
To calculate the total returned units, you can create a calculated field that multiplies the quantity of each returned item by the number of unique items returned per order. You can use the COUNTD([Order ID]) and SUM(IF [Returned] = 'Yes' THEN [Quantity] END) in the calculation.
In the [Choose KPI] field, you can use the following calculation:
CASE [Parameters].[Choose KPI]
WHEN "Sales" THEN SUM([Sales])
WHEN "Units" THEN SUM([Quantity])
WHEN "Profit" THEN SUM([Profit])
WHEN "Orders" THEN COUNTD([Order ID])
WHEN "Returns" THEN SUM(IF [Returned] = 'Yes' THEN [Sales] END)
WHEN "Return Units" THEN SUM(IF [Returned] = 'Yes' THEN [Quantity] END) * COUNTD([Order ID])
END
This way, you will be able to filter your data based on the different KPIs you've defined, and the calculations for returned sales and units will be based on the distinct Order ID values.
(Answer by https://chat.openai.com/chat and formatted by me.)

Create a calculated field to count dimension on a condition in Tableau

I have two dimensions (Case Numbers, Test Code) and I want to count only the Case Numbers which have both test codes i.e. 4802 & 9050.
I created a data similar to yours
Create this calculated field
{FIXED [CaseNumber]: sum(
IF [TestCode] = '4802' OR [TestCode] = '9050' THEN 1 ELSE 0 END)} = 2
TRUE in above calculated field will return only desired CaseNumbers. Check
OR
OR

How to get a sum of all rows that meets condition in postgres

I am trying to return sums with their specific conditions.
SELECT
COUNT(*),
SUM("transactionTotal" WHERE "entryType"=sold) as soldtotal,
SUM(case when "entryType"=new then "transactionTotal" else 0 end) as newtotal
FROM "MoneyTransactions"
WHERE cast("createdAt" as date) BETWEEN '2020-10-08' AND '2020-10-09'
I am trying to sum up the rows with "entryType"=sold and "entryType"=new and return those values separately.
obviously both my logic are wrong.
can someone lend a hand.
You were on the right track to use conditional aggregation, but your syntax is slightly off. Try this version:
SELECT
COUNT(*) AS total_cnt,
SUM(transactionTotal) FILTER (WHERE entryType = 'sold') AS soldtotal,
SUM(transactionTotal) FILTER (WHERE entryType = 'new') AS newtotal
FROM MoneyTransactions
WHERE
createdAt::date BETWEEN '2020-10-08' AND '2020-10-09';
Note: Assuming your createdAt column already be date, then there is no point in casting it. If it is text, then yes you would need to convert it, but you might have to use TO_DATE depending on its format.

Tableau mixing aggregate and non-aggregate IF

I am trying to add a calculated field in my Data Source connection.
If column 'Order Status' is not equal to 'Shipped' then give the sum of the 'Carton Count' else return 0.
This is what I tried and not sure why it won't let me use this.
IF([Order Status]) != 'Shipped'
THEN SUM([Carton Count])
ELSE 0
END
Tableau doesn't work in a traditional way where we aggregate the the measures in a formula, tableau doesn't allow you to aggregate in a calculated field instead you take measures as it is and use aggreagation at sheet level.
So your formula will be like:
IF([Order Status]) != 'Shipped'
THEN [Carton Count]
ELSE 0
END
When you drag this field on to sheet then aggregation is applied automatically

Crystal reports sum

I need the sum of two database fields. I use this formula field :
{dbfield1}+{dbfield2}
and if dbfield1 and dbfield2 are != from null in database the sum is showing, but if the dbfield1 or dbfield2 are missing(no data) the formula field is not showing.
how can I manage this in Crystal report?
Two options :
Either use the Convert Database Fields to Null option under Report Options, which will convert the numeric field nulls to zero and make your sum work, or
Use the IsNull function in your formula :
If IsNull({dbfield1}) And IsNull({dbfield2}) Then
0
Else If IsNull({dbfield1}) Then
{dbfield2}
Else If IsNull({dbfield2}) Then
{dbfield1}
Else
{dbfield1}+{dbfield2}