AF Analytics - Working with date-time arithmetic

Blog Post created by asoudek on Aug 31, 2016

Understand how to use date-times in your expressions

Using date-time in Analytic Expressions looks straight forward, but you can get yourself in a jam if you do not know the tricks.  Suppose you want to calculate the rate of change of an Attribute and also the duration over which this rate of change was computed.  I want to store these results as well.


In this example, we want to calculate the rate of change for the Volume1 Attribute and also the duration for this rate of change, and store these in other Attributes.  You would be tempted to use the following approach, see the Figure below.


The Variables st and et store the date-time of the archived values of the Volume1 Attribute.  The DeltaTime Variable is then just the difference between these two Variables.  The result is in dd:hh:mm:ss format.  We can use it in the ROC Variable calculation and we get a result.  Behind the scene the DeltaTime Variable was converted to seconds, so the resulting ROC Variable is in bbl/sec. This is fine, but we also wanted to store the DeltaTime Variable to an Attribute.  When you try this you get an error on the Element Attribute.


Try to change the unit of measure for the Raw Delta Time Attribute to DateTime, but then you get an error that the DateTime value type does not support UOMs.  Ay Caramaba.

No worries, the trick is to use the Float() function.  Use Float() to change the DeltaTime Variable to a double.  Then use the Convert() function to assign units of measure of hours to the DeltaTimeInHours Variable and perform the rate of change calculation.  See Figure below for details. You can now map the DeltaTimeInHours Variable to an Attribute with no problem.