Analytics Logic

Calculation logic in Analytics is used to transform data during Data Loads which generate new lines (Flush Data Load) or modify data (Calculation Data Load) in Data Sources or Datamarts.

The Data Load calculation defines:

  • The scope of the data, by means of a set of projections and filters.

  • The order in which the data is to be processed (sort criteria).

  • The logic that calculates additional or updated field values.

Projections are specified in the logic. Filter and sort criteria are defined in the web client, or other client producing equivalent API requests.

The Calculation Data Load has three phases:

  • Initial – Modifies the input rows, either filters those coming from the source or creates new ones. It is executed once at the beginning of the process when, for instance, global state can be initialized.

  • Row – Modifies the values. The logic is evaluated for each data row in scope (as defined by the filter criteria), and in the given order (as defined by the sort criteria). For any given row, the inputs are mapped from the Datamart field values of that row, if not overridden to fixed values in the calculation definition’s input section. The logic outputs are written to the row’s matching fields (matched by name, and only if Display Mode is not set to Never). Only the logic elements with the Row group are mapped from and to Datamart fields.

  • Summary – Modifies the target rowset. It is called only once, after all the data rows have been processed. This is the place, for example, to store calculations such as running totals, etc.

Simple Logic

In the simplest case, the processing of each row is independent of any other row in the Datamart. In this situation, there is no need to preserve state between logic evaluations. The inputs to the logic for each row are mapped from the corresponding Datamart field values. The output fields are set to the matching logic elements’ results. An example could be to enrich a Datamart with a List Price, looked up from Price Records (from approved Quotes). The list price for a given transaction line would be independent of any other transaction.

Advanced Logic

In more complex scenarios, it may be required for one data row, or one row’s calculation result, to input into another row’s calculation. As with any logic in Pricefx, it is possible to preserve state across logic evaluations (and indeed logic element evaluations), in so called ‘global variables’. Global variables can only be stored and retrieved in ‘script’ logic elements, i.e., those written in the Groovy language. In a Groovy script, you can use the logic API, which provides access to context specific data and processing capabilities, in addition to a library of general purpose helper functions.

One such general tool is the global variables map ‘api.global’. Another, in the context of a Datamart calculation, is the DatamartRowSet. In some cases, the api.global map will suffice, for instance, if each row is to be given an incremental sequence number (if for some reason it was not possible to do this in the ETL stage). In other cases, a more advanced capability may be required: for each row to have access to the full Datamart data set (in scope), either read only (each row updating only its own output fields), or with write access (one row evaluation triggering a need to find another row and update that row’s outputs).

The calculation specification cannot define any new fields. All fields that will be set by the calculation logic, must be predefined in the Datamart. Potentially, this requires ‘calculated’ fields to be added to the Datamart (in the schema definition), without an actual calculation expression. Such fields are neither populated from Data Source data, nor are calculated on the fly as some expression of other, persisted, fields. They merely serve as placeholders for outputs calculated by the process.

To set up a calculation logic:

  1. Go to Administration > Logics > Analytics.

  2. Click the Add button and fill in the fields in the dialog.

  3. Click the name of the logic in the list.
    Configuration of the Data Load logics is mostly the same as in the generic calculation logic. The specifics are that the element can be assigned to three groups: Init, Row, Summary (in the Calculation Context column). If you do not assign the element to one of the groups explicitly, it will be included in all the groups.

  4. After you save the logic, it is ready to be used in the Calculation step of the Data Load setup.

Found an issue in documentation? Write to us.

 
Pricefx version 12.0