TiKiLa Math Operations
An overview of the math operations you can perform with TiKiLa
TiKiLa comes with a broad range of calculation functions that you can use to perform math operations. When working with these functions, make sure you are accessing data in a number format (integer or float). If your number is parsed as a string, you can use the cast function to transform it.
TiKiLa Math Functions
add(number_1, number_2)
This function lets you add two numbers.
Example:
add(15,20) —> 35
subtract(number_1, number_2)
This function subtracts the second number from the first.
Example:
subtract(520,20) —> 500
multiply(number_1, number_2)
This function multiplies two numbers with each other.
Example:
multiply(2,3) —> 6
divide(number_1, number_2)
This function divides number_1 by number_2. It will fail if number_2 is 0. See the alternative divide functions below to handle this case.
Example:
divide(4,2) —> 2 | divide(5,0) —> ERROR
divideOrNull(number_1, number_2)
This function divides number_1 by number_2. If number_2 is 0, it will not fail but return an empty (null) value.
Example:
divide(4,2) —> 2 | divide(5,0) —> null
divideOrZero(number_1, number_2)
This function divides number_1 by number_2. If number_2 is 0, it will not fail but return 0.
Example:
divide(4,2) —> 2 | divide(5,0) —> 0
modulo(number_1, number_2)
This functions returns the remainder of the division of number_1 by number_2.
Example:
divide(5,3) —> 2
round(number_with_decimals, decimals)
This function allows you to round a number to a certain number of decimal places. The first argument is the number you want to round, and the second argument is the number of decimal places you want to round to.
Example:
round(3.14159, 2) —> 3.14 | round(2.12, 1) —> 2.2
Example
Example JSON - Copy this to your account to try it out yourself
{"name":"Patch JSON TiKiLa Math Operations","description":"An example of TiKiLa Math functions","nodes":{"patch_json_v3_1":{"type":"patch_json_v3","logging_enabled":false,"error_rules":[],"x":602,"y":338,"inputs":{"patches":{"export_enabled":true,"data":[{"op":"add","path":"/metrics/cost_per_click","value":"{{\n round(\n divide(\n divide(\n inputData(\"/spend\"),\n 100\n ),\n inputData(\"/clicks\")\n ),\n 2\n )\n}}"}]},"value":{"export_enabled":true}}},"start_1":{"type":"start","logging_enabled":false,"error_rules":[],"x":150,"y":50},"stop_1":{"type":"stop","logging_enabled":false,"error_rules":[],"x":984,"y":556},"input_any_1":{"type":"input_any","logging_enabled":false,"error_rules":[],"x":147,"y":154,"inputs":{"any":{"title":"demo_fb_insights","description":"The any to input","export_enabled":true,"data":{"clicks":200,"spend":5140,"conversions":7}}}}},"edges":[{"id":"input_any_1.any:patch_json_v3_1.data","points":[]},{"id":"start_1.start:patch_json_v3_1.patch","points":[]},{"id":"patch_json_v3_1.patched:stop_1.stop","points":[]}],"groups":{},"widgets":{},"variables":{},"config":{"capture_inputs_enabled":false,"caching_enabled":false},"tags":[]}
Take the following situation. We have pulled insights on a Facebook Campaign with the following stats:
{
"clicks": 200,
"spend": 5140,
"conversions": 7
}
The spend is in $-cents - so it’s $51.40. Now we want to calculate a metric: cost_per_click. To do this, we can simply add a Patch JSON TiKiLa node and use some of the math functions from above: To calculate cost_per_click:
{{
round(
divide(
divide(
inputData("/spend"),
100
),
inputData("/clicks")
),
2
)
}}
We first divide the spend by 100 to convert it from $-cents to $. Then we divide it again by the clicks to get our metric. Lastly we round it to two decimal places.
The result looks like this:
{
"clicks": 200,
"spend": 5140,
"conversions": 7
"metrics": {
"cost_per_click": 0.26
}
}
Video Example
Last updated on June 21, 2023