You may have experienced that when working with stepped tags in formulas, your output is not quite what you were expecting. As an easy example, let's look at a batch process where we have a a discrete tag with the quality (top), and a digital tag with the product name (middle). Suppose now we want to create a tag that only shows the quality when the product is Alpha, you might think to implement it as in the screenshot below. However, as you can see, the result (bottom tag) is not quite what you would be expecting:
So why we do we get this sawtooth pattern? The first thing to understand is that our resulting formula tag only gets calculated where one of our input tags has a datapoint. Where there is a datapoint for any of the input tags, values for the other input tags are obtained through interpolation. The second factor that gives us this result in that formula tags are always of the analog type, meaning the interpolation is linear. Since our underlying discrete tags are sparse (they only have a datapoint when the value changes), our formula only gets calculated at a handful of timestamps. These datapoints are then connected via linear interpolation, giving us the sawtooth pattern above.
The solution to this issue is to make sure our formula gets calculated at more points. More concretely, we need to make sure our formula is also calculated just before one of the stepped tags has a point. To do that, we would need to add tags to our formula that always have a point just before the stepped input tags. Luckily such tags always exist: we can simply take the stepped tags again, and shift them by -1s, and they will always have a point 1 second before the base tag. To not have them influence the actual outcome of the calculations, we can simply multiply them by 0 in our formula. Using this trick gives us the expected result:
This trick should see you through for just about any formula that uses sparse stepped tags. Just remember that we always need to add a shifted version of all stepped tags in our formula. Also, if a stepped tag already has a shift in the base formula, simply subtract 1s from that shift for the additional variable (e.g., 5m becomes 4m59s, -5m becomes -5m1s, ...).