Forum Discussion

RafiQLB's avatar
RafiQLB
Regular Visitor
1 year ago
Solved

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) ...
  • MasonMA's avatar
    1 year ago

     

     

    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:)