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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
IvanMislav
Helper I
Helper I

Dynamic label based on custom measure

Hi, i have a simple data model. Something like company name, income, expenses.

 

I created a custom measure Balance = income - expenses.

 

What I want to do is create a dynamic lable: 

  •  if balance < 0 = "Negative"
  • if balance >= 0 = "Positive"

Finally, I want to have a number of companies by type.

 

I wrote a measure "Number of negative companies" = CALCULATE(DISTINCTCOUNT(data[CompanyName]);Balance<0) and I get an error saying "A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed."

 

What am I doing wrong?

 

Thank you!

 

 

5 REPLIES 5
Greg_Deckler
Super User
Super User

You could do this:

 

Label = 
SWITCH(TRUE(),
    [Balance]<0,"negative",
    [Balance]>0,"positive",
    "zero"
)

NegativeCount = 
VAR __table = SUMMARIZECOLUMNS('Table19'[Company],'table19',"__balance",[Balance])
RETURN
COUNTX(FILTER(__table,[__balance]<0),[Company])

See attached Table 19, Page 11


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi, Greg!

 

Thank you for your answer. I can't find the attached table.

Whoops, forgot to attach.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks, I can see the file now.

 

This works when there are unique values for companies. In my table company name values are not unique.

Works for me. See updated attachment. Not sure why you would get different results. Perhaps share your sample data. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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