How to find bar nos for the 1st or 2nd occurance

My code has certain conditions, when these conditions are met- shapes are drawn using plotshape function.
I want to find out the bar nos from the last bar/beginning bar.
I am using following code, there is something not correct here, can you suggest the correction.
I can see system is plotting a shape from meeting the criteria, but bar nos is returned as 0.

cond=(b1==2 AND b2==2 AND b3==1);
Plotshapes(IIf(cond,shapedownarrow, Null),colorwhite, layer=0, yposition=su, offset=-14,XShift=-2);
 x=Cum(1);
 px1=LastValue(ValueWhen(cond,x,1)); // want to find the bar nos for the 1st occurance 
  _TRACE("This is a px1 :"+px1);  

Please close this as it is duplicate. Sorry for inconvenience.