The formula is exactly the same as what you would put into Excel. The differences are that you need specify the first row in the formula and you need to know exactly what the Excel autofill is doing (which is not always straight-forward).
Interactively, it would look like this:
The JSL code would look like this:
If(
Row() == 1, 0,
:A[Row()] != Lag( :A, 1 ), If( Lag( :B, 1 ) == 0, 1, 0 ),
Lag( :B, 1 )
)
Or just use Mark's formula which looks simpler!
Dan Obermiller