I have the following information in a table
| equipment | run | runend | failure | removal_date |
| A | 1 | 1/1/2021 | 0 | 4/1/2021 |
| A | 2 | 2/1/2021 | 0 | 4/1/2021 |
| A | 3 | 3/1/2021 | 0 | 4/1/2021 |
| A | 4 | 4/1/2021 | 1 | 4/1/2021 |
| A | 5 | 4/1/2021 | 0 | 20/1/2021 |
| A | 6 | 10/1/2021 | 0 | 20/1/2021 |
I would like to create an extra column that has a count down to the failure point, so something that looks like this:
| equipment | run | runend | failure | removal_date | RUL |
| A | 1 | 1/1/2021 | 0 | 4/1/2021 | 3 |
| A | 2 | 2/1/2021 | 0 | 4/1/2021 | 2 |
| A | 3 | 3/1/2021 | 0 | 4/1/2021 | 1 |
| A | 4 | 4/1/2021 | 1 | 4/1/2021 | 0 |
| A | 5 | 4/1/2021 | 0 | 20/1/2021 | 16 |
| A | 6 | 10/1/2021 | 0 | 20/1/2021 | 10 |
So basically a count where each row is counted up until the runend which is closest to the removal_date shown in the table.
I think this can be achieved using a window function and I have managed to add a column which counts all of the rows for an equipment, but I am stuck on how I narrow this window down and then have a count where the first row actually takes the last count and works backwards. This is what I have so far:
w = Window.partitionBy("equipment", "run").orderBy(asc("runend"))
df = df.withColumn("rank", rank().over(w))
# Just to see what the df looks like
df.where(col("equipment") == "A").groupby("equipment", "run", "rank", "failure", "runend", "removal_date").count().orderBy("equipment", "runend").show()
So I get a table that looks like that, I think I am on the right track but still missing some parts
| equipment | run | runend | failure | removal_date | rank|
| A | 1 | 1/1/2021 | 0 | 4/1/2021 | 1 |
| A | 2 | 2/1/2021 | 0 | 4/1/2021 | 2 |
| A | 3 | 3/1/2021 | 0 | 4/1/2021 | 3 |
| A | 4 | 4/1/2021 | 1 | 4/1/2021 | 4 |
| A | 5 | 4/1/2021 | 0 | 20/1/2021 | 5 |
| A | 6 | 10/1/2021 | 0 | 20/1/2021 | 6 |