Introduction
Enriching data is at the heart of analysis modelling from adding additional data and extrapolating information to performing complex calculations and deduplication, PhixFlow covers it all. In this page we will highlight some of the key areas of enrichment, provide examples, and list the links to additional useful enrichment resources.
PhixScript Functions
PhixScript is the language of PhixFlow and it can be used anywhere that supports expressions, such as attributes on a table and in filters.
There are over 115 functions available and these are listed in Functions. but to help you get started here is a short list of commonly used functions:
Common Functions
- Comments can be added to a single line using
//
or to a section using/* */
. - if: used where you need to evaluate a simple condition before processing an expression.
- ifNull: If its first value is provided then this is the returned value, else it will return the second value.
- switch: evaluates a set of conditions in turn, and returns the associated result for the first match.
- forEach: Iterates over an Array or Recordset and processes a script for each entry.
- listToString: takes a list and returns a string delimited by a comma.
- contains: Used on a string will return true (1), if a string contains another specified string.
- listContains: Used on a list returns the position of a entry in a list (with 1 being the first item) if the value being looked for is found in the list, and false (0) otherwise.
- replaceAll: Replace all occurrences of a pattern with a replacement string. Also see replaceFirst.
- dateDiff: Return the time difference between two dates in milliseconds.
- dateAdd: Add or subtract from a date using a specified unit e.g. _MONTH.
- now: Returns the current day and time, and today returns just the day.
- _NULL: is how to declare a null value e.g. $output = _NULL.
- substring: Returns the portion of the supplied string between specified character positions, counting from 1.
- stringLength: Returns the length of a string.
- trim: Removes leading and trailing white spaces from a string, returning the cleansed string.
- toString: Converts a value from select data types into a string. For example, a date.
- toDate: Converts a string in a recognised date format (e.g. 20120521 or 20120521.172108) into a date.
Debugging
- debug: Adds a debug message into the Log. This function is often used when creating a model and trying to work out why an Expression is not behaving as expected.
- error: Adds a error message into the Log and displays the message to the user.
Writing PhixScript
Where a PhixScript is more than a single function, it must be wrapped in a do()
function. do()
can also be used within functions where you need to carryout multiple functions such as in if
or switch
.
Single Function PhixScript
This is used in attribute expressions and filters alike where a single function is called.
// If ExamResult is greater than 95 then return "Distinction" else return "Pass" if( in.ExamResult > 95, "Distinction", "Pass")
Multiple Function PhixScript
PhixScripts containing multiple functions must be wrapped in a do(). For more information see do().
The value returned from the PhixScript is the final value output, therefore it is useful to add the desired output to the end of the script.
Note that variables declared in an attribute expression (variables are declared with a $ symbol with the data type being implied) will be accessible to subsequent attributes within the same table by using the $variable name.
do( // calculate the miles per gallon. $mpg = in.distance / in.gallonsUsed, // if $mpg is over 65 set efficiency to Highly Efficient if( $mpg > 65, $efficiency = "Highly Efficient", // ELSE, set $efficiency to be null $efficiency = _NULL ), // Set the value returned by the PhixScript $efficiency )
Variables
PhixFlow contains a number of Internal Variables available to PhixScript used for obtaining system and user information.
Variables in PhixFlow are declared using a $ variable. The data type is implied by the first value entered, for example:
$myArray = []
. Creates an array.$myString = "Hello World"
. Create a string.$myDate = toDate("20120521")
. Create a date.$myNumber = 42
. Create a number
Lookup Information
Lookups can be performed using three different techniques, the method selected depends on your requirements.
Lookup Function with Filtering
Scenario
Lookup information from a separate table and pass filtering information dynamically to return only selected records.
Example
You need to retrieve all invoices for a specific date range that have not been sent.
Solution
Show one with _out.someting - Typically running a model
Show one with $variable - If your using an action and dynamically setting the $variable.
Lookup Function with Order/Index
Scenario
This is a highly efficient method for performing a lookup against a small set of records. This kind of lookup will automatically cache a set amount of records, 3000 by default, in memory allowing for faster lookups.
Example
You have a product code on an invoice and want to return the name of the product to display. There are a finite amount of products.
Solution
Merges
Matching
Simple exact matches and make reference to addition al functions for more advanced matching see Lev Distance etc.