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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
RafiQLB
Regular Visitor

Grand total problem incorrect on Power BI Matrix , How to Fix Sum of Metrics calculation

Hello Power BI community,

 

I would like your help in setting the total on a matrix. Indeed:

I have the following measures calculated in DAX:

1. Total Purchase Quantity: SUM (Purchased Quantity)

2. Total Sold Quantity: SUM (Sold Quantity)

3. TOTAL VARIANCE: SUM (Purchased Quantity) - SUM (Sold Quantity)

Subsequently, I calculated two other measures:

NEG TOTAL VARIANCE (-): SUM(TOTAL VARIANCE), IF (SUM(TOTAL VARIANCE) < 0)
POS TOTAL VARIANCE (+): SUM(TOTAL VARIANCE), IF (SUM(TOTAL VARIANCE) > 0)

Actually, I don't end up with correct grand totals (-) and totals (+) at the bottom of the matrix. Additionally, if I display both measurements on two displays, it gives me a single value, depending on the result: (-/+).

Can anyone suggest a solution?

 

Have a great day!

1 ACCEPTED SOLUTION
MasonMA
Impactful Individual
Impactful Individual

 

 

Hi @RafiQLB ,

 

Thanks for sharing the model. I was not able to see you also have a 'Cities' Dim table that's also filtering your 2 Fact tables. So the earlier SUMX(VALUES(Products[ProductID])) logic did not consider combinations of city + date + product. As a result, totals are still incorrect especially in filtered contexts.


You may test this below 

_POS TOTAL VARIANCE (-) = 
SUMX(
    ADDCOLUMNS(
        CROSSJOIN(
            VALUES(Products[ProductID]),
            VALUES(Cities[CityID]),
            VALUES(Caln[Date])
        ),
        "@Purchased", CALCULATE([Total Purchased Quantity]),
        "@Sold", CALCULATE([Total Sold Quantity]),
        "@Variance", CALCULATE([Total Purchased Quantity]) - CALCULATE([Total Sold Quantity])
    ),
    VAR _var = [@Variance]
    RETURN IF(_var < 0, _var, 0)
)

Same logic for your negative Variance calculation. 
Hope it works:) 

 

View solution in original post

6 REPLIES 6
MasonMA
Impactful Individual
Impactful Individual

 

 

Hi @RafiQLB ,

 

Thanks for sharing the model. I was not able to see you also have a 'Cities' Dim table that's also filtering your 2 Fact tables. So the earlier SUMX(VALUES(Products[ProductID])) logic did not consider combinations of city + date + product. As a result, totals are still incorrect especially in filtered contexts.


You may test this below 

_POS TOTAL VARIANCE (-) = 
SUMX(
    ADDCOLUMNS(
        CROSSJOIN(
            VALUES(Products[ProductID]),
            VALUES(Cities[CityID]),
            VALUES(Caln[Date])
        ),
        "@Purchased", CALCULATE([Total Purchased Quantity]),
        "@Sold", CALCULATE([Total Sold Quantity]),
        "@Variance", CALCULATE([Total Purchased Quantity]) - CALCULATE([Total Sold Quantity])
    ),
    VAR _var = [@Variance]
    RETURN IF(_var < 0, _var, 0)
)

Same logic for your negative Variance calculation. 
Hope it works:) 

 

Thank you very much, my dear.

 

You're truly a very responsive person.

You've done me a great service, and I thank you for the time you took to answer me.

Your solution seems to work well compared to the previous script.

 

I'll get back to you if necessary.

 

Have a great day.

MasonMA
Impactful Individual
Impactful Individual

@RafiQLB 

 

Hi, for your VARIANCE calculation, you would need to use Iterator functions on report, 

POS TOTAL VARIANCE (+) = 
SUMX(
    VALUES(SalesData[Product]),
    VAR _var = [TOTAL VARIANCE]
    RETURN IF(_var > 0, _var, 0)
)
NEG TOTAL VARIANCE (-) = 
SUMX(
    VALUES(SalesData[Product]),
    VAR _var = [TOTAL VARIANCE]
    RETURN IF(_var < 0, _var, 0)
)

so it will look like this on report

MasonMA_0-1752428458165.png

Hope it helps:)

Thank you @MasonMA  for your support, But it still not working because data are giving from different Tables so the total is giving wrong !

 

- Table 1 : Products.

- Table 2 : Purchasing.

- Table 3 : Sales

 

Many thanks 

MasonMA
Impactful Individual
Impactful Individual

HI @RafiQLB, Assuming you have a relationship like below in your model. Your Products Dim table is filtering Purchasing fact table and Sales fact table (remember i used a different sample data)

MasonMA_0-1752446048936.png

For the calculations, there might be different ways to author your codes, but if we keep using SUMX to Loop over a table, evaluate an expression for each row, and then sum the results.

We can use ADDCOLUMNS with VALUES to return a distinct list of ProductIDs in the current filter context and use CALCULATE inside to ensure correct context transition, For the Variance calculation, the codes can be adjusted to below (You will need to replace the table names and column names)

POS TOTAL VARIANCE (+)_Adj = 
SUMX(
    ADDCOLUMNS(
        VALUES(Products[ProductID]),
        "@Purchased", CALCULATE(SUM(Purchasing[Purchased Quantity])),
        "@Sold", CALCULATE(SUM(Sales[Sold Quantity])),
        "@Variance", CALCULATE(SUM(Purchasing[Purchased Quantity])) - CALCULATE(SUM(Sales[Sold Quantity]))
    ),
    VAR _var = [@Variance]
    RETURN IF(_var > 0, _var, 0)
)

and

NEG TOTAL VARIANCE (-)_Adj = 
SUMX(
    ADDCOLUMNS(
        VALUES(Products[ProductID]),
        "@Purchased", CALCULATE(SUM(Purchasing[Purchased Quantity])),
        "@Sold", CALCULATE(SUM(Sales[Sold Quantity])),
        "@Variance", CALCULATE(SUM(Purchasing[Purchased Quantity])) - CALCULATE(SUM(Sales[Sold Quantity]))
    ),
    VAR _var = [@Variance]
    RETURN IF(_var < 0, _var, 0)
)

With updated calculation you will see 

MasonMA_1-1752446487519.png

 

Thanky very much @MasonMA for your support,

 

But I still have incorrect Grand Total at the bottom of the matrix!

If the compensated Grand Total is (+) it displays nothing in total
and if the compensated Grand Total is (-) it displays the overall total compensated between the values (-) and (+)

 

Many thanks, Have a nice day

 

Screenshot 4.PNGScreenshot 2.PNG

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.