# Using Output Variables in Astera

The *Variables* object in Astera allows users to create variables that can be used in multiple objects within a dataflow or workflow. The user can assign a fixed value to a variable or allocate it dynamically, depending on the requirement. An output variable is used when you need to pass a certain value from an inner flow (dataflow) to an outer flow (workflow).

### Use Case

In this case, we have a *Customers* dataset with *LateArrivals* and *TotalOrders* fields that contain the count of orders which arrived late and the total number of orders for each customer, respectively. We want to find out the following:

1\. The total number of orders, by summing all the entries in the *TotalOrders* column, for each customer.

2\. The total number of orders which were shipped late, by summing all the entries in the *LateArrivals* column.

3\. The percentage of orders which were shipped late, by using the following expression:

PercentageLate = (LateArrivals/TotalOrders) \* 100.

Finally, we want to notify the administration by sending them an automated e-mail if the *PercentageLate* is greater than a fixed *Threshold* value.

### Using the Variables object in a Dataflow

To achieve our desired functionality, we have created a dataflow with the following objects:

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F11SfRQx6QtFjA6XGEkGq%2F01-dataflow.PNG?alt=media\&token=e8cbaf86-86f6-47be-8b37-45881aa271e4)

1\. [*Delimited File Source*](https://documentation.astera.com/astera-data-stack-v10/dataflows/sources/delimited-file-source)*:* To read the *Customers* dataset from the CSV file.

2\. [*Aggregate Transformation*](https://documentation.astera.com/astera-data-stack-v10/dataflows/transformations/aggregate-transformation)*:* To sum the values in the *LateArrivals* and *TotalOrders* columns.

3\. [*Expression Transformation*](https://documentation.astera.com/astera-data-stack-v10/dataflows/transformations/expression-transformation)*:* To calculate *PercentageLate.*

4\. *Variables:* To set the output variables, *PercentLate* and *Threshold*.

#### Steps to Assign Values to Output Variables

1\. Drag-and-drop the *Variables* object from the Resources section of the Toolbox onto the designer.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F9OrKKaKIvL5ckNNtkRrR%2F03-toolbox.png?alt=media\&token=c0808d3c-8def-4e1a-88c4-7254f6ae89ef)

2\. Right-click on the *Variables* object and select *Properties.* Alternatively, you can also double-click on the object’s header to open *Properties.*

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FeSmFLVCjT65ncd6p1DuA%2F04-properties-option.png?alt=media\&token=5e276208-f790-4c98-b4b6-8c9e41a5ab81)

3\. A *Variables Properties* window will open, where you can specify the *Name, Data Type, Variable Type, Default Value, and Parameter Path* of the variable.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F8qXVer0D03XnQ5GEZ2Dy%2F05-properties-window.PNG?alt=media\&token=1e97cd43-cd91-4d53-abe0-80f56ad21106)

4\. Specify appropriate values for *Name, Data Type, Variable Type* and *Default Value* (**optional**) for the variables you want to create. In this case, we want to create output variables, *PercentLate* and *Threshold*.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FjmzynrboFlLQpouuqXXS%2F06-variables.png?alt=media\&token=5df9b5ad-f120-4d05-8ca7-de0f743f8155)

5\. Go to the drop-down menu in the *Parameter Path* column and expand the *Expression* section. Select *PercentageLate*.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FluatfbVLhamNj1ljya47%2F07-parameter-path.PNG?alt=media\&token=c377d267-0390-443e-8698-18a475e8e361)

*PercentageLate* will only appear in this drop-down menu if it was marked as a *Variable* in the properties of the *Expression* object, as shown below.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2Fu1otg1fSy6DHe1B0OmSo%2F02-expression-object.PNG?alt=media\&token=07d26254-4597-4aeb-9ce1-4a08e60b86fb)

6\. To set the value of the variable, *Threshold*, we will create a new variable, *SetThreshold* and use the $SetVariable(String Variable, Any Value) function in the *Layout Builder* of the *Expression* object. Make sure to specify the parameters of the function with the correct syntax.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FzQJai6pYpdLqUAHlkvcW%2Fset-variable.png?alt=media\&token=fa2e447f-4626-467a-9d0e-98686e8a22c8)

Here, we have set the value of *Threshold* to 9.99. Click *OK* to close this window.

**Note:** The Set Variable functionality works in two cases only:

1\. When there is a set-level transformation (purple box) in the flow. Here, we have used an *Aggregate* transformation, which is a set transformation, in the dataflow. If you do not have a set-level transformation in your flow, you can add a *Passthru* transformation and pass the data from the *Expression* object to it.

2\. Alternatively, in case you do not want to use a set-level transformation, you can uncheck all the checkboxes in the *Output* column in the *Layout Builder* of the *Expression* object for this functionality to work.

We have successfully configured the *Variables* object in the dataflow.

### Using the Variables in a Workflow

We will now complete our desired use case by using [workflow tasks](https://documentation.astera.com/astera-data-stack-v10/workflows). We can run the dataflow in a workflow using the *Run Dataflow* object. In our case, we have made the following workflow (shown below) to notify the admin if the *PercentLate* exceeds the *Threshold* value. Notice that the *PercentLate* and *Threshold* variables appear in the *Run Dataflow* object because they are defined as output variables.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FLYydoWbXA2M1OVUY1BuJ%2F10-workflow-16412791678102.PNG?alt=media\&token=206e9ee7-2c19-4ca9-852a-0faafbc52c61)

The output of the *Decision* object depends on the following condition:

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FR3e3DHVZgQ9TJlJz4zb6%2F11-decision-16412792776063.png?alt=media\&token=50f34921-c800-44f8-bd2d-aa6d721be88b)

If the *PercentLate* is greater than the defined *Threshold*, our *Decision* object will return True, and the *SendMail* task will be executed. Here is a job trace of the workflow:

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2Fl96teBPEDFQHkUTBV9zt%2F12-job-progress.png?alt=media\&token=f349a3ac-5b3d-4d8e-87a3-6f34326a152e)

The *Decision* object has returned False as our *PercentLate* variable, 4.4%, is less than our specified *Threshold* of 9.99%.

This is how you can use the *Variables* object to create and use output variables in Astera.

<br>
