Spark Dataframe windowing last value function




I am trying the use “last” windowing analytical function for getting the last value of the window but its not getting the value properly.

suppose if I have a five rows in a window which I have defined and last value of one column is “Blue”. So I am adding a column to the dataframe withColumn to update this last value for all the rows in a given window.

but it is not updating the last value for all the rows and looks like its taking previous value and updating the row.

Any suggestions to update the last column value to all the rows in a window in spark dataframe.