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

Pivot a table on start and end times

rotsanders
6 - Meteoroid

I have a table that has a start and end datetime for a certain event. I am trying to find a way to pivot it out so that every hour that this event occurs has it's own row.

 

Below is an example of what I'm trying to do. I want to go from the first table to the second table. Does anyone know a good way to do this? 

 

IDstartend
18/30/2012 6:158/30/2012 8:27
24/4/2012 8:254/4/2012 14:47
310/4/2010 10:1010/4/2010 10:11
   
   
ID Hour 
18/30/2016 6 
18/30/2016 7 
18/30/2016 8 
24/4/2012  8 
24/4/2012 9 
24/4/2012 10 
24/4/2012 11 
24/4/2012 12 
24/4/2012 13 
24/4/2012  14 
310/4/2010  10 

 

Thanks!!

3 REPLIES 3
jdunkerley79
ACE Emeritus
ACE Emeritus

the easiest way is to use a generate rows tool.

 

First convert the start and end columns into datetimes and then you can use the DateTime add function.

 

The generate rows would start at:

datetimetrim(start, "hour")

and end at:

hour <= datetimetrim(end, "hour")

the step is:

datetimeadd(Hour,1, "hours")
dataMack
12 - Quasar

Here's how I would approach it, though there are likely several ways to solve the problem:

 

1. First I would use a Transpose tool - ID is your key feild and start and end are your data fields.  This will put all the dates in the same column with the ID field repeated.

2. Next I would use a formula tool to truncate the hour and make that its own field

3. Then a multi-row formula tool to interrogate the difference in hours between between start and end, and make that a field

4. Using the value from steps 2 and 3, you use a Generate  Rows tool to insert the # of rows needed (#3)

5. Then another Multi-Row fomula tool to fill in the details of the new rows

SophiaF
Alteryx
Alteryx

On the off chance you are looking to keep the formatting standardized, here's my take:

 

Use RegEx to parse [start] and [end] into Date and Hour:

(\d+\W\d+\W\d{4})\s(\d+)

Generate Rows:

  • Initialization Expression
  • [StartHour]
  • Condition Expression
  • [GeneratedHour]<=[EndHour]
  • Loop Expression
  • [GeneratedHour]+1

 

Formula Tool to concatenate:

StartDate]+" "+tostring([GeneratedHour])
Sophia Fraticelli
Senior Solutions Architect
Alteryx, Inc.
Labels