Thanks, Shin. That will actually be very useful for a different project I'm working on, but I'm afraid I am using SQL for this project and Tableau won't let me pivot the data. Sorry, I should have mentioned that previously.
Any other thoughts?
Thanks, Shin. I'll spend some time messing around with this and see what I can do.
Pivot using custom SQL
You can also use custom SQL to pivot your data. When you use the UNION ALL operator in a custom SQL query, you can take values from distinct columns and put them into a new column.
For example, suppose you have a table called Contest.
Runner Start Time End Time Amanda 9/3/2016 3:04 PM 9/3/2016 3:25 PM Oscar 9/3/2016 3:04 PM 9/3/2016 3:21 PM William 9/3/2016 3:04 PM 9/3/2016 3:16 PM
To optimize your analysis of this data in Tableau, you can use the following custom SQL query to pivot the "Start Time" and "End Time" columns so that their values are in a single column.
, 'Start' as [Action]
, [Start Time] as [Time]
, 'End' as [Action]
, [End Time] as [Time]
The above custom SQL query does the following:
- Pivots the Start Time column header into a string value called Start and adds that value to a new column called Action.
- Pivots the End Time column header into a string value called End and adds that value to a new column called Action.
- Pivots the Start Time and End Time columns so that their values are in a new column called Time.
The following table shows the results of this custom SQL query.
Runner Action Time Amanda Start 9/3/2016 3:04 PM Oscar Start 9/3/2016 3:04 PM William Start 9/3/2016 3:04 PM Amanda End 9/3/2016 3:25 PM Oscar End 9/3/2016 3:21 PM William End 9/3/2016 3:16 PM
To pivot data using custom SQL
- Connect to your data.
- Double-click the New Custom SQL option in the left pane. For more information, see Connect to a Custom SQL Query.
- In the Edit Custom SQL dialog box, copy and paste the following custom SQL query and replace the contents with information about your table:Select [Static Column]
, 'New Value (from Column Header 1)' as [New Column Header]
, [Pivot Column Values 1] as [New Values]
Select [Static Column]
, 'New Value (from Column Header 2' as [New Column Header]
, [Pivot Column Values 2] as [New Values]
Select [Static Column]
, 'New Value (from Column Header 3' as [New Column Header]
, [Pivot Column Values 3] as [New Values]
- New Value (from Column Header 1-3): New names that you give to the original column headers, which are used as row values in the pivot.
- Pivot Column Values 1-3: The columns whose values need to be pivoted into a single column.
- New Column Header: The name you give the new column that contains the new row values from New Value (from Column Header 1-3).
- New Values: The name give the new column that contains the original values from Pivot Column Values 1-3.
- Table: The table that you connected to.
- Click OK.