Tableau group by multiple groups - tableau-api

I have created two groups on a customer level (all with distinct customer IDs) based on some criterias. But I am having difficulties grouping these on a group level (group ID). The data structure is a follows: a customer has one customer ID and a group ID. The customer ID is distinct but the group ID is not; i.e. multiple customers (customer IDs) are part of a group and therefore have the same group ID.
My tableau code looks something like this:
IF [Sales] >= 200000 and [Category] = 'A'
OR [CAC] <= 10000 and [Category] = 'A'
THEN 'Good customer'
ELSE 'Bad customer'
END
The above code gives me the grouping on a customer level. However, I want to see the group level, i.e. if just one customer from a group is a 'Good customer' then the entire group should be classified as a 'Good customer'. This means that if just one customer from the group is classied as a 'Good customer' then all the [Sales] and [CAC] of the customers within the particular group should be summed up on a group level and displayed under 'Good customer' on a group level instead of on customer level.

Try this:
Create a field [GoodCustomer] with this formula:
IF [Sales] >= 200000 AND [Category] = 'A'
OR [CAC] <= 10000 AND [Category] = 'A'
THEN 1
ELSE 0
END
This is your condition but assigns a numeric value to it (1 = good customer, 0 = bad customer)
Create a field [GoodGroup] with this formula:
{fixed [Group]: IIF(SUM([GoodCustomer]) > 0, True, False)}
For each group this checks if the sum of [GoodCustomer] is greater 0 (which means that at least one customer was good). If this is true it sets it to True (or use 'Good Group') if it is false, set it to False (or 'Bad Group')
This should give you what you described above.

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.)

Indicate more than one record with matching fields

How can I indicate multiple records with the same Invoice number, but a different Sales Person ID? Our commissions can be split into multiple Salespeople, so there can be two different Salespeople per an invoice.
For example:
Grouped by: Sales Person ID (No Changing this option)
These records are in the Group Footer.
Sales Person ID: Invoice: Invoice Amt: Commissions: (Indicator)
4433 R100 20,000 3,025 * More than one record on the same invoice with a different sales person
4450 R096 1,987 320
4599 R100 20,000 3,025 * More than one record on the same invoice with a different sales person
4615 R148 560 75
4777 R122 2,574 356
If your report has less than 1000 invoices, you may try something like this.
This will return true when a second ocurrence of the invoice shows up. Then you can make something like set the row background do red.
Global NumberVar Array invoices;
numbervar nextIndex := count(invoices) + 1;
if nextIndex <= 1000 and not ({Result.InvoiceNumber} in invoices) then (
redim invoices [nextIndex];
invoices[nextIndex] := {Result.InvoiceNumber};
true;
)
else false;
If you want to detect the first occurrence, you will need something more sophisticated.
I think a SQL Expression Field would be a good way to achieve the result you want. You already have an InvoiceNo in each row of data. You just need a SQL Expression Field that uses that INvoiceNo to execute a query to count the number of salespersons who get a commission.
Something along the lines of:
(
Select Count(Sales_Person_Id)
From [Table]
Where [Table].InvoiceNo = InvoiceNo
)
This will return an integer value that represents the number of salespersons who are associated with one invoice. You can either drop the SQL Expression Field in your Indicator column, or write some other formula to do something special.

Tableau:Create a calculated field using Case statement and case statement uses a logical calculation

I have a question regarding tableau calculated field.
I am using Sample Super Store data. Below is my questions
I want to Categorize Sales into 'Low Sales','High Sales' and 'Super Sales' based on below
using Case Statements
SUM(Sales)>500000 then categorize as 'Less Sales'
(Sales) between 500000 and 1000000 then 'High Sales'
Else SUM(Sales)>1000000 then 'Super Sales'
After categorising I need to find out the count of [Order] under Super Sales '
Try below steps:
Create 3 sets for Order ID 3 conditions:
Set 1:
Right Click Order ID --> Create --> Set Here move to the condition tab and select condition for Sales and value is <50000
Similarly create 2 more sets.
Create a calculated field Display and write below code:
IF [Set 1]=TRUE
THEN "Less Sales"
ELSEIF [Set 2]=TRUE
THEN "High Sales"
ELSEIF [Set 3]=TRUE
THEN "Super Sales"
END
Place the calcualte field on Rows and Order ID on Columns
For order ID select the aggrigation Count Distinct
Now you should be able to see the order count for all sales

DISTINCT ON still gives me an error that select item should be in GROUP BY

I have a table with a list of customer IDs and a list of dates as follows:
id |take_list_date | customer_id
1 |2016-02-17 | X00001
2 |2016-02-20 | X00002
3 |2016-02-20 | X00003
I am trying to return a count of all the IDs in the table on a specific day in the following format:
label: 2016-02-20 value: 2
The following query produces the required results within the specified date range:
select
count(customer_id)::int as value,
take_list_date::varchar as label
FROM
customer_take_list
where
take_list_date >= '10-12-2017'
and
take_list_date <= '20-12-2017'
GROUP BY
take_list_date
ORDER BY
take_list_date
The problem is I have to include an ID field to make it compatible with Ember Data. When I include an ID field I need to add it to the Group By clause which produces incorrect results.
After looking at some suggestions on other SO questions I tried to resolve this using DISTINCT ON:
select distinct on
(take_list_date) take_list_date::varchar as label
count(customer_id)::int as value
FROM
customer_take_list
where
take_list_date >= '10-12-2017'
and
take_list_date <= '20-12-2017'
order by
take_list_date
Bizarrely this still gives me the same Group By error. What have I done wrong?
I'm not an expert in the technologies involved, but I think you need to create an arbitrary ID rather than use one of the ID's in the table. An example is here: Add Postgres incremental ID. I think your final query should look something like this:
SELECT
COUNT(customer_id)::int as value,
take_list_date::varchar as label,
ROW_NUMBER() OVER (ORDER BY take_list_date) AS id
FROM
customer_take_list
where
take_list_date >= '10-12-2017'
and
take_list_date <= '20-12-2017'
GROUP BY
take_list_date
ORDER BY
take_list_date

How can I order a group, based on a summary field of a subgroup

I have a report which has essentially
Order
OrderDetail 1
OrderDetail ..
OrderDetail n
These details can have parts and/or labour costs associated with them.
Currently, I group based on OrderId and then have the OrderDetail information in the details section of the report. This works perfectly.
However, now I need to group the Orders based on two criteria OrderType and LabourCost of the entire Order. I have put together a quick formula to determine order.
if(Sum({order.Labour}, {order.OrderId})> 0) then
if({order.type} = "type1") then 1 else 2
else
if({order.type} = "type1") then 3 else 4
Basically, if it should be sorted based on labour then on type. (the Sum({order.Labour}, {order.OrderId}) sums the labour grouping based on the orderid)
However when I go to the Group Expert and add the group by field to my formula and then preview my report it spins (I cancelled the preview after a minute). If I remove the Sum portion of the formula then it takes less than a second.
Is there a way to order this report?
How I would approach it:
First, create a sql-expresssion field that calculates the labor total for each order:
// {%TOTAL_LABOR}
(
SELECT Sum(Labour)
FROM OrderDetail
WHERE OrderId=Order.OrderId
)
Next, create a formula field:
// {#OrderGroup}
if({%TOTAL_LABOR}> 0) then
if({order.type} = "type1") then 1 else 2
else
if({order.type} = "type1") then 3 else 4
Finally, create a new group, based on the formula field, ensuring that it groups before the order group. You can suppress the group's header and footer if desired.