Skip to main content

UnPivot

Creates multiple records from sets of columns. It offers a way to consolidate columns by exploding records.

Before UnPivot:

idscore_item1score_item2score_item3
1001452
10026010

After UnPivot:

iditemscore
100114
100125
100132
100216
100220
1002310

Example syntax

{
"stack": [
{ "id": "id", "item": "1", "score": "score_item1" },
{ "id": "id", "item": "2", "score": "score_item2" },
{ "id": "id", "item": "3", "score": "score_item3" },
]
}

Stack

Stack is an array of stack entries. At least 2 stack entries should exist. Each stack entry is a JSON object that contains field assignment(s). This defines the column consolidation (the unpivot), as each stack entry can assign a unique column to a consolidated column name.

"stack": [
{ "brand": "1", "score": "score_b1" }, // assigns score_b1 into q1 (also adds a brand id of 1)
{ "brand": "2", "score": "score_b2" }, // assigns score_b2 into q1 (also adds a brand id of 2)
{ "brand": "3", "score": "score_b3" }, // assigns score_b2 into q1 (also adds a brand id of 2)
]

A stack entry field assignment can be an expression

"stack": [
{ "brand": "1", "score": "score_b1 * 100" },
{ "brand": "2", "score": "score_b2 * 100" },
{ "brand": "3", "score": "score_b3 * 100" },
]

Keep

Keep is optional. It is a convenient way to add expression(s) to every stack entry. You might use it if you have a long list of variables to keep and don't want to redundantly type them for every stack entry.

// using array "keep" (simple)
{
"keep": ["id"],
"stack": [
{ "brand": "1", "score": "score_b1" },
{ "brand": "2", "score": "score_b2" },
{ "brand": "3", "score": "score_b3" },
]
}

// equivalent syntax without using "keep"
{
"stack": [
{ "id": "id", "brand": "1", "score": "score_b1" },
{ "id": "id", "brand": "2", "score": "score_b2" },
{ "id": "id", "brand": "3", "score": "score_b3" },
]
}
// using object "keep" (calc in keep)
{
"keep": {
"id": "id",
"q2": "q2 * 100", // it can be an expression
},
"stack": [
{ "brand": "1", "score": "score_b1" },
{ "brand": "2", "score": "score_b2" },
{ "brand": "3", "score": "score_b3" },
]
}

// equivalent syntax without using "keep"
{
"stack": [
{ "id": "id", "q2": "q2 * 100", "brand": "1", "score": "score_b1" },
{ "id": "id", "q2": "q2 * 100", "brand": "2", "score": "score_b2" },
{ "id": "id", "q2": "q2 * 100", "brand": "3", "score": "score_b3" },
]
}

Integrated Meta

It may be convenient to add field meta, such as a label for new vars and values.

{
"keep": ["id"],
"stack": [
{ "brand": "1", "score": "score_b1" },
{ "brand": "2", "score": "score_b2" },
{ "brand": "3", "score": "score_b3" },
],
"meta": {
"brand": {
"label": "Brand",
"valueLabels": [
{ "value": 1, "label": "Acme" },
{ "value": 2, "label": "NSP" },
{ "value": 3, "label": "Netherbrand" }
]
},
"score": {
"label": "Brand Score"
}
}
}

Integrated Filter:

It may be convenient to filter out rows that have empty values.

// unpivot stage
{
"keep": ["id"],
"stack": [
{ "brand": "1", "score": "score_b1" },
{ "brand": "2", "score": "score_b2" },
{ "brand": "3", "score": "score_b3" },
],
"filter": [
"score is not null"
]
}