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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
PAschieri
Frequent Visitor

Difficult Sum Measure

Hi guys, I´m trying to get the following DAX:

 

If the company has activities > 0 for code 100, then SUM all hours for that company.

If the company has no activities for code 100, then Hours = 0.

 

The difficult part is that the result should ignore the company filter as below:

 

Captura de pantalla 2024-11-19 174636.png

 

 

 

 

 

 

 

Thanks!

2 ACCEPTED SOLUTIONS

HI @PAschieri ,
Understood now, please try the bellow measure:

Calculated Hours = 
VAR HasActivitiesCode100 = 
    CALCULATE(
        SUM('Activities'[Activities]),
        'Activities'[Code] = 100
    ) > 0

RETURN
IF(
    HasActivitiesCode100, 
    CALCULATE(SUM('Hours'[Hours]), ALLEXCEPT('Company', 'Company'[Name])),
    CALCULATE(SUM('Hours'[Hours]), ALLEXCEPT('Company', 'Company'[Name]))
)

 

 

Is this post help you? Please consider to:

Accept as Solution!
Give a Kudo
Follow me on Linkedin

View solution in original post

Hi @PAschieri ,

Thanks for all the replies!
And @PAschieri , I create sample data myself:

vjunyantmsft_0-1732168996621.png

You can use this DAX to create a measure:

TotalHoursWithCondition = 
VAR _table =
    CALCULATETABLE (
        VALUES ( Data[Company] ),
        FILTER ( ALL ( Data ), 'Data'[Code] = 100 )
    )
RETURN
    CALCULATE ( SUM ( 'Data'[Hours] ), ALL ( Data ), 'Data'[Company] IN _table )

And the final output is as below:

vjunyantmsft_1-1732169062879.png


Best Regards,
Dino Tao
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
Bibiano_Geraldo
Super User
Super User

Based on your description if company has no activities then the hours should be 0, so in your exepected result for B company, the measure should show 0, no?

If yes, please use this DAX bellow and let me know if its what you want.

Calculated Hours = 
VAR HasActivitiesCode100 = 
    CALCULATE(
        SUM('Activities'[Activities]),
        'Activities'[Code] = 100
    ) > 0

RETURN
IF(HasActivitiesCode100, SUM('Hours'[Hours]), 0)

 

Make sure to replace columns and table names with your owns.

 

 

Is this post help you? Please consider to:

Accept as Solution!
Give a Kudo
Follow me on Linkedin

Hi Bibiano. No, it should not indicate 0 for company B, that´s the difficult part of the measure.

 

No matter if the company has activities or no, the result should indicate 45 for all companies.

Hi @PAschieri ,

Thanks for all the replies!
And @PAschieri , I create sample data myself:

vjunyantmsft_0-1732168996621.png

You can use this DAX to create a measure:

TotalHoursWithCondition = 
VAR _table =
    CALCULATETABLE (
        VALUES ( Data[Company] ),
        FILTER ( ALL ( Data ), 'Data'[Code] = 100 )
    )
RETURN
    CALCULATE ( SUM ( 'Data'[Hours] ), ALL ( Data ), 'Data'[Company] IN _table )

And the final output is as below:

vjunyantmsft_1-1732169062879.png


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

HI @PAschieri ,
Understood now, please try the bellow measure:

Calculated Hours = 
VAR HasActivitiesCode100 = 
    CALCULATE(
        SUM('Activities'[Activities]),
        'Activities'[Code] = 100
    ) > 0

RETURN
IF(
    HasActivitiesCode100, 
    CALCULATE(SUM('Hours'[Hours]), ALLEXCEPT('Company', 'Company'[Name])),
    CALCULATE(SUM('Hours'[Hours]), ALLEXCEPT('Company', 'Company'[Name]))
)

 

 

Is this post help you? Please consider to:

Accept as Solution!
Give a Kudo
Follow me on Linkedin

jdbuchanan71
Super User
Super User

@PAschieri 

Based on your description "If the company has activities > 0 for code 100, then SUM all hours for that company." shouldn't the measure amount for Company B = 0?

2 measures

1 to sum hours

 

Total Hours = SUM(YourTable[Hours])

 

 

1 to calculate the total hours for the filtered list

 

MEASURE = 
VAR _100Companies =
    FILTER (
        VALUES ( YourTable[Company] ),
        CALCULATE ( SUM ( YourTable[Activities] ), ALLEXCEPT ( YourTable, YourTable[Company] ), YourTable[Code] = "100" ) > 0
    )
RETURN
    CALCULATE (
        SUMX ( ALLEXCEPT ( YourTable, YourTable[Company] ), [Total Hours] ),
        _100Companies
    )

 

jdbuchanan71_0-1732051880625.png

 

Hi, thanks for your answer but it´s not correct.  It should not indicate 0 for company B, that´s the difficult part of the measure.

 

No matter if the company has activities or no, the result should indicate 45 for all companies.

Helpful resources

Announcements
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! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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