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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mandyhpnguyen
Regular Visitor

Fix Matrix Total in Dax Measure Column with Condition and Variables in Power BI?

Hi everyone, 

 

I have another Power BI Matrix Total issue as follows and hope you can help me with it.

 

I have a made-up dataset (end of this post) that is as close to my real project as possible so you can use it directly to solve the problem.

 

Final Expected Result

mandyhpnguyen_2-1675902332771.png

  

The basic questions are to create a matrix table to count the Headcount and sum the Working Hour by Animal Type with a few tweaks of conditions as follows: 

 

In both targeted outcomes, we limit only Domestic animals except for Dragon (use both wild and domestic).

 

1. Headcount

 

Condition: Create a table to count distinct Animal (distinct Animal ID) by Quarter and then sum up the Headcount of 4 quarters for each Animal Type (of course, only domestic animals except Dragon).

 

Expected Result: I use Excel pivot table to get the result as follows:

mandyhpnguyen_1-1675902293216.png

 

2. Working Hour

 

Condition: Create a table showing the totals of Working Hour of all "Domestic" animals except "Dragon" (count both domestic and wild dragons) by each Animal Type.

 

Expected Result: I used some sumifs condition in Excel to get this result:

mandyhpnguyen_3-1675902424719.png

 

IN POWER BI:

The best I can do give this results with the incorrect totals:

mandyhpnguyen_4-1675903001201.png

Here are my DAX codes:

 

HeadCount

 

HeadCount = 
var tot_q1 = 
IF (
    SELECTEDVALUE(AnimalLabor[Animal Type])="Dragon",
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q1"),
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q1", AnimalLabor[Location]="Domestic")
)

var tot_q2 = 
IF (
    SELECTEDVALUE(AnimalLabor[Animal Type])="Dragon",
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q2"),
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q2", AnimalLabor[Location]="Domestic")
)

var tot_q3 = 
IF (
    SELECTEDVALUE(AnimalLabor[Animal Type])="Dragon",
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q3"),
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q3", AnimalLabor[Location]="Domestic")
)

var tot_q4 = 
IF (
    SELECTEDVALUE(AnimalLabor[Animal Type])="Dragon",
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q4"),
    CALCULATE(DISTINCTCOUNT(AnimalLabor[Animal ID]), AnimalLabor[Quarter] = "Q4", AnimalLabor[Location]="Domestic")
)

return tot_q1 + tot_q2 + tot_q3 + tot_q4

 

 

Working Hour

 

Working Hour = 
IF (
    SELECTEDVALUE(AnimalLabor[Animal Type])="Dragon",
    CALCULATE(SUM(AnimalLabor[Hour Working])),
    CALCULATE(SUM(AnimalLabor[Hour Working]), AnimalLabor[Location]="Domestic")
)

 

 

------------------------------------

Dataset

 

LocationAnimal TypeAnimal IDHour WorkingQuarter
DomesticRat11Q1
DomesticRat34Q2
WildRat24Q1
WildRat42Q2
WildRat29Q3
DomesticRat63Q3
WildRat15Q3
WildOx15Q4
DomesticOx32Q1
DomesticOx47Q1
WildOx32Q2
WildOx74Q4
DomesticOx27Q3
WildOx53Q2
WildTiger14Q4
DomesticTiger16Q1
WildTiger48Q2
WildTiger28Q3
DomesticTiger31Q4
WildCat12Q4
WildCat23Q1
DomesticCat36Q1
DomesticCat24Q2
WildCat33Q2
WildDragon17Q4
DomesticDragon28Q4
DomesticDragon25Q1
WildDragon22Q1
DomesticDragon34Q2
DomesticDragon15Q3
WildDragon17Q1
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @mandyhpnguyen ,

This is because the context is difference, please try to create two new measure according to your original measure, try below dax formula:

Adjust Headcount = SUMX(VALUES('Table'[Animal Type]),[HeadCount])
Adjust Working Hour = SUMX(VALUES('Table'[Animal Type]),[Working Hour])

vbinbinyumsft_0-1676009045311.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Thennarasu_R
Responsive Resident
Responsive Resident

Hi @mandyhpnguyen 

Could you use this Measure.

Some times in Matrix Visual Calculate the total wrongly so that case We Can require this error 
First thing will Create a one measure
then,
Aply old measure into New measure.If we can apply in variables It won't work thats why This mandatory.


1  Actual Sales=Calculate(SUM(Sales))
2  Total Output  = Calculate(SUMX( VALUES (Accuracy[Product Name]), calculate (SUMX( VALUES (Accuracy[Product Name]), [ Actual Sales] )))
)


Thanks ,
Thennarasu

Thanks,

 

I got the exact solution in the comment below but you helped to explain the case!

 

I really appreciate it!

 

Best,

Mandy

Anonymous
Not applicable

Hi @mandyhpnguyen ,

This is because the context is difference, please try to create two new measure according to your original measure, try below dax formula:

Adjust Headcount = SUMX(VALUES('Table'[Animal Type]),[HeadCount])
Adjust Working Hour = SUMX(VALUES('Table'[Animal Type]),[Working Hour])

vbinbinyumsft_0-1676009045311.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks!

 

This is the correct solution for my particular case!

Best,

Mandy

amitchandak
Super User
Super User

@mandyhpnguyen , Change the return like this example

return Sumx(Values(AnimalLabor[Animal Type]), calculate( tot_q1 + tot_q2 + tot_q3 + tot_q4))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

@amitchandak I did but the result stayed the same. Did it work in your .pbix?

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.