Tableau Desktop and Web Authoring Help

Pivot Data from Columns to Rows

Sometimes, analyzing data that is stored in a crosstab format can be difficult in Tableau. When working with Microsoft Excel, text file, Google Sheets, and .pdf data sources, you can use the pivot your data from crosstab format into columnar format. If you are working with other data sources, you can Pivot using custom SQL (Tableau Desktop).

For example, suppose you have the number of devices sold by quarter for three vendors in three separate fields. You can pivot your data so that the vendor is in one field and the number of devices sold is in another field.

Pivot the data

After you have set up the data source, in the grid, select two or more columns. Click the drop-down arrow next to the column name, and then select Pivot. New columns called "Pivot field names" and "Pivot field values" are created and added to the data source. The new columns replace the original columns that you selected to create the pivot.

Add to the pivot

To add more data to the pivot, select another column, click the drop-down arrow next to the column name, and then select Add Data to Pivot. Make sure that the pivot columns and values look as expected before you begin your analysis.

To remove a pivot, click the drop-down arrow next to the name of a pivot column, and then select Remove Pivot.

Troubleshooting pivots

Red fields in the view and fields with exclamation points in the Data pane: Because the original fields are replaced with new pivot fields, any references to the original fields in the view will no longer work. They cause fields to become red in the view or show a red exclamation point next to the field in the Data pane.

Null values in the grid: If all of the original fields used in the pivot are removed, for example in an extract refresh, null values display in the pivot fields.

No pivot option: Pivot appears when you select two or more columns in a single Microsoft Excel, text file, Google Sheets, and .pdf data source. If using a different data source in Tableau Desktop, you can use custom SQL to pivot.

Pivot using custom SQL (Tableau Desktop)

You can also use custom SQL to pivot your data, even if you aren't working Excel, text file, Google Sheets, and .pdf data sources. 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.

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.

Select [Runner]

, 'Start' as [Action]

, [Start Time] as [Time]

From [Contest]

Union ALL

Select [Runner]

, 'End' as [Action]

, [End Time] as [Time]

From [Contest]

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.