Unpivoting · unpivot
---
config:
flowchart:
useMaxWidth: false
---
flowchart LR
classDef ok fill:#d4edda,stroke:#28a745,color:#000
classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
O["Unpivoting · unpivot<br/>🟢1"]:::ok
v_basic["DS#91;unpivot Id, Me#93;"]:::ok
v_basic --- O
click v_basic href "#form-basic"
Types
Takes any scalar · Gives any type
Comes from: any operator that returns a value
Goes to: whatever takes its input's type
Forms
DS[unpivot Id, Me] 🟢 · any scalar → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
unpivot Id_2, Me_1, on the Integer measuresA,BandC.- Manual:
Unpivoting, Example 1, output verbatim: “The VDS identifier is assigned a value equal to the name of the Measure M of op”, and “The VDS measure is assigned a value equal to the value of the Measure M of op”.
- Expected:
Six data points, one per data point and measure:
Id_2holdsA,BorCandMe_1its value, such as(1, A)= 5 and(2, C)= 9.
DS_r := DS_1 [ unpivot Id_2, Me_1 ];
| Id_1 | A | B | C |
|---|---|---|---|
| 1 | 5 | 2 | 7 |
| 2 | 3 | 4 | 9 |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | 5 |
| 2 | A | 3 |
| 1 | B | 2 |
| 2 | B | 4 |
| 1 | C | 7 |
| 2 | C | 9 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
unpivot Id_3, Me_2, on the measuresA(Number),BandC(Integer).- Manual:
Unpivoting, Example 2, output verbatim: “All the measures of op must have the same basic scalar type, including sub-types”, which accepts them, as Integer is a sub-type of Number. The manual’s
ds_2.jsonnames its datasetDS_1, a slip: the example loads it asDS_2, the name that the page and the script give it.- Expected:
Nine data points, with
Me_2typed Number, as the manual types it. The manual prints2.1for(6, A)with trailing spaces, which the importer stripped, as for every non-String value.
DS_r := DS_2 [ unpivot Id_3, Me_2 ];
| Id_1 | A | B | C |
|---|---|---|---|
| 4 | 3.1 | 1 | 6 |
| 5 | 4.2 | 2 | 3 |
| 6 | 2.1 | 3 | 2 |
| Id_1 | Id_3 | Me_2 |
|---|---|---|
| 4 | A | 3.1 |
| 5 | A | 4.2 |
| 6 | A | 2.1 |
| 4 | B | 1 |
| 5 | B | 2 |
| 6 | B | 3 |
| 4 | C | 6 |
| 5 | C | 3 |
| 6 | C | 2 |
Files: script.vtl · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Null measure skipped 🟢 pandas ✅ · DuckDB ✅
- Tests:
unpivot Id_2, Me_3, on a data point whoseMe_2is null.- Manual:
Unpivoting: “For each Data Point DP and for each Measure M of op whose value is not null, the operator inserts a Data Point into VDS”, and “When a Measure is null then unpivot does not create a Data Point for that Measure.”
- Expected:
(1, Me_1)= 5,(2, Me_1)= 3 and(2, Me_2)= 4, with no data point for the nullMe_2ofId_1= 1.Id_2holds component names, so it is a String, andMe_3has the type of the measures, Integer.
DS_r := DS_1 [ unpivot Id_2, Me_3 ];
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 5 | |
| 2 | 3 | 4 |
| Id_1 | Id_2 | Me_3 |
|---|---|---|
| 1 | Me_1 | 5 |
| 2 | Me_1 | 3 |
| 2 | Me_2 | 4 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗