I have this dataframe:
+------+-------------------+------+----------+------+
|catulz| hatulz|ccontr| dmovto|amount|
+------+-------------------+------+----------+------+
| I|1900-01-01 16:00:00| 123|2022-09-01|300.00|
| U|1900-01-01 17:00:00| 123|2022-09-02|500.00|
| I|1900-01-01 16:00:00| 123|2022-09-02|150.00|
| U|1900-01-01 18:00:00| 123|2022-09-03|500.00|
| I|1900-01-01 16:00:00| 123|2022-09-03|150.00|
| I|1900-01-01 16:00:00| 123|2022-09-04|150.00|
| U|1900-01-01 19:00:00| 123|2022-09-04|150.00|
| I|1900-01-01 16:00:00| 123|2022-09-05|150.00|
| I|1900-01-01 16:00:00| 123|2022-09-06|150.00|
| I|1900-01-01 16:00:00| 123|2022-09-07|150.00|
+------+-------------------+------+----------+------+
I need get the amount with this rules:
- If I have only one day in dmovto, take the row's amount;
- When I have the same day in dmovto:
- Look by PREVIOUS partition by ccontr + dmovto ; if I have a row with "U", get the amount. Otherwise, get the amount with "I"
Like that:
+------+-------------------+------+----------+------+----------+
|catulz| hatulz|ccontr| dmovto|amount|new_amount|
+------+-------------------+------+----------+------+----------+
| I|1900-01-01 16:00:00| 123|2022-09-01|300.00| 300.00 |
| U|1900-01-01 17:00:00| 123|2022-09-02|500.00| 300.00 |
| I|1900-01-01 16:00:00| 123|2022-09-02|150.00| 300.00 |
| U|1900-01-01 18:00:00| 123|2022-09-03|500.00| 500.00 |
| I|1900-01-01 16:00:00| 123|2022-09-03|150.00| 500.00 |
| I|1900-01-01 16:00:00| 123|2022-09-04|150.00| 500.00 |
| U|1900-01-01 19:00:00| 123|2022-09-04|150.00| 500.00 |
| I|1900-01-01 16:00:00| 123|2022-09-05|150.00| 150.00 |
| I|1900-01-01 16:00:00| 123|2022-09-06|150.00| 150.00 |
| I|1900-01-01 16:00:00| 123|2022-09-07|150.00| 150.00 |
+------+-------------------+------+----------+------+----------+
PS: "U" is from "updated", and it's the priority. If I create a window by "ccontr" + "dmovto" and order by "hatulz", it could work
I tryed create a Window.partitionBy(["ccontr","dmovto"]).orderBy("hatulz") and use lag and last but without success
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
from pyspark.sql.types import StructType,StructField, IntegerType, StringType, DateType, DecimalType, \
StringType, TimestampType
from datetime import datetime
from decimal import Decimal
from pyspark.sql.window import Window
from pyspark.sql.functions import avg,col
spark = SparkSession.builder.master("local[4]").appName("tests").getOrCreate()
vdata = [
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-01','%Y-%m-%d'),Decimal(300)),
('U',datetime.strptime('17:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-02','%Y-%m-%d'),Decimal(500)),
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-02','%Y-%m-%d'),Decimal(150)),
('U',datetime.strptime('18:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-03','%Y-%m-%d'),Decimal(500)),
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-03','%Y-%m-%d'),Decimal(150)),
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-04','%Y-%m-%d'),Decimal(150)),
('U',datetime.strptime('19:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-04','%Y-%m-%d'),Decimal(150)),
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-05','%Y-%m-%d'),Decimal(150)),
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-06','%Y-%m-%d'),Decimal(150)),
('I',datetime.strptime('16:00:00:00',"%H:%M:%S:%f"),123,datetime.strptime('2022-09-07','%Y-%m-%d'),Decimal(150)),
]
schema = StructType([
StructField("catulz",StringType(),False),
StructField("hatulz",TimestampType(),False),
StructField("ccontr",IntegerType(),False),
StructField("dmovto",DateType(),False),
StructField("amount",DecimalType(10,2),False)])
df = spark.createDataFrame(vdata,schema)