I am in need of creating a function that can transform raw data into target data. The raw data consists of a table with columns for customer, product, and revenue, while the target data includes customer, revenue, and cumulative revenue.
customer | product | revenue |
---|---|---|
Customer A | Product 1 | EUR 10 |
Customer A | Product 2 | EUR 10 |
Customer B | Product 1 | EUR 5 |
Customer B | Product 2 | EUR 2 |
Customer C | Product 1 | EUR 5 |
customer | revenue | cumulative revenue |
---|---|---|
Customer A | EUR 20 | EUR 20 |
Customer B | EUR 7 | EUR 27 |
Customer C | EUR 5 | EUR 32 |
In PySpark, I have written code to achieve this transformation. However, I am not familiar with writing this type of function in TypeScript for front-end calculations "on the fly". Please see below for the PySpark code:
from pyspark.sql import functions as F, window as W
window = W.Window.partitionBy(F.col("helper")).orderBy(F.col("net_revenue").desc())
df = ( df .groupby("customer") .agg( F.sum("net_revenue").alias("net_revenue") ) .withColumn('helper', F.lit(1)) .withColumn( "cumulative_revenue", F.sum("net_revenue").over(window) ) )
Could you provide guidance on how to translate this functionality to TypeScript for front-end implementation?