Creating rounding transformation script
This guide explains how to create a transformation script that will round numbers to a given number of decimal places.
Introduction
Values in a data extract can be rounded to a given number of decimal places using a transformation script.
One use case for a rounding transformation script is during the creation of a geo map . To increase performance of Adverity when displaying data, it is recommended the latitude and longitude values are set to no more than two decimal places. This guide uses this scenario as an example.
The rounding transformation script contains two instructions:
-
convertnumbers - In most cases, data extracts contain the values as strings. This instruction converts the strings to numbers so they can be rounded.
-
convertx - This instruction is where you provide the Python script to round the numbers.
Prerequisites
Before you read this reference, perform all of the following actions:
-
Ensure the data source from which you are collecting data contain the values you wish to round. This guide uses the following location fields as an example:
-
Latitude
-
Longitude
-
-
Create a datastream to collect data from this data source. For more information, see Creating a datastream.
-
Find the name of the columns in the data extract that contain the values to round.
Procedure
To create a transformation script that round numbers to a given number of decimal places, follow these steps:
-
Go to the Transformations page.
-
In the top right corner, click + Create transformation.
-
Click Custom Script.
-
To assign the transformation to individual datastreams of your choice, follow these steps:
-
In the Assign to section, select Individual datastreams.
-
In the drop-down menu, select the datastreams to assign the transformation to them.
-
-
To assign the transformation to all existing and new datastreams that belong to Datastream Types of your choice, follow these steps:
-
In the Assign to section, select Datastreams Types.
-
In the drop-down menu, select the Datastream Types to assign the transformation to all datastreams that belong to these types.
-
(Optional) In the Do not assign to these individual datastreams drop-down menu, select the individual datastreams that belong to the Datastream Types that you have selected, but you do not want to assign the transformation to them.
-
-
Click Next.
-
In Select Instruction, enter and select convertnumbers.
-
In Fields, click Add.
-
In Fields, ensure String is selected and enter the name of the column in the data extract that contains the values to convert. For example, enter
latitude
as the name of the column that contains the values to convert. -
(Optional) Repeat steps 8 and 9, to convert any additional values into numbers. For example, enter
longitude
as the name of an additional column that contains the values to convert. -
Below the convertnumbers instruction, click Add Instruction.
-
In Select Instruction, enter and select convertx.
-
In Field, select String from the drop-down field and enter the name of column the contains the values to round. This guide uses
latitude
as an example. -
In Python Expression, enter the following code to round the values in the selected column to a given number of decimal places. The example below will round values in the
latitude
to two decimal places:-
round({latitude},2)
-
-
(Optional) Repeat steps 11 to 14 for any other values you wish to round. For example, to round the values in the
longitude
column, repeat steps 11 to 15 and enter the following python code:-
round({longitude},2)
-
-
Select the Fail on Error checkbox.
-
Click Next.
-
(Optional) In Transformation name, rename the transformation.
-
Click Create +.
As a result, you have created the rounding transformation script. Apply the script to the relevant datastream to round the chosen values to a given number of decimal places.
Applying the transformation to a datastream
To apply the transformation to a datastream, follow these steps:
-
Go to the Datastreams page.
-
Click the datastream to which you want to apply the transformation.
-
In the Transformations section, click + Add transformation.
-
Click Assign Existing transformation.
-
To the right of the transformation you want to apply to the datastream, click + Assign
-
In the top right corner of the page, click Close to close the Assign transformations window.
As a result, the transformation is added to the datastream. Click Fetch now to collect data in the datastream with the applied transformation script.
If you wish to round the latitude and longitude values, ensure the rounding script is run before the geolocation-creation script.