# String Value Comparison in AF Calculations

Blog Post created by John Messinger on May 27, 2019

In most types of calculations performed using AF, some form of value comparison is required, whether comparing numeric values, or states represented as Digital states and AF Enumeration values. Numeric comparisons are pretty straight forward, however it's not always immediately clear as to how comparisons involving non-numeric values should be performed.

This post demonstrates how to perform string value comparison in a number of scenarios using both the Formula Data Reference and Expression Analysis calculation methods. Discussion as to which is the better calculation choice is out of scope for this post - there are plenty of other discussions in PI Square that compare these options.

I have used the following sample data and configurations for demonstration purposes:

AFEnumeration Set for input

AFEnumeration Set for output result

Element configuration

## Using the Formula Data Reference

In the above Element, a Formula data reference has been used to calculate a traffic light status value for the two status attributes: Control Mode Status and Production Throughput Status.

Control Mode Status calculates a result based on the Controller Mode attribute value; the Controller Mode attribute data type is configured as an AFEnumeration set called ControllerMode. It's important to note that whilst the value in PSE appears to be text, the underlying enumeration value is an integer type. This behaviour is the same as digital states in the PI Data Archive.

The Control Mode Status attribute's data type is also configured as an AFEnumeration set called RAGStatus.

Let's look at the data reference configuration for the Control Mode Status attribute:

The basic logic of the above formula, translated into equivalent PE syntax for readability, is as follows:

If Controller Mode == "Manual" Then "Red" Else If Controller Mode == "Auto" Then "Green" Else "Amber"

It's important to note here that the Formula DR can't actually perform a comparison against string (text) values. This means that the above expression as given is invalid in a Formula DR configuration. As a side note, the Formula Data Reference also can't output a string result as given in the above expression.

When performing a comparison of an AFEnumeration value, there are two important things to know:

1. You must use the underlying numeric value, as per the configuration example in the screenshot above.
2. The Formula DR must return a numeric result.

Production Throughput Status calculates a result based on the current rate of production as shown in the Production Rate attribute. As the Production Rate is a numeric value to start with with, we have a pretty straightforward configuration in the Data Reference:

Due to truncation in the above screenshot, the full formula is:

if P < 25 then 0 else if P >= 25 and P < 75 then 1 else 2

In both of these examples, the output result is a number representing the appropriate AFEnumeration value from the RAGStatus Enumeration set.

There is a third input attribute in this Element, called Production State:

This attribute references a digital PI Point (BA:Active.1), and is configured as a String data type. It's not possible to use this attribute as an input parameter for the Formula DR - the following screenshot shows that the attribute doesn't even show up in the list of possible inputs:

If you need to perform a comparison against a digital PI Point value, then you would would need to configure the attribute as an Integer type (or use a child attribute to display the numeric representation), or alternatively create an AFEnumeration set that replicates the digital state set on the PI Data Archive server and use this as the attribute's data type. This is actually the approach I took with the Controller Mode attribute above - the PI Point DR references tag CDM158, and the ControllerMode enumeration set duplicates the Modes digital state set from the data archive.

One final note about using the Formula data reference. This data reference doesn't strictly use Performance Equation syntax

## Using AF Expression Analysis

AF Analyses provide much more flexibility in terms of comparing string values. The following example shows an Expression analysis to determine the status of the Controller Mode:

Removing the Formula DR from the Control Mode Status attribute shows that the above Analysis is able to determine the correct state:

As can be seen above, it's also possible to produce a string output result from an Expression analysis.

With the Expression Analysis, we can now also perform a comparison of the Production State attribute:

For the sake of completeness, here is the Production Throughput Status calculation implemented as an Expression Analysis:

## Conclusion

Comparing string values is a fairly common requirement in AF analytics, and so I would generally lean towards using Expression Analyses for this task. There are times however when an on-demand result may be needed and the Formula DR is chosen for this purpose. Knowing the limitations of the Formula DR in respect to string value comparison will help you determine whether using this is the right approach.