Dax Function: SUMX
Category: Aggregation functions
The SUMX function in Power BI is a versatile DAX (Data Analysis Expressions) function that calculates the sum of an expression evaluated row by row over a table. Unlike the simpler SUM function, which works on a single column, SUMX allows for dynamic calculations by evaluating a formula or expression for each row and then summing up the results.
Purpose:
- Perform row-by-row calculations and then aggregate the results into a single value.
- Use it to apply dynamic operations, such as multiplying or filtering, before summing.
Calculations:
- SUMX evaluates an expression for each row in a table and sums the results.
- It is ideal for scenarios where the sum involves calculated fields or expressions.
Practical Use Cases:
- Calculating total sales by multiplying quantity and price per unit for each row in a sales table.
- Summing conditional values, such as revenue for specific categories or regions.
- Applying advanced row-level logic in aggregation.
SUMX( , )
Parameter Type Description Table Table The table containing the rows to evaluate. Can be a physical table or a table returned by another DAX function. Expression Scalar The formula or calculation to perform for each row of the table. Must return a numeric value.
How Does SUMX Dax Function Works?
- The SUMX function iterates through each row in the specified table.
- For every row, it evaluates the provided expression.
- The results of the expression are aggregated using summation.
For example, consider a table with columns Quantity and PricePerUnit. The expression SUMX(Sales, Sales[Quantity] * Sales[PricePerUnit])
will calculate the total revenue by multiplying quantity and price for each row and summing up the results.
Formula Example:
TotalRevenue = SUMX(Sales, Sales[Quantity] * Sales[PricePerUnit])
This calculates the total revenue by summing up the revenue for each row in the Sales table.
What Does It Return?
The function returns a scalar numeric value, which is the total sum of the calculated results from the expression evaluated for each row in the table.
When Should We Use It?
- When your summation involves row-level calculations or expressions that cannot be handled by the basic SUM function.
- For dynamic or conditional calculations, such as calculating weighted averages or totals based on complex logic.
- When combining multiple columns or applying filters in aggregation.
Examples
Basic Usage
To calculate total revenue using the Quantity and PricePerUnit columns:
TotalRevenue = SUMX(Sales, Sales[Quantity] * Sales[PricePerUnit])
This multiplies quantity and price for each row and sums the results.
Column Usage:
If you want to calculate total profit (Revenue – Cost) for each row:
TotalProfit = SUMX(Sales, Sales[Revenue] - Sales[Cost])
Advanced Usage
Combining SUMX with filtering using the CALCULATETABLE function:
FilteredTotal = SUMX(
CALCULATETABLE(Sales, Sales[Region] = "North"),
Sales[Quantity] * Sales[PricePerUnit]
)
This calculates total revenue for rows where the Region column equals “North.”
Tips and Tricks
- Performance Considerations: SUMX can be slower on large datasets due to row-by-row evaluations. Optimize the table or use filters to reduce the number of rows processed.
- Expression Validation: Ensure the expression produces a numeric value for each row; otherwise, the function will throw an error.
- Use with Related Tables: SUMX works seamlessly with relationships in Power BI. You can reference columns from related tables in the expression.
Performance Impact of SUMX DAX Function:
- Table Size: Processing large tables row by row can be computationally expensive. Optimize table size using filters.
- Avoid Nested Calculations: Reduce the complexity of the expression to improve performance.
- Use Columns Wisely: Use calculated columns sparingly to avoid redundant computations.
Related Functions You Might Need
- SUM: For simple aggregation of a single numeric column.
- AVERAGEX: For calculating the average of an expression across rows.
- CALCULATE: For applying filters in conjunction with SUMX.
- FILTER: To define row-specific conditions before using SUMX.
Want to Learn More?
For more information, check out the official Microsoft documentation for SUM. You can also experiment with this function in your Power BI reports to explore its capabilities.
Maximize the potential of Power BI and enhance your data insights with our expert consulting services. Whether you’re looking for assistance with advanced DAX functions, help designing interactive dashboards, or support in optimizing your data models for better performance, our skilled Power BI consultants are ready to provide tailored solutions for your business. Visit our Power BI consultancy page to learn more about how we can empower your organization to make more informed, data-driven decisions.
1. What is the difference between SUM and SUMX in Power BI?SUM adds up values in a single column, while SUMX evaluates an expression for each row and sums the results.
2. Can SUMX be used with filters?Yes, you can use SUMX in conjunction with filters like CALCULATE or FILTER for conditional calculations.
3. Is SUMX slower than SUM?SUMX is typically slower than SUM because it evaluates expressions row by row, which can be computationally intensive for large datasets.
4. What data type does SUMX return?SUMX returns a numeric value, such as an integer or decimal, based on the calculated results.
Established in 2020, Lets Viz Technologies provides a full range of high-quality data analysis and data visualization services. We are also an authorized Zoho Partner.
Sitelinks