Forum Discussion
SUMX not totaling correctly (Context Transition not applying?)
I am pulling my hair out on this one. I have transactional data for orders with numerous rows per order. I want a measure that returns orders over $500,000, but I need to group the data first by the order #.
Here is my measure
Row by Row, this works, but the totals are not lining up
Some order #'s are blank, and I have tried filtering out the blanks. Again, it works at the row level, but not at the total. I have also tried GROUPBY. I have tried numerous approaches that result in the correct row level total, but not the grand total. I don't know what I am doing wrong.
I'm not certain, but this could be due to clustering, which is why you should never use SUMMARIZE to create calculated columns, always use ADDCOLUMNS
Try
OVER 500000 = VAR SalesThreshold = 500000 VAR GroupedOrders = ADDCOLUMNS ( SUMMARIZE ( 'Revenue Source', 'Revenue Source'[Order No] ), "TotalSales", CALCULATE ( SUM ( 'Revenue Source'[Master Net USD] ) ) ) VAR FilteredOrders = FILTER ( GroupedOrders, NOT ISBLANK ( [Order No] ) && [TotalSales] > SalesThreshold ) RETURN SUMX ( FilteredOrders, [TotalSales] )For a detailed explanation of why to use ADDCOLUMNS, see https://www.sqlbi.com/articles/all-the-secrets-of-summarize/
20 Replies
- DataNinja777
Super User
Hi jwin2424 ,
The issue with totals not aligning arises because DAX evaluates grand totals differently from row-level calculations. While your measure works correctly at the row level, the grand total does not automatically reapply the same logic, leading to discrepancies. To address this, you can create a summarized table grouped by Order No, filter the grouped data to exclude orders below the threshold and blank Order No, and explicitly calculate the grand total using these filtered results. Introducing a variable for the threshold value improves maintainability, allowing changes to be made in one place if the threshold changes in the future.
Here’s the updated measure with the threshold defined as a variable:
OVER 500000 = VAR SalesThreshold = 500000 VAR GroupedOrders = SUMMARIZE( 'Revenue Source', 'Revenue Source'[Order No], "TotalSales", CALCULATE(SUM('Revenue Source'[Master Net USD])) ) VAR FilteredOrders = FILTER( GroupedOrders, NOT ISBLANK([Order No]) && [TotalSales] > SalesThreshold ) RETURN SUMX(FilteredOrders, [TotalSales])In this measure, the SalesThreshold variable sets the threshold for orders, ensuring you only need to update this value if it changes in the future. The SUMMARIZE function groups data by Order No and calculates total sales for each order. The FILTER function then removes rows where the Order No is blank or the total sales are less than the threshold. Finally, SUMX ensures that the correct total is calculated by summing the filtered sales. This approach explicitly calculates the grand total, ensuring consistency between row-level results and the total. By introducing the SalesThreshold variable, the formula is not only accurate but also easier to maintain and update.
Best regards,
- govind_021
Super User
Hey jwin2424
Please use this measureOVER 500000 =
VAR SalesThreshold = 500000
RETURN
CALCULATE(
SUM('Revenue Source'[Master Net USD]),
FILTER(
'Revenue Source',
NOT ISBLANK('Revenue Source'[Order No]) &&
CALCULATE(SUM('Revenue Source'[Master Net USD])) > SalesThreshold
)
)- jwin2424
Resolver I
This is not calculating orders > 500k, it is calculating rows over 500k.
- jwin2424
Resolver I
I am running into the same issue.
Anytime I use SUMX, it itterates. The [Total Sales] is using the CALCULATE function, so I am not sure why it is not properally summing.- johnt75
Super User
I'm not certain, but this could be due to clustering, which is why you should never use SUMMARIZE to create calculated columns, always use ADDCOLUMNS
Try
OVER 500000 = VAR SalesThreshold = 500000 VAR GroupedOrders = ADDCOLUMNS ( SUMMARIZE ( 'Revenue Source', 'Revenue Source'[Order No] ), "TotalSales", CALCULATE ( SUM ( 'Revenue Source'[Master Net USD] ) ) ) VAR FilteredOrders = FILTER ( GroupedOrders, NOT ISBLANK ( [Order No] ) && [TotalSales] > SalesThreshold ) RETURN SUMX ( FilteredOrders, [TotalSales] )For a detailed explanation of why to use ADDCOLUMNS, see https://www.sqlbi.com/articles/all-the-secrets-of-summarize/