Jump to content

Capsule that changes based on current value


Recommended Posts

I am trying to create a capsule for the last year that changes based on the current value. I have a data item that is a certain product code that changes on no set interval. I want to be able to automatically calculate the average value of a different data item during all the instances of the given product for the last year.

Example: Currently making product A. Formula should calculate the average temperature of all the times we were making product A in the last year. Tomorrow making product B. The same formula should calculate the average temperature of all the times making product B in the last year.

Rather than manually creating capsules for each product, I would like a formula that will do this because of the large number of different products.

How can I do this?

Link to comment
Share on other sites

Hello Kspring,

I think you can do it that way:

First create a condition based on the material signal by using toCondition(). The function creates a capsule everytime the value of the signal changes and stores the value in a defined property, in this case "Material":



Next filter only for batches where the current material is produced using formula:

//Create capsule of 1 day ending now
$capsule = capsule(now()-1d, now())

//Get current material by selecting last value inside capsule created above
$currentMaterial = $gradeCode.toScalars($capsule).last()

//Only keep batches for current Material
$batches.removeLongerThan(1mo).keep($c -> $c.property('Material') == $currentMaterial)


Then create the condition the time range you want to aggregate over. Here I am using a monthly condition:


Last step is calculating the value. The within() function is used to only keep that portions of the signal during the specified condition.

$temperature.within($filteredBatches).aggregate(average(), $month, middleKey()).toDiscrete()


Depending on your data you might need to do some adjustments on the timeranges I used in the formulas.




Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...