Cumulative sum by index, range limited with a single value from a table I calculate cumulative sum using the following code and it works just fine: