# Tree Transform

The *Tree Transform* object in Astera Data Stack enables users to transform data in a hierarchical structure. Users can create new fields in the parent node based on the data in the collection/child node. *Tree Transform* supports rule-based routing, filtering, merging, and sorting of data while maintaining its hierarchy.

{% embed url="<https://youtu.be/_yrhfQBWPUY?list=PLmyM3TZtVUyJfz1bkpZ-CxJ33fAwHIbug>" %}

### Use Case

In this case, we are using the [*XML/JSON File Source*](/astera-data-stack-v9/dataflows/sources/xml-json-file-source.md) to extract our source data.&#x20;

![](/files/t1J0WQthn3dlS8CcBaaP)

The source file contains customers’ information in the parent node and their orders and shipping details in the collection/child node.

You can preview this data by right-clicking on the source object’s *Header > Preview Output.*

![](/files/fBI2Nu8x0zEq1g9niGVx)

A *Data Preview* window will open, displaying a preview of the hierarchical source data.

![](/files/BQHzrTFvqt0gFbbnwlXS)

Now, we want to create a new field in the parent node that contains the count of orders that arrived late for each customer and route these records to a destination file, all while maintaining the hierarchical format of the dataset.

This new field in the parent node will depend on two fields: *RequiredDate* and *ShippedDate*, that are already present in the collection/child node.

In other words, we are trying to transform hierarchical data without flattening its structure.

### Using Tree Transform Transformation

1. To get a *Tree Transform* object from the *Toolbox*, go to *Toolbox > Transformations > Tree Transform* and drag and drop the *Tree Transform* object onto the designer.

![](/files/pnUmNXadEoI55AFwlMxw)

The transformation object is empty right now. This is because we have not mapped any fields to it yet.

2. Auto-map source fields onto the transformation by dragging and dropping the root node of the source object *CustomerOrders* onto the root node of the transformation object – *TreeXform*.

![](/files/QAz6ZiTEVwaoUYg9totN)

Now that the mapping is done, let’s configure the *TreeXfrom* object.

### Configuring the Tree Transform Object

1. Right-click on the object’s header and select *Properties* from the context menu. A configuration window for *Tree Transform* will open. The first window is the *Layout Builder* window. This is where we can create or delete fields, change their name or data types, mark any field as a variable, or attach expressions to fields.

![](/files/L9yUQzwuyVfxKKuuv1BD)

2. Create a new field under the parent node. Let’s name it *LateOrders*.

![](/files/sHwZLY92q4NR0bLhJGfI)

3. Click *Next*, and you will be directed to a *Layout Modifications* window.

![](/files/CfbVnWY3rd5KbUyZUCS3)

4. Select the *LateOrders* field. Here you can modify any field by applying expressions to it.

![](/files/aLVPaGaSGJNcyZnoWuzg)

5. Write the expression that counts the number of late-arriving orders.

Click *“…”* next to the expression box to open the *Expression Builder* where you can make use of the extensive library of built-in functions and expressions in Astera.

![](/files/p0No6Wad1t17yPaT0yOD)

This creates the field values for *LateOrders* based on the field values of the *RequiredDate* and *ShippedDate* fields.

6. Click on the *Orders* node and a panel *(Options for Collection)* will appear. These options are only available for collection nodes.

![](/files/pFqpVROY9xfeRPRVSvYi)

*Options for Collection*:

* *Show* – Shows collection node in the *Tree Transform* object.
* *Flatten With Item Count* – Flattens data based on the record count in the collection node against each item in the parent node.
* *Flatten Based on Rules* – Flattens a part of hierarchical data based on predefined rules.
* *Route Based on Rules* – Routes and creates subsets of data based on predefined rules.
* *Merge With Item Count* – Merges data based on the record count in the collection node against each item in the parent node.
* *Hide* – Hides collection node from the *Tree Transform* object.
* *Calculation Formula* – An expression box used for writing rules to route or flatten hierarchical data.
* *Sort Based On Keys* – Sorts hierarchical data based on the field in the collection node. Only available for *Show* and *Flatten With Item Count* options.

This is where we will specify the rule for routing late-arriving orders. Select *Route Based on Rules*. A new section for adding rules will appear in the window.

![](/files/EzXeZbGtx4ZGB2qiHtY8)

7. Add a new rule by clicking on the *Add switch condition* icon ![9\_icon](/files/iZrKlysyZUdlcwPLSBSM). Now, write an expression to route late arriving orders and name this rule “LateArrivals”.

![](/files/R7cWHxgjgNIUvQwZO266)

8. Click *OK*. Now observe that a new collection node *Orders\_LateArrivals* has been added to the *Tree Transform* object.

![](/files/L28vDaBxS2gcw7c4zrEy)

To preview data, right-click on the header of the transformation object and select *Preview Output* from the context menu.

![](/files/EZ49cYzFCMp3ppxFH4sU)

A *Data Preview* window will open. On expanding the records, you will get corresponding order details and appended details of late-arriving orders.

![](/files/d70FDN4uxPZ72MAlBYiR)

To store the output for late arriving orders, you can write it to a destination file or use that data further in the dataflow.

This concludes using the *Tree Transform* transformation object in Astera Data Stack.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.astera.com/astera-data-stack-v9/dataflows/transformations/tree-transform.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
