Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Calculating date difference between site visits

essemMLB
7 - Meteor

Hello Alteryx community,

 

I'm working on a dataset containing dates, visitor IDs, and visit IDs, and my objective is to calculate the difference, in hours, between visits. Here's an example pertaining to a single visitor:

 

DateTimeVisitor_IDVisit_ID
2019-09-019:00:00 AM123456789A
2019-09-0110:00:00 AM123456789B
2019-09-0111:00:00 AM123456789C
2019-09-024:00:00 PM123456789D
2019-09-027:00:00 PM123456789E
2019-09-033:00:00 PM123456789F

 

As you can see, Visitor 123456789 visited my site in six different sessions over the course of three days. Of course, my dataset contains hundreds of thousands of Visitor_IDs, so it's not possible to break them out and work on them individually. Ultimately, I'd like to get to this:

 

DateTimeVisitor_IDVisit_IDHours Since Last Visit
2019-09-019:00:00 AM123456789AFirst Visit
2019-09-0110:00:00 AM123456789B1
2019-09-0111:00:00 AM123456789C1
2019-09-024:00:00 PM123456789D29
2019-09-027:00:00 PM123456789E3
2019-09-033:00:00 PM123456789F20

 

Hopefully my example here is straightforward. I've tried a few ways but what I seem to get stuck on is ensuring that the calculation of hours since last visit stays within the Visitor_ID, i.e. Alteryx does not just calculate the time difference between each row regardless of the ID that groups it. 

 

As a final note, you can see that I marked the first row as 'First Visit.' For each Visitor_ID, there's going to be a row with nothing to calculate previous to it. I'm open to suggestions on the right way to label that, as obviously I don't want to set it to 0.

7 REPLIES 7
BrandonB
Alteryx
Alteryx

A bit of Date Time Standardization and then a multi row formula tool does the trick. Workflow attached. Take a look! As you add more visitor ID's you will want to check the box for Visitor_ID in the group by box of the multi row formula. 

 

Date Standardization.png

 

 

DiganP
Alteryx Alumni (Retired)

@essemMLB I like @BrandonB 's solution. The key part to this is to concatenate the Date+Time in the formula tool by using the DateTimeParse([DateTime],'%Y-%m-%d %I:%M:%S %p') formula and then using the multi-row formula tool to look at the below rows. 

 

In the multi-row formula tool, you can use the datetimediff() function to calculate the different in hours for the below rows.

 

if isnull([Row-1:New Field]) then 'First Visit' else datetimediff([X],[Row-1:X],'Hours') endif

 

Digan
Alteryx
essemMLB
7 - Meteor

Hey! Thanks for the quick reply.

 

I'm getting this error when applying the formula to my workflow: 

 

ConvError: Formula (42): DATETIMEPARSE: Cannot convert "2019-01-12 05:27:54" to a date/time with format "%Y/%m/%d %I:%M:%S %p": DateTime error: read an hour 1 to 12 (%I), but did not read an AM/PM indicator (%P): '%Y/%m/%d %I:%M:%S %p'

DiganP
Alteryx Alumni (Retired)

@essemMLB The reason why you are getting that warning is because the datetime doesn't have a AM or PM. If you add AM or PM to that date time, the syntax will work fine. 

 

Edit: you can find more information about the syntax/functions here

Digan
Alteryx
essemMLB
7 - Meteor

It's important that I keep these times in 24-hour format, though. Is there a way to rectify this error without losing that format?

essemMLB
7 - Meteor

Actually, what I did to solve my error was: read the documentation and switch the %I to %H. 

 

Who would have thought that the solution would be to actually read Alteryx's documentation?! What a novel concept! haha

DiganP
Alteryx Alumni (Retired)

@essemMLB Sorry didn't get a chance to reply, yes that should do it! 

Digan
Alteryx
Labels