Tableau Pivot Table Columns

This Tableau article will show you how to pivot the table columns with an example. Unfortunately, there is no direct pivot option to achieve this goal in tableau desktop; however, we can write the custom SQL query to convert the unpivot data.

The table data that we use in this example is

Table Rows

You can use the following T-SQL query to get the same table data from the adventure works database.

       YEAR(OrdHead.OrderDate) AS [Order Year],
       SUM(Details.OrderQty) AS [Order Quantity]
  FROM Sales.SalesOrderDetail AS Details
       Production.Product AS PROD ON
          Details.ProductID = PROD.ProductID
       Sales.SalesOrderHeader AS OrdHead ON
         Details.SalesOrderID = OrdHead.SalesOrderID
ORDER BY [Order Year]

First, connect Tableau to Microsoft SQL Server and drag-and-drop the table to see the data inside.

Add Table to Workspace

Next, go to the Data Menu and select convert to custom SQL option.

Select custom SQL option

Tableau Pivot Table Columns

We must write an SQL query using the PIVOT function to transform the Unpivot table columns data. Next, click the preview results option to view the pivot output.

SELECT Name,[2011], [2012], [2013], [2014]
      SELECT [Name]
           ,[Order Year]
           ,[Order Quantity]
        FROM [dbo].[SQL Pivot Source] [SQL Pivot Source]
     ) AS SOURCE
    SUM([Order Quantity]) FOR [Order Year] 
	 IN ([2011], [2012], [2013], [2014])
   ) AS [Result] 
custom SQL for Tableau Pivot Table Columns

Close all the windows to view the Table details and output.

Tableau Pivot Table Columns Preview

Let me design a simple horizontal side-by-side bar chart to display the pivot data.

Chart from Tableau Pivot Table Columns