← lag operator page · aggregate/lag/ds_offset/manual_ex_1
pandas ✅ · DuckDB ✅
lag with offset 1 at data set level, partitioned by Id_1 and Id_2 and ordered by Id_3.
Lag, Example 1, output verbatim: "If defaultValue is not specified then the value returned when the offset goes outside the partition is null".
each data point gets the values of the year before; 1993, the first of each partition, gets null.
DS_r := lag ( 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 |
{
"datasets": [
{
"name": "DS_1",
"DataStructure": [
{"name": "Id_1", "type": "String", "role": "Identifier", "nullable": false},
{"name": "Id_2", "type": "String", "role": "Identifier", "nullable": false},
{"name": "Id_3", "type": "Time_Period", "role": "Identifier", "nullable": false},
{"name": "Me_1", "type": "Integer", "role": "Measure", "nullable": true},
{"name": "Me_2", "type": "Integer", "role": "Measure", "nullable": true}
]
}
]
}
| Id_1 | Id_2 | Id_3 | Me_1 | Me_2 |
|---|---|---|---|---|
| A | XX | 1993 | ||
| A | XX | 1994 | 3 | 1 |
| A | XX | 1995 | 4 | 9 |
| A | XX | 1996 | 7 | 5 |
| A | YY | 1993 | ||
| A | YY | 1994 | 9 | 3 |
| A | YY | 1995 | 5 | 4 |
| A | YY | 1996 | 10 | 2 |
{
"datasets": [
{
"name": "DS_r",
"DataStructure": [
{"name": "Id_1", "type": "String", "role": "Identifier"},
{"name": "Id_2", "type": "String", "role": "Identifier"},
{"name": "Id_3", "type": "Time_Period", "role": "Identifier"},
{"name": "Me_1", "type": "Integer", "role": "Measure"},
{"name": "Me_2", "type": "Integer", "role": "Measure"}
]
}
]
}