For the cases where "Counter" is less than 5, you need to adjust the divisor down to the sum of the weights for each of those levels, so the code should look like:
If( :sample != Lag( :sample, 1 ) | Row() == 1,
counter = 0
);
counter = counter + 1;
If(
counter == 1, :Mean1,
counter == 2, Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4 ) / 5,
counter == 3, Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 6 ) / 11,
counter == 4,
Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 6, Lag( ::Mean1, 1 ) * 4 ) /
15,
Mean(
Lag( ::Mean1, 1 ) * 1,
Lag( ::Mean1, 1 ) * 4,
Lag( ::Mean1, 1 ) * 6,
Lag( ::Mean1, 1 ) * 4,
Lag( ::Mean1, 1 ) * 1
) / 16
);
Jim