1 2 Previous Next 17 Replies Latest reply on Aug 9, 2016 9:39 AM by Mahfooj Khan

# Counting days of the same variable

Greetings Tableau community;

I have a date variable that records customer's visit. I want to count the days between visits. In other words.

customer_id | visit_date

customer 1 | 07/02/2016

customer 1 | 08/01/2016

customer 1 | 08/04/2016

Is the same customer, but I want to calculate how many days between visits.

• ###### 1. Re: Counting days of the same variable

Is it what you wanted?

No of day visit between each date. Between 8th Jan to 7th Feb 30 days. Let me know If you've any query.

OR

Hope this help.

Mahfooj

1 of 1 people found this helpful
• ###### 2. Re: Counting days of the same variable

Mahfooj Khan

I did the second option but the column is not add it to the table.

• ###### 3. Re: Counting days of the same variable

Why? Can you share entire screen shot of your worksheet?

• ###### 4. Re: Counting days of the same variable

What is a good resource to learn how to use tableau builtin functions?

• ###### 5. Re: Counting days of the same variable

I modify the the code to group by Customer ID. First visit cannot be calculated.

IF ATTR([Customer ID]) == ATTR([Customer ID])

THEN

DATEDIFF('day',LOOKUP(ATTR([Visit Date]), -1, ATTR([Visit Date]))

END

However the first visit is getting calculated by the last visit of the previous customer.

How to group the function by customer.

• ###### 6. Re: Counting days of the same variable

I want to add the complete solution that solve the problem.

IF LOOKUP(ATTR([customer_id]),-1) = ATTR([customer_id)

THEN

DATEDIFF('day',LOOKUP(ATTR([visit_day]),-1,ATTR([visit_day]))

END

• ###### 7. Re: Counting days of the same variable

Can you tell me what's the purpose of lookup on customer_id? Do you wants to compare the date diff with other customers? Please explain. Without seeing your underlying data unable to provide any solution. Kindly share us a sample set of data or your sample workbook in .twbx format.

thanks!!

Mahfooj

• ###### 8. Re: Counting days of the same variable

I want to count days by customer. I marked your solution as helpful since I used your code for my solution. I cannot share sensitive data.

Thank you for your code, did help me.

Best Regards!

Gilbert Diaz

• ###### 9. Re: Counting days of the same variable

Its true no one can share their org data. But I'm asking you to share the dummy data which is very similar with your original data.

Keep the format same and change the underlying values. Also provide your expected output. Hope you got my point.

• ###### 10. Re: Counting days of the same variable

`customer_id | visit_day | count_dayscustomer 1 | 1/1/2016 | customer 2 | 1/3/2016 |customer 2 | 1/15/2016 | 12 customer 3 | 2/1/2016 | customer 3 | 2/5/2016 | 4`

• ###### 11. Re: Counting days of the same variable

You can easily achieve this using the calc field which I've given earlier. I guess in compute part you've missed something that's why you're unable to get the correct result. Anyways I'm giving you the walk though. Have a look

Option-1 using table calculation:

Option-2 using LOD

Hope this help.

Mahfooj

1 of 1 people found this helpful
• ###### 12. Re: Counting days of the same variable

You are right. I changed my code and used the calculation table to restart for every customer_id.

• ###### 13. Re: Counting days of the same variable

If my solution has answered your question then you may close the thread by marking it as correct. So that It may help others who've similar query.

• ###### 14. Re: Counting days of the same variable

I don't see where to mark it as correct. I do see "THIS HELPED ME"

1 2 Previous Next