Tableau measure count items if between dates - date

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.

Related

level of detail expressions cannot contain table calculations or the attr function in Tableau

i have this tableau workbook
basically this calculated day different between each user_id and each transaction for each user_id with this calculation
DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))
that pull filters its so filter the conditions of users (We can ignore this)
and date_rante filters its for calculated day different between date range on parameter
with this calculated
lookup(min(([Created At])),0) >= [START_DATE] and
lookup(min(([Created At])),0) <= [END_DATE]
so from the frequency i want to find out the Max of different day, with this calculated
MAX({FIXED [User Id]:DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))})
but it says
level of detail expressions cannot contain table calculations or the attr function
so i used this solution https://kb.tableau.com/articles/howto/finding-the-dimension-member-with-the-highest-measure-value
and from that solution, i applied with my codes into like this
MAX({FIXED [User Id]:DATEDIFF('day',INT(LOOKUP(MIN([Created At]),-1)), INT(MIN([Created At])))})
but it turns to error datediff being called with string,integer,integer
based on #Anil solution, i tried to create it, and idk why the results was like this
new picture
Presently, as far as my knowledge of tableau is, tableau doesn't allow to calculate LOD calcs or further aggregations on table calcs. To find the transactions where the user took most/max time (in days) in subsequent order- You can do this workaround..
Let's assume your datediff calc field is named as CF1. create another calc field lets say CF2 with following calculation
rank_unique([CF1])
EDIT:
Change table calcs on this field similar to CF1. putting a filter on this field will give you the dates with max(time diff) as shown in screenshot.
table calculation options on first (datediff field)
table calculation options on second field (rank_unique)
I have added third field on colors
(Please note no field used in filters just to highlight)

how to put condition users on tableau

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

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.

Tableau - YTD, MTD as columns in text table

I'm trying to find a way to create a table like the one above, but instead of having columns of months I would like to have columns of MTD, QTD, YTD for all selected measure values. I created calculated fields on the [Date] field but when I try to use that it still splits the data in to separate columns of months and quarters... I'm using two data sources and they are linked on the [Date] field. If I try to put two of my calculated fields in the columns bar it just combines them like 'MTD/YTD'. How can I get them to display as separate columns?
I know this is strange because there will be overlapping data (everything in the MTD will also be in the QTD and the YTD).
I found a way to do this. I created a calculated field on the date field and assigned 'MTD' to all records from the last month. Then I assigned 'QTD' to all records from the start of the quarter to the day before the start date of the 'MTD' records. Then I assigned 'YTD' to all records from the start of the year to the day before the start of the 'QTD' records. (this way there are no overlapping records).
After that I just did a quick table calc to make each of them a 'running total' table (across).
I created parameters for the dates so the can easily be updated.