Build Custom Formula Functions

Version:
2019.3
Last modified: October 10, 2019

Create custom functions that can be accessed through Alteryx Designer anywhere that an Expression Editor exists, such as the Formula tool, Multi-Row Formula tool, Filter tool, or others. Custom functions are made by:

  • Writing an XML file, which wraps parameters into other function calls.
  • Writing a C-style DLL.

Regardless of the method of creation, the functions are made available to Designer using an XML formula function file, which must contain the following attributes:

  • <name>: A unique name that appears in the functions list.
  • <NumParams>: The number of parameters the function accepts.
  • <Category>: The function category that displays the function. This can be a new category or an existing one.
  • <InsertText>: The boilerplate text that is inserted to the expression editor when the function is used.
  • <Description>: A description that displays when the function is hovered over.

Build Using XML

When creating a function using only XML, the file's structure follows that of the example below.

XML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FormulaAddIn>
  3.   <Function>
  4.     <Name>Sample_XML</Name>
  5.     <NumParams variable="false">3</NumParams>
  6.     <Category>Sample</Category>
  7.     <InsertText>Sample_XML(Num1, Num2, Num3)</InsertText>
  8.     <Description>Obtains the average of three numbers.</Description>
  9.     <Formula>((P1+P2+P3)/3)</Formula>
  10.   </Function>
  11. </FormulaAddIn>

The NumParams element has an attribute variable. The attribute is set to false because the number of parameters accepted is fixed. When using only XML to create a function, the number of parameters accepted is always fixed.

The Formula element contains the formula that actually performs the work. The contents are not visible to the user. The parameters are always referenced as P1, P2, P3, and so on, regardless of the text presented in InsertText.

To add a second function to the same XML file, create another Function element within the FormulaAddIn element.

Build using a DLL and XML

To use a DLL, write your desired function, then export it from the DLL. Assuming the exported function is defined as:

XML
  1. struct FormulaAddInData{
  2. 	int nVarType;		// 1 for double, 2 for wchar_t
  3. 	int isNull;		// 1 if NULL, 0 if valid
  4. 	double dVal;		// valid if nVarType==1 && isNull==0
  5. 	const wchar_t * pVal;	// valid if nVarType==2 && isNull==0
  6. };

The exported function has the signature:

typedef long ( _stdcall * FormulaAddInPlugin)(int nNumArgs, FormulaAddInData *pArgs, FormulaAddInData *pReturnValue);

nNumArgs contains the actual number of arguments in the call.

pArgs points to an array of the actual arguments.

The function should return a 1 to signal success and a 0 to signal failure.

pReturnValue returns the computed result if the call is successful or a string error message if the call is unsuccessful. Any returned strings must be allocated with GlobalAlloc, and Alteryx Designer manages freeing the string.

With the DLL successfully set up, you need to reference the DLL from the XML file.

XML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FormulaAddIn>
  3.   <Function>
  4.     <Name>AddInData</Name>
  5.     <NumParams variable="true">3</NumParams>
  6.     <Category>Sample</Category>
  7.     <InsertText>AddInData(VarType, isNull, double)</InsertText>
  8.     <Description>Verifies data type before adding data.</Description>
  9.     <Dll>
  10.     	<Name>AddInData</Name>
  11.     	<EntryPoint>FormulaAddInData</EntryPoint>
  12.     </Dll>
  13.   </Function>
  14. </FormulaAddIn>

The NumParams element has an attribute variable. The attribute is set to true because the number of parameters accepted is variable. When using a variable number of parameters, the value should be equal to the minimum number required.

The Dll element contains two child elements that provide the needed information about the DLL.

  • <Name>: The name of the DLL file.
  • <EntryPoint>: The name of exported function being used.

Access functions in Designer

To make the functions available in Alteryx Designer, go to the RuntimeData folder in your Alteryx Designer installation. Check for a folder titled \FormulaAddIn. If the folder does not exist, create it.

Sample XML in FormulaAddIn

The folder contains Sample.xml, a file that is correctly configured. Add a category, save the file, and restart Alteryx Designer to see the sample function.

C:\Program Files\Alteryx\bin\RuntimeData\FormulaAddIn

Save your XML file to the \FormulaAddIn directory. If using DLL, save the related DLL file in the same place. Standard naming convention is to name the DLL and XML file to match.

Was This Helpful?

Need something else? Visit the Alteryx Community or contact support.