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.

SELECT PROD.Name,
       YEAR(OrdHead.OrderDate) AS [Order Year],
       SUM(Details.OrderQty) AS [Order Quantity]
  FROM Sales.SalesOrderDetail AS Details
     INNER JOIN
       Production.Product AS PROD ON
          Details.ProductID = PROD.ProductID
     INNER JOIN 
       Sales.SalesOrderHeader AS OrdHead ON
         Details.SalesOrderID = OrdHead.SalesOrderID
GROUP BY 
         PROD.Name,
         YEAR(OrdHead.OrderDate)
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]
FROM (
      SELECT [Name]
           ,[Order Year]
           ,[Order Quantity]
        FROM [dbo].[SQL Pivot Source] [SQL Pivot Source]
     ) AS SOURCE
PIVOT 
   ( 
    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