Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
yamacha
Helper I
Helper I

Matrix calculation with multi-layered SUMX

Hi there

I have been struggling with the  Matrix Calculation for years, and SUMX + VALUES seems to be most handy way that can cover most situations. However as you can  below it won't help when there are more than 2 layers of context (i.e. LP_ABBR (customer name), MonthDate and ProductLine5 here)... The displayed outcome are obviously wrong... Need your help on it. Thanks!

yamacha_2-1724402472001.png

yamacha_3-1724402594575.png

 

 

 

 

9 REPLIES 9
Anonymous
Not applicable

Hi All,

Firstly  ahadkarimi thank you for your solution!
And @yamacha to my understanding,you want to get the error for each product line for that month and then add that up, right? 
The matrix can be aggregated for you, you don't need to write your own aggregation function, maybe my data is too simple to reproduce your problem, if you can can you upload some sample data, maybe it will help to solve your problem.

 

ERROR_LP = ABS(SUM('basic data'[Actual Qty])-SUM('basic data'[FCST Qty]))

 

vxingshenmsft_0-1724637472100.png

I would be extremely proud if I could solve your problem and look forward to hearing from you!

Hope it helps!

 

Best regards,
Community Support Team_ Tom Shen

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

 

Hi Xingshen

Thanks for the comments, but unfortunately it's not that simple. There is a further layer below the product line that is the UPN (SKU), and I need to calculate the error on these 2 different levels (that's why you see I have to columns for Error). 

Anonymous
Not applicable

Hi @yamacha ,
I apologise for not providing you with a suitable solution as I didn't understand you correctly.
If sumx and value may not be able to calculate the value of the difference between the strata correctly, you can use the alexecpt function to add a qualification to determine the stratum you need to calculate

-- ERROR_LP_PL5 = 
CALCULATE(
    MAXX(
        VALUES('Table'[ProductLine5]),
        ABS(SUM('Table'[Actual Qty]) - SUM('Table'[FCST Qty]))
    ),
    ALLEXCEPT('Table', 'Table'[LP_ABBR], 'Table'[MonthDate], 'Table'[ProductLine5])
)
-- ERROR_LP_UPN = 
CALCULATE(
    MAXX(
        VALUES('Table'[UPN]),
        ABS(SUM('Table'[Actual Qty]) - SUM('Table'[FCST Qty]))
    ),
    ALLEXCEPT('Table', 'Table'[LP_ABBR], 'Table'[MonthDate], 'Table'[ProductLine5],'Table'[UPN])
)

vxingshenmsft_0-1724979695102.png

If in doubt, check out the pbix file I uploaded and I'd be honoured if I could help you out!

Best regards,
Community Support Team_ Tom Shen

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

 

Hi Xingshen

Thanks for the warm follow-up. I tried with your codes, and found the issue below

As you were using the naming 'Table' to represent multiple tables, I'm kinda confused on the syntax below. I assume the 3 Tables with column quoted refer to the master tables, then what is the first 'Table'?  I do have 2 tables for actual and forecasted demand respectively. Thanks.

 ALLEXCEPT('Table', 'Table'[LP_ABBR], 'Table'[MonthDate], 'Table'[ProductLine5])

 

Anonymous
Not applicable

Hi @yamacha ,

Has your problem been solved after all this time, or has a new problem arisen, if there are any other questions on this issue, feel free to contact me and I'll get back to you as soon as I receive the message.

Hope it helps!

Best regards,
Community Support Team_ Tom Shen

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Hi Xingshen

Sorry for the late response, busy indeed these days.

I kinda locate the root cause (not the aggregation forumula issue): once I apply the 'RemoveEmpty' function that is simply to scope out lines with empty Actual Qty/FCST Qty, the total will become weird (2nd pic below). Just wonder if you have any clue on it? Thanks!

yamacha_0-1727659223735.pngyamacha_1-1727659295934.png

 

 

Anonymous
Not applicable

Hi @yamacha ,
The ALLEXCEPT function is used in order to better determine the level at which you need to perform the subtraction to ensure that the correct subtraction is performed. If the ALLEXCEPT function is not included, it is possible that the two measurements will yield the same result according to the context of SUMX, which is in a nutshell to determine the level at which the subtraction should be performed to the Row of the matrix, and the level at which the subtraction should be performed. ALLEXCEPT is used to make a judgement on the Row, depending on what is put in the Row, and is not directly related to the different tables.
I would be honoured if my answer solves your query!

vxingshenmsft_1-1725341314807.png

Hope it helps!

 

Best regards,
Community Support Team_ Tom Shen

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

ahadkarimi
Solution Specialist
Solution Specialist

Hi @yamacha, try this SUMMARIZE, if you encounter any issues, let me know.

 

ERROR_LP_PLS = 
SUMX(
    SUMMARIZE(
        ACTUAL_LP,
        LP_ABBR[LP_ABBR],
        TIME[MonthDate],
        MASTER_PL[ProductLine5],
        "ActualQty", SUM(ACTUAL_LP[Actual Qty]),
        "ForecastQty", SUM(FCST_LP[FCST Qty])
    ),
    ABS([ActualQty] - [ForecastQty])
)

 

 

Did I answer your question?  If so, please mark my post as the solution!✔️
Your Kudos are much appreciated!  Proud to be a Responsive Resident!

Thanks ahadkarimi

I created two columns (end with Neo) with your codes as below. As only 'ACTUAL_LP' is summarized in your code so it will miss the lines w/o actual demand but still with forecast, and ultimately underestimate the error.

yamacha_0-1724917436106.png

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors