Variable as a condition

Hey guys I am struggling with one equation right now, that I need for my bachelor thesis. :confused:

 
 Variable x(mth);
 Scalar t(w);
 binary variable (y(w,mon))
 set i,w,mon;
 alias (mon,mth);
 
 ....
 
 Ver(i,w,mon).. ord(i)*t(w)-1+(M*Y(w,mon)) =G= sum( mth$(ord(mth) le ord(mon)) ,x(mth))

This is the equation right now, but (on the right side) I actually don’t need the sum from mth=1,…,mth=mon but instead I need the sum from the mon where y was equal to 1 for the last time until mth=mon, if that makes sense. Is this possible?

So let’s say we have
y(1)=0, y(2)=1, y(3)=0, y(4)=1, y(5)=0

I would need the following sum to be in the equation
… =G= x(1)+x(2)
… =G= x(3)+x(4)
… =G= x(5)

Obviously the equations would change with the y(mon).

Thanks a lot in advance!