Lead · lead
---
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["Lead · lead<br/>🟢4"]:::ok
subgraph G_ds["Dataset"]
direction LR
v_ds_no_offset["lead(DS over (partition by Id order by Id))"]:::ok
v_ds_offset["lead(DS, n over (partition by Id order by Id))"]:::ok
v_ds_offset_default["lead(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; lead(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
Comes from: any operator that returns a value
Goes to: whatever takes its input's type
Forms
lead(DS over (partition by Id order by Id)) 🟢 · any scalar → any type
No default offset 🟢 pandas ✅ · DuckDB ✅
- Tests:
leadat data set level without an offset, which its syntax makes optional.- Manual:
Lead: “the operator returns the value(s) taken from the Data Point at the specified physical offset beyond 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.
DS_r := lead ( DS_1 over ( partition by Id_1 order by Id_2 ) );
| 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 |
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 ↗
lead(DS, n over (partition by Id order by Id)) 🟢 · any scalar, integer → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
leadwith offset 1 at data set level, partitioned byId_1andId_2and ordered byId_3.- Manual:
Lead, 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 after; 1996, the last of each partition, gets null.
DS_r := lead ( DS_1 , 1 over ( partition by Id_1 , Id_2 order by Id_3 ) );
| 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 | ||
| A | YY | 1993 | 5 | 4 |
| A | YY | 1994 | 10 | 2 |
| A | YY | 1995 | 2 | 7 |
| A | YY | 1996 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
lead(DS, n, default over (partition by Id order by Id)) 🟢 · any scalar, integer → any type
Offset two default zero 🟢 pandas ✅ · DuckDB ✅
- Tests:
leadat data set level with offset 2 and the default value 0.- Manual:
Lead: “the operator returns the value(s) taken from the Data Point at the specified physical offset beyond 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 after it; where that is outside the partition, the last two of
Aand both ofB, both measures are 0.
DS_r := lead ( DS_1 , 2 , 0 over ( partition by Id_1 order by Id_2 ) );
| 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 |
|---|---|---|---|
| A | 2020Q1 | 5 | 2.500000 |
| A | 2020Q2 | 1 | 0.500000 |
| A | 2021Q1 | 0 | 0 |
| A | 2021Q2 | 0 | 0 |
| B | 2020Q1 | 0 | 0 |
| B | 2020Q2 | 0 | 0 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
DS[calc Me := lead(Me, n over (partition by Id order by Id))] 🟢 · any scalar, integer → any type
Next value or null 🟢 pandas ✅ · DuckDB ✅
- Tests:
leadwith offset 1 incalc, partitioned byId_1and ordered byId_2.- Manual:
Lead: “If defaultValue is not specified then the value returned when the offset goes outside the partition is null”.
- Expected:
Me_3is theMe_1of the data point after: 1, 5, 1, null inAand 8, null inB.
DS_r := DS_1 [ calc Me_3 := lead ( Me_1 , 1 over ( partition by Id_1 order by Id_2 ) ) ];
| 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 | 5 |
| A | 2021Q1 | 5 | 2.500000 | 1 |
| A | 2021Q2 | 1 | 0.500000 | |
| B | 2020Q1 | 4 | 6.500000 | 8 |
| B | 2020Q2 | 8 | 4.500000 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗