First value · first_value

🟢 2 of 2 forms 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
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["First value · first_value<br/>🟢2"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_analytic_ds["first_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; first_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

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

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

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

Manual:

First value, Example 1, output verbatim: “The operator returns the first 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 earliest year in the window: 1993 and 1994 get those of 1993, 1995 those of 1994 and 1996 those of 1995; at A, XX, 1995 that is 4 and 9, while the smallest Me_2 of the window is 5.

script.vtl
DS_r := first_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
AXX199331
AXX199431
AXX199549
AXX199675
AYY199393
AYY199493
AYY199554
AYY1996102

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

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

First in order not smallest 🟢 pandas ✅ · DuckDB ✅
Tests:

first_value in calc, where the first value in the order of Id_2 is not the smallest.

Manual:

First value: “The operator returns the first 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 first value in the order of the order clause, not the smallest. 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 = 3 on the data points of A (3, 1, 5, 1 in the order of Id_2) and 4 on those of B, while the smallest Me_1 of A is 1.

script.vtl
DS_r := DS_1 [ calc Me_3 := first_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.5000003
A2020Q213.5000003
A2021Q152.5000003
A2021Q210.5000003
B2020Q146.5000004
B2020Q284.5000004

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