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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

How to do a Count IF with condition using measure?

Hello, 
I need to get a count of employee numbers that are greater than 0 on Average variance, but I am getting an error. Can anybody please help me how to fix it? 

 

Avg_Daily_Var._ % = IFERROR([Avg_Daily_Submitted]/[Avg_Daily_Reqquired],0)

 

 

CountEmp# = CALCULATE(COUNT(Employee[firstlast]), [Avg_Daily_Var._ %] >0.0)

 

 

Error - A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

 

I tried adding a filter that did not work. I want to create a measure, not a calculated column. 

My sample data looks like, 

Capture.PNG

 

Any help will be highly appreciated

Thank you 

 

2 ACCEPTED SOLUTIONS
Ashish_Mathur
Super User
Super User

Hi,

Try these measures:

Avg_Daily_Var._ % = DIVIDE([Avg_Daily_Submitted],[Avg_Daily_Reqquired],0)
CountEmp# = COUNTROWS(FILTER(VALUES(Employee[firstlast]),[Avg_Daily_Var._ %] >0.0))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

@Anonymous , you might want to try

CountEmp#_2 =
COUNTROWS (
    FILTER (
        VALUES ( Employee[firstlast] ),
        [Avg_Daily_Var._ %] < 0.0
            && [Avg_Daily_Var._ %] > -0.10
    )
)

or equally

CountEmp#_2 =
COUNTROWS (
    FILTER (
        VALUES ( Employee[firstlast] ),
        AND ( [Avg_Daily_Var._ %] < 0.0, [Avg_Daily_Var._ %] > -0.10 )
    )
)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

Try these measures:

Avg_Daily_Var._ % = DIVIDE([Avg_Daily_Submitted],[Avg_Daily_Reqquired],0)
CountEmp# = COUNTROWS(FILTER(VALUES(Employee[firstlast]),[Avg_Daily_Var._ %] >0.0))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

HI, thank you so much. This is the one I am looking for but what happens if I have more than one argument. Please see below 

CountEmp#_2 = COUNTROWS(FILTER(VALUES(Employee[firstlast]),([Avg_Daily_Var._ %] < 0.0 AND [Avg_Daily_Var._ %] > -0.10))

I tried it,  did not work. Can you please tell me how to add more than 1 condition on measures? 

 

Thank you so much

 

@Anonymous , you might want to try

CountEmp#_2 =
COUNTROWS (
    FILTER (
        VALUES ( Employee[firstlast] ),
        [Avg_Daily_Var._ %] < 0.0
            && [Avg_Daily_Var._ %] > -0.10
    )
)

or equally

CountEmp#_2 =
COUNTROWS (
    FILTER (
        VALUES ( Employee[firstlast] ),
        AND ( [Avg_Daily_Var._ %] < 0.0, [Avg_Daily_Var._ %] > -0.10 )
    )
)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Awesome, thank you so much for the help. 

edhans
Super User
Super User

Hi @Anonymous , I would redo the logic a bit. First of all, redo your first measure:

Avg_Daily_Var._ % =
DIVIDE(
    [Avg_Daily_Submitted],
    [Avg_Daily_Reqquired],
    0
)

DIVIDE is a safe divide and will return an alternate result (zero in this case) if the denominator is zero or some other error happens. Faster than IFERROR. You should never use the '/' symbol in DAX. Always use DIVIDE().

 

 As to the other measure try this:

CountEmp# =
CALCULATE(
    COUNT( Employee[firstlast] ),
    Employee[SomeField] > 0
)

Where SomeField is a field that would need to have a value (vs 0) for there to be any average. Using a field within CALCULATE will work. Otherwise you'll need to replace the whole comparison with a FILTER() function. I'd need to have data though to really play with it, and that includes the formulas for your other measures, since you are embedding measure inside of measure inside of measure. 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

Hi, sorry, that I don't understand how Employee[SomeField] > 0 works. All I am trying to count the number of employees Employee[SomeField] > 0 and  Employee[SomeField] < 0 (by the way this will be a different measure, not in the same measure). I am not sure how Employee[SomeField] > 0 get me that. Thank you 

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors
Top Kudoed Authors