Pivoting · pivot

🟢 1 of 1 form all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
        ---
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["Pivoting · pivot<br/>🟢1"]:::ok
    v_basic["DS#91;pivot 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[pivot Id, Me] 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

pivot Id_2, Me_1, on a data set with the attribute At_1, whose virality is not defined.

Manual:

Pivoting, Example 1, output verbatim: “It drops the Identifier Component identifier and all the Measure Components from VDS”, then “It groups VDS by the values of the remaining Identifiers”.

Expected:

One data point per Id_1, with the Measures A, B and C: 5, 2, 7 and 3, 4, 9. At_1 is not kept: each group holds several of its values and the script gives “no explicit indication about the keeping of the Attribute”, so by the User Manual “If the virality is not defined, the Attribute is considered as non-viral” and “the non-viral Attributes are not kept in the result and their values are not considered”.

Erratum:

The page adds a Measure “for each distinct value of identifier in op” and types Id_2 String, but the Measures must be known without the data: they are the values of the value domain of Id_2 (1-1-6-18 without one), here VD_1, of A, B and C.

script.vtl
DS_r := DS_1 [ pivot Id_2, Me_1 ];
Input DS_1
Id_1Id_2Me_1At_1
1A5E
1B2F
1C7F
2A3E
2B4E
2C9F
Expected DS_r
Id_1ABC
1527
2349

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · value_domains/VD_1.json · example page ↗

Missing combination is null 🟢 pandas ✅ · DuckDB ✅
Tests:

pivot Id_2, Me_1, where Id_1 = 2 has no data point with Id_2 = B. Id_2 is defined on the value domain VD_1, of A and B, whose values give the Measures of the result.

Manual:

Pivoting: “For each distinct value of identifier in op, it adds a corresponding measure to VDS, named as the value of identifier. These Measures are initialized with the null value.”

Expected:

Id_1 and the Measures A and B: 5 and 2 for Id_1 = 1, and 3 and null for Id_1 = 2, whose B keeps the null it was initialized with.

script.vtl
DS_r := DS_1 [ pivot Id_2, Me_1 ];
Input DS_1
Id_1Id_2Me_1
1A5
1B2
2A3
Expected DS_r
Id_1AB
152
23

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · value_domains/VD_1.json · example page ↗