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