• Last maximum value is a bit contradictory.

    You either want the last non-zero value, or you want the maximum value if it is non-zero.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP