CSV import: calculate average value
This tutorial shows you how to import a CSV file, calculate an average value from a numerical column and send that value via email.
Last updated
This tutorial shows you how to import a CSV file, calculate an average value from a numerical column and send that value via email.
Last updated
Our flow is configured to trigger as soon as the flow is started, imports a CSV file that we uploaded via the file upload interface of the CSV.ImportCSV utility, gets all the rows, calculate an average age of the persons in the CSV and sends the result via email. Our flow looks like this:
The ImportCSV utility from the CSV module is simply configured to read a file that we selected from our file system by clicking on the "Select file" input field:
The content of our CSV sample file looks like this:
The configuration for the GetRows component uses the fileId
variable from the ImportCSV component for the "File ID" input field. We also disabled the "Filter rows" parameter since we're interested in getting all the rows from the CSV file, not just an extracted subset. Also note that the "Row format" is set to "Object" so that the component gives us a formatted JSON list of all the rows in the form [{col1: val1, col2: val2}, ...]
.
Next, we configure the final SendEmail component to send an email with the average age. The trick here is to calculate the average age by modifying the rows
variable returned from GetRows component.
By clicking on the rows
variable, we can configure our modifiers (data transformation functions) that calculate the average by first calculating the sum of all ages and dividing it by the number of items in the rows
list:
Note that the modifiers can be stacked to produce more complex expressions. In the example above, we first "Pick" the age
column only, apply "Sum" to the values and "Div" by the length of the rows
list. Note that the length of the rows
is defined by applying the nested "Length" modifier which we can do by clicking on the rows
variable in the "Div" input field:
A good practice is to test your modifiers using the "Test" functionality of the Modifiers panel:
This is it! Now when we actually run our flow, we'll see a new email in our inbox with the average age of all the persons in our CSV file:
As a bonus for advanced users, we can also use a "low-code" alternative to our modifiers above by defining a JavaScript function modifier that will give us an average of values in our age
column:
As you can see, the "JavaScript Function" modifier takes an arbitrary JavaScript code in which you can refer to the original variable with the $variable
placeholder.