One of my biggest annoyances when it comes to cleaning data is needing to make a transformation that is seemingly super easy but turns out to be much more involved when it comes time to implement. I’ve found that while Qlik is not immune to these scenarios, you can very often script your way to a solution.

One of these common snafus is having to unpivot a table. Or…pivot it? Perhaps un-unpivot?

Let’s look at an example of how to pivot and then unpivot a table using Qlik script.

The Example

I first create a new app in Qlik Sense SaaS (though this all works exactly the same if using Qlik Sense on-prem). I am going to pull in the Raleigh Police Incidents (NIBRS) dataset, provided freely to the public via the city’s ArcGIS Open Data portal; here’s the link to the data:

https://data-ral.opendata.arcgis.com/datasets/ral::raleigh-police-incidents-nibrs/about

Here’s our load script to bring in the table from a CSV file:

Notice the fields on lines 16-20 that begin with “reported” — these appear to be the same date field but with slightly different formats and granularity. Let’s check it out:

Just as we suspected. Now, what if we wanted to pivot (or “transpose”) those fields into just two fields: one for the field names and one for the field values? This can be done in either the Data Manager or the Data Load Editor. When scripting in the load editor, we use the Crosstable() function.

To do this, I’m going to choose to do this pivot with only the date fields and the unique ID field, which in this case is the [OBJECTID] field:

Our crosstable load looks like this:

This screenshot shows that, on lines 3-9, we are loading our key and date fields from the [Data] table we already loaded in. Then, on line 2, we use the Crosstable prefix and include 3 parameters:

  • [Date Field] is the field that will be created to hold the field names that appear on lines 4-9.
  • [Date Value] is the field that will be created to hold the field values from the fields on lines 4-9.
  • 1 indicates that I only want to pivot the date fields around the first loaded field in this table, which is [OBJECTID] in this case. If this was 2, for example, then this operation would pivot the table around [OBJECTID] and [reported_date].

When we run this script, Qlik first loads the data from the CSV into the [Data] table, and then pivots (“crosstables”) the date fields around the key field, [OBJECTID], into a new table called [Dates].

We now see why we did this operation in a separate table and did not include all of the other fields – the pivot predictably increases the number of rows the app is now using:

Here’s what we now have:

Notice how the [Date Field] now holds all of those column names and the [Date Value] field now has those column values. We have successfully turned this:

…into this:

But what if our data had started out in a “pivoted” format? Or what if we want to simply unpivot that data?

The Solution

In order to unpivot the table, we’ll use a Generic Load and some clever scripting.

First, I’ll write the Generic Load:

This screenshot shows that I am using the [OBJECTID] field as my key, [Date Field] as the field names (“attributes”; these will be turned into separate columns), and [Date Value] as the field values (these will become the values for those separate columns).

Here’s the resulting schema:

We now have our data back in the column/value orientation we want, but we have an annoying issue: the resulting tables don’t auto-concatenate. We instead get a separate table for each unpivoted field.

Let’s write some script to always join these tables together without having to write a separate JOIN statement for each table:

Each line of this script is doing the following:

  1. This line creates a new empty table called [Un-unpivoted] with only our key field, [OBJECTID].
  2. *blank*
  3. This line begins a For loop that starts with the number of loaded tables in our app up to this point (NoOfTables()-1) and then decrements (step -1) down to zero (to 0).
  4. Here, inside the For loop, we set the variable vCurrentTable to the table name of the current loaded table index. This just means that every table loaded into the app at this point can be identified by the index at which they were loaded or joined. The first table loaded is 0, the next one is 1, etc. This order changes if that first table is joined with another table later on, though.
  5. *blank*
  6. Here, we check to see if the current table name begins with “Unpivoted.”, which is what our Generic Load tables are prepended with.
  7. If the current table indeed begins with “Unpivoted.”, then we Resident the table with all of its fields and join it to the [Un-unpivoted] we created on line 1.
  8. Now that we’ve joined our table into our master [Un-unpivoted] table, we can drop it from our data model.
  9. This ends our IF statement.
  10. This takes us to the next table index.

Here’s a table that shows the operations for each iteration:

Once we run this, we can see in the resulting table that we were successful!