how to put condition users on tableau - tableau-api

im mysql user, i have data source like this
User_id | Order_id | Status_id | createdAt | Transaction_Amount
user_id as the user, order_id as the id of the order, status_id as the status of each order, createdAt as the date of the transaction .
in MySQL, i divide the condition of the users in 4 conditions.
conditions 1 new_user
the user who doing FIRST transaction in date range, and not doing transaction before the date range
conditions 2 repeat_user
the user who doing transaction before the end of date range, and doing atleast 1 more transactions in date range
conditions 3 existing_user
the user who doing his first transaction before the date range, and doing atleast 1 more transactions in date range
conditions 4 unique_user
the user who doing transaction in date range
i've done it with the queries in mysql, but i want to visualize it in tableau, i already make the filter of the time range, but how to make the conditions to based on condition user

Assuming that you want to have create these conditions in tableau, I propose a solution like this. Since you've not provided any data to work upon, a data I used in excel is as
View-1 add a Calculated field(CF) First Transaction of User as
{FIXED [user_id]: MIN([CreatedAt])}
Drag this field on filter instead of created at and you'll get a view based on your condition_1. Screenshot
View-2 (I am assuming that both transactions may have been completed within the filtered date range. If you need otherwise please specify).
step-1. drag user_id to rows, createdAt at filters (select range of dates).
Step-2 Drag order_id to filters again select count distinct, select at least 2
You'll get a view like this
View-3 Add two CFs as. 1st condition 1 as
Min([First Transaction of User])< min([CreatedAt])
another CF condition 3 check2 as
{Fixed [user_id]: COUNTD([order_id])}>1
Add both these CFs on filter shelf along with createdAt. Add condition-1 check2 to context (important step) select TRUE from both filters and you'll get desired view. like this
View-4 It is the most basic chart in tableau. Try and I am sure you can do it. Good luck

From your workbook it is clear that you want cohort analysis. In this regard Let me explain a few things..
First create a user cohort like this
{FIXED [user_id]: MIN([CreatedAt])}
Output of this field will be date when user first ordered. So if you will add this field to filters the output will be users (added in view) who first ordered within the filtered date/range.
Now if you want to see the users' orders during the same period (condition 1) also add the createdAt field to filters with same date/range.
E.g. suppose your date range is 5jan to 15 jan. Then set both filters to these dates range. First filter will ensure to filter customer with their first transaction and second one will give you desired output.
For condition2, you'll have to add countd(order_ids) to filters with filter value set 'at least 2'.
E.g. setting this filter will ensure that customer did their first and at least one more transaction during the date range.
For condition3, changing dates in filter accordingly will do. Nothing extra to be done.
E.g. suppose you want the customers with second and onward transaction between 5 jan to 15 jan. Then you will have to set first cohort filter with date upto 4 jan but second date filter to 5 jan to 15 jan
Condition 4: remove cohort from filters. Set date range on createdAt field only.
If you want to add another filters like status etc., These will work after you add them to filters.
Try this

Related

Tableau measure count items if between dates

What I am trying to achieve is to get a count of people employed in a particular period.
I have 3 variables:
Employee ID (integer)
Hire date (date)
Termination date (date or null)
Example
the formula I am looking for is something like
if termination_date is null
then
count employee_ID in
dates between Hire_date and max of either hire_date or termination_date
else
count employee ID in
dates between hire_date and termination_date
This aims to show the dynamic of staff level over the time.
I am new to Tableau, not sure how to even start with it. Any suggestions welcome.
This problem will be simpler if you reshape your data to have the following three columns
Employee ID
Date
Action. (where action takes on the values of ‘Hire’ or ‘Terminate’).
Each data row represents one change in status for an employees. If an employee had a termination date, they will have two records in this new format, otherwise just one record showing the hiring date.
You can reshape your data by hand, or leave the original and use Tableau Prep or the Tableau data source page to reshape using a self Union and a few simple calculated fields.
Define a calculated field called Staffing_Change as
if Action=‘Hire’ then 1 else -1 end
Now you can plot the change in staff level over time by putting exact date on columns and sum(Staffing_Change) on Rows. You can use a quick Table calc, Running Sum, to see the net staffing level. For line mark types, I’d use a step style by pressing on the path button on the Marks card. Otherwise, the chart can give the impression of fractional number of employees.

Count at the end of a month in Tableau

I want to find the number of customers per subscription plan at the end of the acquisition month.
As you can see in the workbook, customers can change their subscription plan on any date, but I only want to find the subscription plan at the end of the acquisition month.
I created a field, called EOM Acquisition, but need to find the desired output below for any given month of acquisition. (in sample dataset only October).
How can I do that? Thanks in advance!
Please proceed like this. Create a new calculated field say calculation1 as follows-
IF [Date] = [EOM Acquisition] then [Subscription Plan] END
drag this field instead of subs plan in view and filter out null values, you'll get what you want
Another way add a calc field as follows
IF [Date] = [EOM Acquisition] then 1 ELSE 0 END
And thereafter add sum on this field as measure alongwith EOM and subscription fields.

Tableau Count Distinct when graphed shows chronological last date, when deduplicated, not first

I'm doing a break fix on a Tableau report visualization that shows the outcomes of clients by client id for a given year by showing a running sum of distinct count of client id or RUNNING_SUM(COUNTD([ID])). The X axis of the visualization is the initial date of contact with the client. Occasionally, due to errors in the data or weird behavior, there are clients that have two initial dates, listed as two separate data rows where the column Initial Date will have different values but they will share an ID.
Currently, the visualization shows such people with their chronological last Initial Date and I need it to dedup such that the visualization shows them as starting from the chronological first Initial Date.
I could create a calculated field for if there are two IDs with multiple non identical Initial Dates then use the first, but I'm not sure how to create a calculated field that can groupby or otherwise check multiple dates per ID.
In Python/psuedo code, it would be something like
For ID in IDS:
if len(groupby.IDS.ID)>1:
then Initial_Date = min(InitialDate)
But I have to do the transformation in Tableau
Keep everything the same, but create a calculated field named "Initial Contact Date" with the calculation:
{FIXED [ID]: MIN(InitialDate)}
Then replace the date field on the X axis (Columns) with this date field instead.
That LOD Expression loops through all rows given the ID, and returns only the min one.

Identifying next closest record by date in tableau

I have a table of users and another table of transactions.
The transactions all have a date against them. What I am trying to ascertain for each user is the average time between transactions.
User | Transaction Date
-----+-----------------
A | 2001-01-01
A | 2001-01-10
A | 2001-01-12
Consider the above transactions for user A. I am basically looking for the distance from one transaction to the next chronologically to determine the distances.
There are 9 days between transactions one and two; and there are 2 days between transactions three and four. The average of these is obviously 4.5, so I would want to identify the average time between user A's transactions to be 4.5 days.
Any idea of how to achieve this in Tableau?
I am trying to create a calculated field for each transaction to identify the date of the "next" transaction but I am struggling.
{ FIXED [user id] : MIN(IF [Transaction Date] > **this transaction date** THEN [Transaction Date]) }
I am not sure what to replace this transaction date with or whether this is the right approach at all.
Any advice would be greatly appreciated.
LODs dont have access to previous values directly, so you need to create a self join in your data connection. Follow below steps to achieve what you want.
Create a self join with your data with following criteria
Create an LOD calculation as below
{FIXED [User],[Transaction Date]:
MIN(DATEDIFF('day',[Transaction Date],[Transaction Date (Data1)]))
}
Build the View
PS: If you want to improve the performance, Custom SQL might be the way.
The only type of calculation that can take order sequence into account (e.g., when the value for a calculated field depends on the value of the immediately preceding row) is a table calc. You can't use an LOD calc for this kind of problem.
You'll need to understand how partitioning and addressing works with table calcs, along with specifying your sort order criteria. See the online help. You can then do something like, for example, define days_since_last_transaction as:
if first() > 0 then min([Transaction Date]) -
lookup(min([Transaction Date]), -1) end
If you have very large data or for other reasons want to do your calculations at the database instead of in Tableau by a table calc, then you use SQL windowing (aka analytical) queries instead via Tableau's custom SQL.
Please attach an example workbook and anything you tried along with the error you have.
This might not be useful if you cannot set User ID Field as a filter.
So, you can set
User ID
as a filter. Then following the steps mentioned in here will lead you to calculating difference between any two dates. Ideally if you select any one value in the filter, the calculated field from the link should give you the difference in the dates that you have in the transaction dates column.

MS Access 03 Query Criteras

If I have a report that tracks data for several accounts for each month with rows labeled:
UNITS,
REVENUE,
AVG REV/UNIT
How would I create a query that will filter the report to just show accounts where the UNITS row has increase/decreased 25% and the AVG REV/UNIT has increased/decreased 10%, from the previous month to the current month.
An example would be for the month of June I have the numbers....
JUN
UNITS 3,271
Revenue $3,598.10
Avg R/U $1.08
So when I run the report at the end of July I only want accounts that have a 25% difference in UNITS and/or a 10% difference in AVG REV/UNIT to show on a report.
qryPharmacy
SELECT PHAR_REPORT.*, (IIf(u1 Is Null,0,u1)+IIf(u2 Is Null,0,u2)+IIf(u3 Is Null,0,u3)+IIf(u4 Is Null,0,u4)+IIf(u5 Is Null,0,u5)+IIf(u6 Is Null,0,u6)+IIf(u7 Is Null,0,u7)+IIf(u8 Is Null,0,u8)+IIf(u9 Is Null,0,u9)+IIf(u10 Is Null,0,u10)+IIf(u11 Is Null,0,u11)+IIf(u12 Is Null,0,u12)) AS USUM, (IIf(r1 Is Null,0,r1)+IIf(r2 Is Null,0,r2)+IIf(r3 Is Null,0,r3)+IIf(r4 Is Null,0,r4)+IIf(r5 Is Null,0,r5)+IIf(r6 Is Null,0,r6)+IIf(r7 Is Null,0,r7)+IIf(r8 Is Null,0,r8)+IIf(r9 Is Null,0,r9)+IIf(r10 Is Null,0,r10)+IIf(r11 Is Null,0,r11)+IIf(r12 Is Null,0,r12)) AS RSUM, RMonth.*, PG2.*, PG.pGroup
FROM PHAR_REPORT, RMonth, PG2, PG
WHERE (((PHAR_REPORT.PR) Like ([PCODE] & '*')) And ((PG.pID)=PG2.PID))
ORDER BY PG2.pID, PHAR_REPORT.PR;
You should do it with more than one query. In the first query, select the data for the first month. On a second, to the desired month to compare. Create a third query that links the two first (be care about the correct relationship). Do the grouping/calculations in these queries.
In the 3rd query, create two fields that calculates increasing/decreasing for units and rev/unit. Now, you can add a criterium on each parameter field in the query columns.
The chalenge here is to be sure about hou would you work with the primary keys on months. Eg: if a A row in the first query isn't in the second (for not having an event on second month, for example), it will not be showed. In this case, the solution would be to create the queryes linking a table or query wich has the entyre set of registers, forcing it to show all the desired records despite they have or not occurrences.