Incorrect total: SUMX with ABS...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi All,

I'm struggling to get my head around the following and I hope someone can help.

Here's an example of what's happening:

ProductForecastSalesDiff.ABS Diff.
A1009555
B1501351515
C100110-1010
D5075-2525
E50104040
Total4504252525

<tbody>
</tbody>

I have created the following SUMX formula to calculate the ABS Diff., iterating through each row at a time:

Code:
ABS Diff. = ABS(SUMX(Table,Table[Forecast]-Table[Sales]))

The row values returned are correct (in as much as they're the absolute difference between Forecast and Sales), but the total isn't correct and is a sum of the positive and negative values, i.e. it hasn't summed up the absolute values.

What have I done wrong?

Cheers,

Matty
 
I don't think you can create one measure that will work both with the customers included and without. The total row does not know what was is selected above (though I suppose you might use some sort of ISFILTERED().

What you need to do is iterate through the rows in your table with a SUMX. If your line items were defined by one column, ie just two lines for A and B it would be a simple VALUES ( Table[Product] ), but that won't work for two columns. I believe the "best practice" way to do this would be to use SUMMARIZE and ADDCOLUMNS (Brent Greenwood's blog: DAX Groupers: SUMMARIZE and AGGX(VALUES())), but I am not sure I know how to do that.

You could try instead.

Code:
[Measure]:=
SUMX ( 
    CROSSJOIN ( 
        VALUES ( Table1[Product] ),
        VALUES ( Table1[Month] )
    ),
    CALCULATE ( ABS( Table1[Forecast]-Table1[Sales ] )
)

Hi Gaz,

Thanks for that - it works as desired. :)

Could you help me to understand what's going on here? I know that the VALUES function returns a distinct list of values, but I'm not clear as to what the CROSSJOIN is doing?

I'd already come across Brent Greenwood's blog, so it's good that I was on the right tracks. DAX is still relatively new to me though, so it's taking me time to get my head around it. One thing that I do miss with DAX compared to standard Excel formulas is the 'Evaluate Formula' window in Excel, as you can see the intermediate steps the formula is doing to compute the final result, which helps understanding. Something like this in DAX would be really useful!

Thanks again for your help!

Matty
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
As I say, I don't think my approach is best practice but glad it works.

Basically you need to iterate through the rows in your example table, do the ABS calc for each row, and then sum up the result. This is basically the definition of SUMX. However, you don't want to SUMX on the actual table or it will go row by row down the source table and not at the aggregated level.

We could try using VALUES ( Table[Product] ) in the table part of the SUMX, but then that would just do the calculation for once for Product A and once for Product B, and add the two results. We actually want to go through the table like A1, A2, A3, B1, B2, B3 and then SUM the results. Cross Join takes two tables A, B and 1, 2, 3 and creates the Cartesian product, ie returns all combinations multiplied together. This is the desired A1, A2, A3, B1, B2, B3.
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,685
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top