Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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!
Solved! Go to Solution.
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:)
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.
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
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
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)
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
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
Check out the July 2025 Power BI update to learn about new features.
User | Count |
---|---|
24 | |
9 | |
7 | |
6 | |
6 |
User | Count |
---|---|
29 | |
11 | |
11 | |
10 | |
6 |