Data Aggregation | Dagster Glossary

Back to Glossary Index

Data Aggregation

Combine data from multiple sources into a single dataset.

Aggregation definition:

Data aggregation is one of the most common tasks in data analytics. Data aggregation involves combining or summarizing multiple data points or observations into a single entity or a smaller set of entities. It enables us to transform raw data into more structured, manageable, and useful formats.

In the context of modern data orchestration, data aggregation is often used to summarize large datasets and make them more accessible for downstream analysis, reporting, or visualization. This process involves grouping data by one or more variables and applying aggregate functions, such as mean, sum, count, or min/max, to calculate statistics or metrics for each group.

Data aggregation is commonly used in ETL (Extract, Transform, Load) pipelines to transform data from multiple sources into a single, consolidated dataset. For instance, in a marketing analytics pipeline, data from multiple sources, such as social media, email campaigns, and website traffic, may be aggregated to generate a comprehensive view of marketing performance across channels.

Data aggregation is also a fundamental technique in data warehousing, where large volumes of data are processed and transformed to create a centralized repository of structured data. Data aggregation helps to reduce the size of these datasets by summarizing them into smaller, more manageable datasets that can be queried and analyzed more efficiently.

Overall, data aggregation is an important technique for modern data orchestration because it enables us to process, transform, and manage large volumes of data more effectively, leading to better insights and more informed decision-making.

Data aggregation best practices

When running an aggregation, there are several best practices to keep in mind.

Use a well-defined schema: Define a schema for the input data and output data. You can use Python libraries like Pandas, PySpark, or Dask to define schemas for structured data. The schema ensures that the data is correctly merged and avoids errors due to inconsistent data types or field names.

Use appropriate aggregation functions: Use appropriate functions to aggregate the data based on the business requirements. Python has built-in functions like sum(), min(), max(), and mean() for basic aggregation, and libraries like Pandas and PySpark have more advanced functions for complex aggregations.

Optimize data storage and processing: To optimize storage and processing, consider using libraries like Pandas, PySpark, or Dask, which have built-in optimization features like partitioning, indexing, and caching.

Monitor performance: Use Python libraries like PySpark or Dask to monitor the performance of the aggregation process. Metrics like processing time, data volume, and memory usage can be tracked to identify potential bottlenecks and optimize performance.

Handle errors and exceptions: Python provides exception handling to handle errors and exceptions, such as missing data, invalid data types, or failed data sources. Ensure that the aggregation process can handle these cases gracefully and provide appropriate error handling and logging.

Test and validate: Use Python libraries like unittest or Pytest to write automated tests to validate the output data against expected results or business rules. Ensure that the data pipeline is tested and validated to ensure that it is producing the expected results.

Aggregation vs. Pre-aggregation

Given our definition for "Aggregation", you might be curious what data engineers mean by "pre-aggregation". Pre-aggregation are steps taken specifically to improve query performance and reduce computational overhead when querying the data later on (whereas "aggregation" are just steps to get to a final analysis, and not specifically optimized for query performance or storage).

Pre-aggregation involves creating summary tables or views that contain pre-computed aggregated values at different levels of granularity, such as daily, weekly, or monthly summaries. These pre-aggregated tables can be used to accelerate analytical queries by providing faster access to summarized data, as opposed to querying the raw, detailed data.

When dealing with large volumes of data, complex queries or analytical operations can be time-consuming and resource-intensive. By pre-aggregating the data, these operations can be performed on smaller, more manageable subsets of the data, resulting in faster response times and improved overall system performance.

Pre-aggregation involves selecting relevant attributes and applying aggregation functions such as sum, count, average, minimum, or maximum to generate aggregated values. This process creates summary tables that contain aggregated data at various levels of granularity, such as daily, weekly, monthly, or even at a higher level of abstraction.

There are several benefits to pre-aggregation (click to expand):

Improved query performance

Aggregating data in advance reduces the amount of data that needs to be processed when executing queries. By querying pre-aggregated tables instead of raw data, queries can be executed faster, enabling quicker insights and analysis.

Reduced resource consumption

Pre-aggregation reduces the computational resources required to process data-intensive operations. Aggregating data upfront decreases the complexity of queries, allowing organizations to optimize their hardware and software infrastructure.

Simplified data modeling

Pre-aggregated tables simplify the data model by providing summarized views of the data. This can make it easier for analysts and data scientists to explore and analyze the data without needing to perform complex aggregations on the fly.

But pre-aggregation can be overused. Over-aggregation can result in the loss of granularity, so you need to carefully evaluate the trade-offs between query performance and the level of detail necessary for specific analytical use cases.

Example of data aggregation using Python

Please note that you need to have the necessary Python libraries installed in your Python environment to run this code.

import pandas as pd

# create sample data
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
        'Department': ['Marketing', 'Engineering', 'Marketing', 'Sales', 'Engineering'],
        'Salary': [50000, 70000, 60000, 80000, 75000],
        'Bonus': [1000, 2000, 1500, 2500, 1800]}

df = pd.DataFrame(data)

# group data by department and calculate mean salary and total bonus
agg_data = df.groupby('Department').agg({'Salary': 'mean', 'Bonus': 'sum'})

print(agg_data)

In this example, we start by creating a sample dataset with columns for Name, Department, Salary, and Bonus. We then create a Pandas DataFrame from this data.

Next, we group the data by department using the groupby() method, and specify that we want to calculate the mean salary and sum of bonuses for each department using the agg() method. Finally, we print the resulting aggregated data.

The result of this aggregation is printed out to the screen:

                 Salary  Bonus
Department
Engineering     72500.0   3800
Marketing       55000.0   2500
Sales           80000.0   2500

Example of data pre-aggregation using Python

Again, the process used in pre-aggregation is similar to that used in aggregation, but the end goal is different, optimizing for query performance and storage optimization. But here is a simple example of how we might pre-aggregate data, using Pandas. Please note that you need to install Pandas in your Python environment to run this code.

import pandas as pd

# Raw data
data = {
    'date': ['2023-06-01', '2023-06-01', '2023-06-01', '2023-06-01', '2023-06-02', '2023-06-02', '2023-06-03', '2023-06-03', '2023-06-03', '2023-06-03'],
    'product': ['A','A', 'B', 'B', 'A', 'B', 'A', 'A', 'C', 'C'],
    'sales': [100,300,200,50,150,250,180,120,60,60]
}

# Create a DataFrame from the raw data
df = pd.DataFrame(data)

# Perform pre-aggregation
aggregated_df = df.groupby(['date', 'product']).sum().reset_index()

# Display the pre-aggregated DataFrame
print(f"Raw table:\n{df}")
print(f"Pre-aggregated table:\n{aggregated_df}")

In this example, we have raw sales data for different products on different dates. The pre-aggregation is performed by grouping the data by the 'date' and 'product' columns using the groupby function. Then, the sum function is applied to calculate the total sales for each combination of date and product. The resulting pre-aggregated DataFrame contains summarized data. Note the loss of granularity in the output:

Raw table:
         date product  sales
0  2023-06-01       A    100
1  2023-06-01       A    300
2  2023-06-01       B    200
3  2023-06-01       B     50
4  2023-06-02       A    150
5  2023-06-02       B    250
6  2023-06-03       A    180
7  2023-06-03       A    120
8  2023-06-03       C     60
9  2023-06-03       C     60
Pre-aggregated table:
         date product  sales
0  2023-06-01       A    400
1  2023-06-01       B    250
2  2023-06-02       A    150
3  2023-06-02       B    250
4  2023-06-03       A    300
5  2023-06-03       C    120

In this case, the pre-aggregated DataFrame provides a summary of the sales data by date and product, making it easier to perform queries and analysis on the aggregated data instead of the raw, detailed data. This simple example illustrates how, in many cases, you can optimize queries, reduce storage requirements, and also avoid downstream re-interpretation of how to calculate things like roll-ups.


Other data engineering terms related to
Data Aggregation and Summarization: