# Create condition for past N batches

Go to solution Solved by John Brezovec,

## Recommended Posts

I am trying to create a condition that displays most recent 20 batches in the last month. Is there a good way to do this? I am currently using the below formula, but I get the error Uncertain Scalars cannot be used as thresholds for ScalarPredicates at 'isGreaterThanOrEqualTo', because it seems my \$earliest_start is always an uncertain value.

\$capsule = capsule(now() - 1month, now())
\$selected = \$batch_condition.toGroup(\$capsule,CapsuleBoundary.EnclosedBy).pick(-20)
\$earliest_start = \$selected.startKey()
\$batch_condition.removeLongerThan(40h).keep('Start',isGreaterThanOrEqualTo(\$earliest_start))

Is there any way I can fix this issue or get the most recent 20 batches another way?

##### Share on other sites

• Seeq Team
• Solution

Here's an alternative method to getting the last X batches in the last 30 days:

```// return X most recent batches in the past Y amount of time
\$numBatches = 20 // X
\$lookback = 1mo // Y

// create a rolling condition with capsules that contain X adjacent capsules
\$rollingBatches = \$batchCondition.removeLongerThan(\$lookback)
.toCapsulesByCount(\$numBatches, \$lookback)

// find the last capsule in the rolling condition that's within the lookback period
\$currentLookback = capsule(now()-\$lookback, now())
\$batchWindow = condition(
\$lookback,
\$rollingBatches.toGroup(\$currentLookback, CAPSULEBOUNDARY.ENDSIN).last()
)

// find all the batches within the capsule identified
// ensure all the batches are within the lookback period
\$batchCondition.inside(\$batchWindow)
.touches(condition(\$lookback, \$currentLookback))```

This is similar to yours in that it uses toGroup, but the key is in the use of toCapsulesByCount as a way to get a grouping of X capsules in a condition.

You can see an example output below. All capsules will show up as hollow because by the nature of the rolling 'Last X days' the result will always be uncertain.

Edited by John B
formula was incomplete
• 1
• 2
##### Share on other sites

Thanks! Is it possible to do it in a way that does not require adjacent batches? Sometimes we switch products, so I would like to create a capsule that contains the last N batches of the same product, which may not be continuous.

I feel like there must be some way to do it with condition table formulas and top(), but I can't figure out how to convert my table back to capsules.

##### Share on other sites

• Seeq Team

If I'm understanding you correctly, you want to find the last N batches that were ran that are of the same product as the most recent batch, correct?

We can do that with some more creative condition logic and filtering 🙂

Assuming that our batch capsules have a property called 'Product' that tells us what product that batch ran, we can add some filtering to our above formula to only pass in capsules to toCapsulesByCount() that match the last product produced:

```// return X most recent batches in the past Y amount of time
\$numBatches = 3 // X
\$lookback = 3mo // Y

\$currentLookback = capsule(now()-\$lookback, now())

// find the product of the most recent batch and assign that
// as a capsule property to the lookback condition
// this will let us filter the batches condition dynamically
\$lookbackCond = condition(\$lookback, \$currentLookback)
.setProperty(
'Product',
\$batchCondition.toSignal('Product', endKey()),
endValue(true)
)

// filter the batch condition to only find batches of the active product
\$filteredBatches = \$batchCondition.removeLongerThan(\$lookback)
.touches(\$lookbackCond, 'Product')

// create a rolling condition with capsules that contain X adjacent capsules
\$rollingBatches = \$filteredBatches.toCapsulesByCount(\$numBatches, \$lookback)

// find the last capsule in the rolling condition that's within the lookback period
\$batchWindow = condition(
\$lookback,
\$rollingBatches.toGroup(\$currentLookback, CAPSULEBOUNDARY.ENDSIN).last()
)

// find all the batches within the capsule identified
// ensure all the batches are within the lookback period
\$filteredBatches.inside(\$batchWindow)
.touches(condition(\$lookback, \$currentLookback))```

The key here is to pass a capsule property name to touches() to allow filtering with this dynamic property value (the keep() function requires the input comparison scalar to be certain).

The output will look something like this -- note I'm only getting the 3 more recent batches so you can actually read the property labels 🙂

• 1