G: Hey n8n enthusiasts and data wranglers! 👋
In today’s data-driven world, raw information often comes in a deluge – unorganized, sprawling, and sometimes overwhelming. To make sense of it, derive insights, and power our automated workflows, we need powerful tools for data transformation. Enter the unsung hero of many n8n workflows: The Aggregation Node! 🦸♂️
While other nodes grab the spotlight for fetching or sending data, the Aggregation node quietly performs miracles in the background, consolidating, summarizing, and restructuring your data into exactly what you need.
In this deep dive, we’re going to pull back the curtain on the n8n Aggregation Node. We’ll explore its core mechanics and then supercharge your understanding with 10 practical, real-world use cases that you can apply to your own workflows. Get ready to transform your data like never before! ✨
What is the n8n Aggregation Node, Anyway? 🤔
At its heart, the n8n Aggregation Node is designed to group and summarize data items based on specific criteria. Think of it like the “pivot table” of your n8n workflow. It takes multiple individual data items, groups them by a common field (or fields), and then performs one or more operations (like sum, count, average, etc.) on the grouped data.
Why is this crucial?
- Data Consolidation: Reduce a large number of items into a smaller, more manageable summary.
- Reporting & Analytics: Generate instant reports (e.g., total sales per product, average customer satisfaction).
- Workflow Efficiency: Prepare data for subsequent nodes that expect summarized information (e.g., sending a daily sales summary email instead of individual order notifications).
- Insight Generation: Discover patterns and trends hidden within your raw data.
How Does it Work? The Core Mechanics ⚙️
The Aggregation Node primarily uses two key components:
-
Group By Fields: This is where you tell the node how to group your data. You select one or more fields from your incoming items. All items that have the same value(s) for these fields will be put into the same group.
- Example: If you
Group By
“productCategory”, all items with “Electronics” as their category will be in one group, “Books” in another, and so on.
- Example: If you
-
Aggregation Operations: Once items are grouped, you specify what calculations or transformations to perform on each group. n8n offers a powerful set of operations:
Operation Type Description Example Use Case Sum
Calculates the total sum of numeric values in a field for each group. Total revenue per product. Average
Calculates the average of numeric values in a field for each group. Average order value per customer segment. Count
Counts the number of items in each group. Number of tasks completed per project. Count Unique
Counts the number of unique values in a specified field within each group. Number of unique visitors per day. Min
Finds the minimum numeric value in a field for each group. Lowest temperature recorded at a sensor location. Max
Finds the maximum numeric value in a field for each group. Highest score achieved by a user. Array
Gathers all values of a specified field from items within a group into a single array. List of all line items for an order. Concatenate
Joins all string values of a specified field from items within a group into a single string. Combining all comments from a single user.
Let’s illustrate with a simple example: Imagine you have sales data for different products.
Input Data (multiple items):
[
{ "product": "Laptop", "category": "Electronics", "price": 1200, "quantity": 1 },
{ "product": "Mouse", "category": "Electronics", "price": 25, "quantity": 2 },
{ "product": "Keyboard", "category": "Electronics", "price": 75, "quantity": 1 },
{ "product": "Novel", "category": "Books", "price": 15, "quantity": 3 },
{ "product": "Cookbook", "category": "Books", "price": 30, "quantity": 1 }
]
Aggregation Node Configuration:
- Group By:
category
- Aggregation Operations:
- Operation:
Sum
- Field:
price
- New Field Name:
totalCategoryRevenue
- Operation:
Count
- Field:
product
(or any field, as it counts items) - New Field Name:
numberOfProducts
- Operation:
Output Data (two items):
[
{ "category": "Electronics", "totalCategoryRevenue": 1300, "numberOfProducts": 3 },
{ "category": "Books", "totalCategoryRevenue": 45, "numberOfProducts": 2 }
]
See how powerful that is? From 5 detailed items, we got 2 summarized items, providing quick insights!
10 Real-World Use Cases for the n8n Aggregation Node 🚀
Let’s dive into some practical scenarios where the Aggregation Node truly shines!
1. Total Sales by Product Category 📊
- Scenario: You receive daily order data from an e-commerce platform. You need a daily report showing the total sales generated by each product category.
- Solution: Group the orders by
productCategory
and thenSum
theorderTotal
for each group. - n8n Configuration:
- Group By:
productCategory
- Aggregation Operations:
- Type:
Sum
- Field:
orderTotal
- New Field Name:
totalRevenue
- Type:
- Group By:
- Input Example:
[ { "orderId": "001", "productCategory": "Electronics", "orderTotal": 150 }, { "orderId": "002", "productCategory": "Books", "orderTotal": 45 }, { "orderId": "003", "productCategory": "Electronics", "orderTotal": 200 }, { "orderId": "004", "productCategory": "Home Goods", "orderTotal": 75 } ]
- Output Example:
[ { "productCategory": "Electronics", "totalRevenue": 350 }, { "productCategory": "Books", "totalRevenue": 45 }, { "productCategory": "Home Goods", "totalRevenue": 75 } ]
2. Average Customer Satisfaction Score per Region ⭐
- Scenario: You collect customer feedback with satisfaction scores (1-5) and customer regions. You want to identify which regions have the highest/lowest average satisfaction.
- Solution: Group the feedback entries by
customerRegion
andAverage
thesatisfactionScore
. - n8n Configuration:
- Group By:
customerRegion
- Aggregation Operations:
- Type:
Average
- Field:
satisfactionScore
- New Field Name:
avgSatisfaction
- Type:
- Group By:
- Input Example:
[ { "id": 1, "customerRegion": "North", "satisfactionScore": 4 }, { "id": 2, "customerRegion": "South", "satisfactionScore": 5 }, { "id": 3, "customerRegion": "North", "satisfactionScore": 3 }, { "id": 4, "customerRegion": "East", "satisfactionScore": 4 } ]
- Output Example:
[ { "customerRegion": "North", "avgSatisfaction": 3.5 }, { "customerRegion": "South", "avgSatisfaction": 5 }, { "customerRegion": "East", "avgSatisfaction": 4 } ]
3. Number of Unique Visitors to a Web Page Daily 🧑💻
- Scenario: You’re logging every page view on your website. You need to know how many unique users visited a specific page each day.
- Solution: Group the log entries by
pageURL
anddate
, thenCount Unique
thevisitorID
. - n8n Configuration:
- Group By:
pageURL
,date
- Aggregation Operations:
- Type:
Count Unique
- Field:
visitorID
- New Field Name:
uniqueVisitors
- Type:
- Group By:
- Input Example:
[ { "date": "2023-10-26", "pageURL": "/pricing", "visitorID": "userA" }, { "date": "2023-10-26", "pageURL": "/pricing", "visitorID": "userB" }, { "date": "2023-10-26", "pageURL": "/pricing", "visitorID": "userA" }, { "date": "2023-10-26", "pageURL": "/about", "visitorID": "userC" } ]
- Output Example:
[ { "date": "2023-10-26", "pageURL": "/pricing", "uniqueVisitors": 2 }, { "date": "2023-10-26", "pageURL": "/about", "uniqueVisitors": 1 } ]
4. Minimum and Maximum Temperatures from Sensor Readings 🌡️
- Scenario: You have a network of IoT sensors sending temperature readings. You want to find the daily minimum and maximum temperature for each sensor.
- Solution: Group the readings by
sensorID
anddate
, then useMin
andMax
on thetemperature
field. - n8n Configuration:
- Group By:
sensorID
,date
- Aggregation Operations:
- Type:
Min
, Field:temperature
, New Field:minTemperature
- Type:
Max
, Field:temperature
, New Field:maxTemperature
- Type:
- Group By:
- Input Example:
[ { "sensorID": "S001", "date": "2023-10-26", "temperature": 20 }, { "sensorID": "S001", "date": "2023-10-26", "temperature": 25 }, { "sensorID": "S002", "date": "2023-10-26", "temperature": 18 }, { "sensorID": "S001", "date": "2023-10-26", "temperature": 19 } ]
- Output Example:
[ { "sensorID": "S001", "date": "2023-10-26", "minTemperature": 19, "maxTemperature": 25 }, { "sensorID": "S002", "date": "2023-10-26", "minTemperature": 18, "maxTemperature": 18 } ]
5. Tracking Project Task Status Counts 🏗️
- Scenario: You’re managing tasks for various projects. You want to see a summary of how many tasks are in “To Do”, “In Progress”, or “Done” status for each project.
- Solution: Group tasks by
projectID
andstatus
, thenCount
the items within each group. - n8n Configuration:
- Group By:
projectID
,status
- Aggregation Operations:
- Type:
Count
, Field:taskID
(or any field), New Field:taskCount
- Type:
- Group By:
- Input Example:
[ { "projectID": "P001", "taskID": "T001", "status": "To Do" }, { "projectID": "P001", "taskID": "T002", "status": "In Progress" }, { "projectID": "P002", "taskID": "T003", "status": "To Do" }, { "projectID": "P001", "taskID": "T004", "status": "To Do" } ]
- Output Example:
[ { "projectID": "P001", "status": "To Do", "taskCount": 2 }, { "projectID": "P001", "status": "In Progress", "taskCount": 1 }, { "projectID": "P002", "status": "To Do", "taskCount": 1 } ]
6. Consolidating Order Line Items into an Array 🛒
- Scenario: Your e-commerce system sends individual line items for each order (e.g., each product in an order is a separate item). You need to consolidate them so each order is a single item with an array of its products.
- Solution: Group by
orderID
, and then use theArray
operation on fields likeproductName
,quantity
,price
to collect them into nested arrays or a single array of objects if you restructure first. - n8n Configuration:
- Pre-step (Merge/Combine items if line items are separate fields): Use a Set node or Code node to create a single
lineItem
object first. - Group By:
orderID
- Aggregation Operations:
- Type:
Array
, Field:lineItem
(if you created an object like{productName: "...", quantity: ...}
), New Field:lineItems
- Type:
- Pre-step (Merge/Combine items if line items are separate fields): Use a Set node or Code node to create a single
- Input Example (assuming pre-processed into lineItem objects):
[ { "orderID": "ORD123", "lineItem": { "productName": "Laptop", "qty": 1, "price": 1200 } }, { "orderID": "ORD123", "lineItem": { "productName": "Mouse", "qty": 1, "price": 25 } }, { "orderID": "ORD456", "lineItem": { "productName": "Book", "qty": 2, "price": 30 } } ]
- Output Example:
[ { "orderID": "ORD123", "lineItems": [ { "productName": "Laptop", "qty": 1, "price": 1200 }, { "productName": "Mouse", "qty": 1, "price": 25 } ] }, { "orderID": "ORD456", "lineItems": [ { "productName": "Book", "qty": 2, "price": 30 } ] } ]
7. Generating a Combined Summary Description 📝
- Scenario: You’re gathering various notes or comments related to a specific ticket or project. You want to compile all these individual notes into a single, comprehensive description.
- Solution: Group by
ticketID
(orprojectID
), and use theConcatenate
operation on thecommentText
field. You can specify a separator like\n\n
for readability. - n8n Configuration:
- Group By:
ticketID
- Aggregation Operations:
- Type:
Concatenate
- Field:
commentText
- New Field Name:
fullDescription
- Separator:
\n\n
(for new lines)
- Type:
- Group By:
- Input Example:
[ { "ticketID": "TKT789", "commentText": "Customer reported login issue." }, { "ticketID": "TKT789", "commentText": "Checked logs, identified blocked IP." }, { "ticketID": "TKT789", "commentText": "IP unblocked, issue resolved." }, { "ticketID": "TKT101", "commentText": "New feature request received." } ]
- Output Example:
[ { "ticketID": "TKT789", "fullDescription": "Customer reported login issue.\n\nChecked logs, identified blocked IP.\n\nIP unblocked, issue resolved." }, { "ticketID": "TKT101", "fullDescription": "New feature request received." } ]
8. Identifying Most Active Users from System Logs 👤
- Scenario: Your system generates logs for every user action. You want to find out which users were most active (performed the most actions) within a certain period.
- Solution: Group the log entries by
userID
anddate
, thenCount
the actions. You can then sort the results to find the highest counts. - n8n Configuration:
- Group By:
userID
(anddate
if you need daily activity) - Aggregation Operations:
- Type:
Count
, Field:actionType
(or any field), New Field:actionCount
- Type:
- Group By:
- Input Example:
[ { "userID": "alice", "actionType": "login", "date": "2023-10-26" }, { "userID": "bob", "actionType": "view_page", "date": "2023-10-26" }, { "userID": "alice", "actionType": "create_record", "date": "2023-10-26" }, { "userID": "bob", "actionType": "edit_record", "date": "2023-10-26" }, { "userID": "alice", "actionType": "view_page", "date": "2023-10-26" } ]
- Output Example:
[ { "userID": "alice", "actionCount": 3 }, { "userID": "bob", "actionCount": 2 } ]
9. Inventory Management: Total Stock per Warehouse 📦
- Scenario: You have inventory records that specify the quantity of each product in different warehouses. You need a summary of the total stock for each product across all warehouses.
- Solution: Group by
productSKU
, andSum
thequantityInStock
. - n8n Configuration:
- Group By:
productSKU
- Aggregation Operations:
- Type:
Sum
, Field:quantityInStock
, New Field:totalGlobalStock
- Type:
- Group By:
- Input Example:
[ { "productSKU": "PN001", "warehouseID": "W001", "quantityInStock": 100 }, { "productSKU": "PN002", "warehouseID": "W001", "quantityInStock": 50 }, { "productSKU": "PN001", "warehouseID": "W002", "quantityInStock": 75 } ]
- Output Example:
[ { "productSKU": "PN001", "totalGlobalStock": 175 }, { "productSKU": "PN002", "totalGlobalStock": 50 } ]
10. Counting Event Log Errors by Service Endpoint 🚨
- Scenario: Your microservices generate error logs with the service name and endpoint path. You want to monitor which endpoints are generating the most errors.
- Solution: Group by
serviceName
andendpointPath
, thenCount
the error logs. - n8n Configuration:
- Group By:
serviceName
,endpointPath
- Aggregation Operations:
- Type:
Count
, Field:logID
(or any identifier), New Field:errorCount
- Type:
- Group By:
- Input Example:
[ { "logID": "E1", "serviceName": "Auth", "endpointPath": "/login", "errorType": "DB_Error" }, { "logID": "E2", "serviceName": "Products", "endpointPath": "/list", "errorType": "API_Timeout" }, { "logID": "E3", "serviceName": "Auth", "endpointPath": "/login", "errorType": "Auth_Failed" }, { "logID": "E4", "serviceName": "Auth", "endpointPath": "/register", "errorType": "Validation_Error" } ]
- Output Example:
[ { "serviceName": "Auth", "endpointPath": "/login", "errorCount": 2 }, { "serviceName": "Products", "endpointPath": "/list", "errorCount": 1 }, { "serviceName": "Auth", "endpointPath": "/register", "errorCount": 1 } ]
Tips and Best Practices for Using the Aggregation Node ✨
- Understand Your Data: Before configuring, know the structure of your incoming data. What fields are you trying to group by? Which fields contain the values you want to aggregate?
- Data Types Matter: Ensure the fields you’re using for
Sum
,Average
,Min
, andMax
are numeric. If they’re strings, you might need a precedingSet
orCode
node to convert them. - Handle Missing Data: The Aggregation node typically ignores
null
or missing values for numeric aggregations. Be aware of how this impacts your results. If you need to treatnull
as zero, pre-process with aSet
node. - Chaining Aggregations: Sometimes you need multi-level aggregation. For example, sum daily sales by product, then average those daily sums over a month. You can achieve this by chaining two Aggregation nodes, or an Aggregation node followed by a
Code
node for more complex calculations. - Use the “New Field Name” Wisely: Give your aggregated fields descriptive names so it’s clear what they represent in downstream nodes.
- Test Iteratively: Start with a simple configuration, test, and then add more complex operations or
Group By
fields. Use the “Execute Workflow” feature and check the output in the “Result” pane.
Conclusion: Unleash Your Data’s Potential! 🚀
The n8n Aggregation Node is an incredibly versatile and powerful tool for data transformation within your workflows. Whether you’re building reports, consolidating records, or extracting crucial insights, mastering this node will significantly enhance your automation capabilities.
By understanding its core concepts (Group By
and Aggregation Operations
) and exploring these 10 real-world use cases, you’re now equipped to tackle even the most challenging data aggregation tasks.
So go ahead, open your n8n instance, pull out that Aggregation Node, and start transforming your data from raw chaos into actionable intelligence! Happy automating! 🤖💡