Last value · last_value

🟢 2 of 2 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["Last value · last_value<br/>🟢2"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_analytic_ds["last_value(DS over (partition by Id order by Id data points ...))"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_analytic_comp["DS#91;calc Me #58;#61; last_value(Me over (partition by Id order by Id data points ...))#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_analytic_ds href "#form-analytic-ds"
    click v_analytic_comp href "#form-analytic-comp"
    click used__clause__calc href "../clause/calc.html" "Calculation of a Component · calc"
    

Types

Takes 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

last_value(DS over (partition by Id order by Id data points ...)) 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

last_value at data set level, over a window from one data point before to one after in the order of Id_3.

Manual:

Last value, Example 1, output verbatim: “The operator returns the last value (in the value order) of the set of Data Points that belong to the same analytic window as the current Data Point”.

Expected:

the values of the latest year in the window: 1993 gets those of 1994, 1994 those of 1995, 1995 and 1996 those of 1996; at A, XX, 1994 that is 7 and 5, while the greatest Me_2 of the window is 9.

script.vtl
DS_r := last_value ( DS_1 over ( partition by Id_1, Id_2 order by Id_3 data points between 1 preceding and 1 following ) );
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
AXX199349
AXX199475
AXX199568
AXX199668
AYY199354
AYY1994102
AYY199527
AYY199627

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

DS[calc Me := last_value(Me over (partition by Id order by Id data points ...))] 🟢 · any scalar → any type

Last in order not greatest 🟢 pandas ✅ · DuckDB ✅
Tests:

last_value in calc, where the last value in the order of Id_2 is not the greatest.

Manual:

Last value: “The operator returns the last value (in the value order) of the set of Data Points that belong to the same analytic window as the current Data Point”; its Example 1 takes the last value in the order of the order clause, not the greatest. The window is written as the whole partition, as the Analytic invocation gives two defaults for an omitted window with an order clause.

Expected:

Me_3 = 1 on the data points of A (3, 1, 5, 1 in the order of Id_2) and 8 on those of B, while the greatest Me_1 of A is 5.

script.vtl
DS_r := DS_1 [ calc Me_3 := last_value ( Me_1 over ( partition by Id_1 order by Id_2 data points between unbounded preceding and unbounded following ) ) ];
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.5000001
A2020Q213.5000001
A2021Q152.5000001
A2021Q210.5000001
B2020Q146.5000008
B2020Q284.5000008

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