Lag · lag

🟢 4 of 4 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · 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
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    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["Lag · lag<br/>🟢4"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_no_offset["lag(DS over (partition by Id order by Id))"]:::ok
        v_ds_offset["lag(DS, n over (partition by Id order by Id))"]:::ok
        v_ds_offset_default["lag(DS, n, default over (partition by Id order by Id))"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_offset["DS#91;calc Me #58;#61; lag(Me, n over (partition by Id order by Id))#93;"]:::ok
    end
    subgraph G_use["Used with"]
        direction LR
        used__clause__calc["calc ↗<br/><small>used in over()</small>"]:::ext
    end
    O --- G_ds
    O --- G_comp
    O -.- G_use
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
    click v_ds_no_offset href "#form-ds-no-offset"
    click v_ds_offset href "#form-ds-offset"
    click v_ds_offset_default href "#form-ds-offset-default"
    click v_comp_offset href "#form-comp-offset"
    click used__clause__calc href "../clause/calc.html" "Calculation of a Component · calc"
    

Types

Takes integer, any scalar · Gives any type

Viral attributes: combined with the propagation rules → Viral propagation

Comes from: any operator that returns a value

Goes to: whatever takes its input's type

Forms

lag(DS over (partition by Id order by Id)) 🟢 · any scalar → any type

No default offset 🟢 pandas ✅ · DuckDB ✅
Tests:

lag at data set level without an offset, which its syntax makes optional.

Manual:

Lag: “the operator returns the value(s) taken from the Data Point at the specified physical offset prior to the current Data Point”; the page does not say which offset applies when it is omitted.

Expected:

the error 0-5-1-3, “{op} requires an offset parameter”: with no offset there is no “specified physical offset”, and the page gives no default.

script.vtl
DS_r := lag ( DS_1 over ( partition by Id_1 order by Id_2 ) );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000

Expected error 0-5-1-3: {op} requires an offset parameter.

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/error.json · example page ↗

lag(DS, n over (partition by Id order by Id)) 🟢 · any scalar, integer → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

lag with offset 1 at data set level, partitioned by Id_1 and Id_2 and ordered by Id_3.

Manual:

Lag, Example 1, output verbatim: “If defaultValue is not specified then the value returned when the offset goes outside the partition is null”.

Expected:

each data point gets the values of the year before; 1993, the first of each partition, gets null.

script.vtl
DS_r := lag ( DS_1 , 1 over ( partition by Id_1 , Id_2 order by Id_3 ) );
Input DS_1
Id_1Id_2Id_3Me_1Me_2
AXX199331
AXX199449
AXX199575
AXX199668
AYY199393
AYY199454
AYY1995102
AYY199627
Expected DS_r
Id_1Id_2Id_3Me_1Me_2
AXX1993
AXX199431
AXX199549
AXX199675
AYY1993
AYY199493
AYY199554
AYY1996102

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

lag(DS, n, default over (partition by Id order by Id)) 🟢 · any scalar, integer → any type

Offset two default zero 🟢 pandas ✅ · DuckDB ✅
Tests:

lag at data set level with offset 2 and the default value 0.

Manual:

Lag: “the operator returns the value(s) taken from the Data Point at the specified physical offset prior to the current Data Point”, and the default value is “the value returned when the offset goes outside of the partition”.

Expected:

each data point gets the values of the one two before it; where that is outside the partition, the first two of A and both of B, both measures are 0.

script.vtl
DS_r := lag ( DS_1 , 2 , 0 over ( partition by Id_1 order by Id_2 ) );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2
A2020Q100
A2020Q200
A2021Q131.500000
A2021Q213.500000
B2020Q100
B2020Q200

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

DS[calc Me := lag(Me, n over (partition by Id order by Id))] 🟢 · any scalar, integer → any type

Previous value or null 🟢 pandas ✅ · DuckDB ✅
Tests:

lag with offset 1 in calc, partitioned by Id_1 and ordered by Id_2.

Manual:

Lag: “If defaultValue is not specified then the value returned when the offset goes outside the partition is null”.

Expected:

Me_3 is the Me_1 of the data point before: null, 3, 1, 5 in A and null, 4 in B.

script.vtl
DS_r := DS_1 [ calc Me_3 := lag ( Me_1 , 1 over ( partition by Id_1 order by Id_2 ) ) ];
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2Me_3
A2020Q131.500000
A2020Q213.5000003
A2021Q152.5000001
A2021Q210.5000005
B2020Q146.500000
B2020Q284.5000004

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