Jump to content

Recommended Posts

Posted

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?

Posted

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":

$signal.toCondition('Material')

image.thumb.png.a08a3b2693994b19a50691e9a9724a20.png

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)

image.thumb.png.5393fba186b89e68883ad40f17f38b5a.png

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

image.png.cfd8b4a941a0073bd2ec11234b74edd5.png

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()

image.thumb.png.08848abea1a21ecbab08939dde57dd9b.png

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

Regards,

Thorsten

 

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...