Using the Unpivot Step to make a Tabular Table out of a Crosstab Table

Data Tutorial SQL Tips

If your table is in a crosstab format and you are looking to get it into a flat tabular format, Chartio has a Data Pipeline step that can help you accomplish this task.

After querying the columns you want in the Chart Builder, our crosstab style table appears.

We have in this example the Student ID# of a number of students in our school and the test results from 3 different aptitude tests 1 A, 1 B, and 1 C.

![alt_text](/assets/303006/tutorials/unpivot-step-from-crosstab-to-tabular-table/8c6c43b810a0d603ca3e7af913b0c6d73dce2f3dda86462ccb52a28c4a570842/crosstab-to-tabular-table-img-1.png “image_tooltip”)

What we are after in this case is to count the number of times each score appears in each aptitude test.

1. After we get the crosstab style table, we can use 1 Data Pipeline step to unpivot the table into a tabular format.

![alt_text](/assets/2e91cc/tutorials/unpivot-step-from-crosstab-to-tabular-table/80f2847e126083eeb8f844e6d21bda3fff8f86b1fb42ad4967ba1c3bd1eeb5f0/crosstab-to-tabular-table-img-2.png “image_tooltip”)

2. Then we can just add a column that replicates the “Score” column, and the purpose for that is to use a group step to count all returns of each score. Then we need to hide the Student ID# column.

![alt_text](/assets/1dbdb7/tutorials/unpivot-step-from-crosstab-to-tabular-table/43ebda5d9b73526f88e241658e901090bec7d5986fcad559b187f32628e0281a/crosstab-to-tabular-table-img-3.png “image_tooltip”)

  1. Here is where we will use the group Data Pipeline step to get the count of each potential score for each of the three tests. Selecting GROUP as the grouping options for the first two columns, we can select COUNT for the score replica column.

![alt_text](/assets/7ae466/tutorials/unpivot-step-from-crosstab-to-tabular-table/5b926b9f47a87211f72fb0b4e53dbb0e0489bcffa597c4322bcfad78f2274fb3/crosstab-to-tabular-table-img-4.png “image_tooltip”)

3. Now we can use a re-order step and a pivot step to get the count of each score, under a column header that represents each test.

![alt_text](/assets/8c31cf/tutorials/unpivot-step-from-crosstab-to-tabular-table/d04513acf7177e97b383b6b202dc85554629af9b620f8860e50c40be702e2871/crosstab-to-tabular-table-img-5.png “image_tooltip”)

4. We can now visualize this table as a stacked bar chart to show how each test’s scores were distributed.

![alt_text](/assets/314d68/tutorials/unpivot-step-from-crosstab-to-tabular-table/0de16c0ec8fd2f31abd8ec1dd2914c15e8876bb208ab9cc963d24d0c3558a7c6/crosstab-to-tabular-table-img-6.png “image_tooltip”)

This workaround will help you easily flatten a crosstab table instead of having to write a monotonous query with numerous UNIONs. This will give you the ability to visualize these crosstab tables quickly and get to the insights your decision makers are looking to reach.