Last value · last_value
---
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
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_valueat data set level, over a window from one data point before to one after in the order ofId_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, 1994that is 7 and 5, while the greatestMe_2of the window is 9.
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 ) );
| Id_1 | Id_2 | Id_3 | Me_1 | Me_2 |
|---|---|---|---|---|
| A | XX | 1993 | 3 | 1 |
| A | XX | 1994 | 4 | 9 |
| A | XX | 1995 | 7 | 5 |
| A | XX | 1996 | 6 | 8 |
| A | YY | 1993 | 9 | 3 |
| A | YY | 1994 | 5 | 4 |
| A | YY | 1995 | 10 | 2 |
| A | YY | 1996 | 2 | 7 |
| Id_1 | Id_2 | Id_3 | Me_1 | Me_2 |
|---|---|---|---|---|
| A | XX | 1993 | 4 | 9 |
| A | XX | 1994 | 7 | 5 |
| A | XX | 1995 | 6 | 8 |
| A | XX | 1996 | 6 | 8 |
| A | YY | 1993 | 5 | 4 |
| A | YY | 1994 | 10 | 2 |
| A | YY | 1995 | 2 | 7 |
| A | YY | 1996 | 2 | 7 |
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_valueincalc, where the last value in the order ofId_2is 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 ofA(3, 1, 5, 1 in the order ofId_2) and 8 on those ofB, while the greatestMe_1ofAis 5.
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 ) ) ];
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 |
| A | 2020Q2 | 1 | 3.500000 |
| A | 2021Q1 | 5 | 2.500000 |
| A | 2021Q2 | 1 | 0.500000 |
| B | 2020Q1 | 4 | 6.500000 |
| B | 2020Q2 | 8 | 4.500000 |
| Id_1 | Id_2 | Me_1 | Me_2 | Me_3 |
|---|---|---|---|---|
| A | 2020Q1 | 3 | 1.500000 | 1 |
| A | 2020Q2 | 1 | 3.500000 | 1 |
| A | 2021Q1 | 5 | 2.500000 | 1 |
| A | 2021Q2 | 1 | 0.500000 | 1 |
| B | 2020Q1 | 4 | 6.500000 | 8 |
| B | 2020Q2 | 8 | 4.500000 | 8 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗