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
prab
Employee
Employee

Measure to return 0(zero) if distinct count is blank

Need some help from the community. 

 

I am trying to get the count of programs for each month (date as lastdayofthemonth) with target as above. However if for any month, there are no program with target as 'Above' then the measure should return 0. 

 

Measure = CALCULATE(DISTINCTCOUNT('Table'[Program]),'Table'[Target (Below/Above)]="Above")
In the "Month wise Above" visual want see "Friday January 31" as 0,  
 

image.png

Sample data

 

MonthYearProgramLocationProjectTarget (Below/Above)LastdayofMonth
1/1/2020ABCHydp1below1/31/2020
1/1/2020XYZHydp2below1/31/2020
1/1/2020TEST1Hydp1below1/31/2020
1/1/2020ABCHydp2below1/31/2020
1/1/2020XYZHydp2below1/31/2020
1/1/2020cb1blrp2within1/31/2020
1/1/2020dumblrp2within1/31/2020
1/1/2020vixblrp2within1/31/2020
1/1/2020bullblrp2within1/31/2020
12/1/2019ABCHydp1below12/31/2019
12/1/2019XYZHydp2below12/31/2019
12/1/2019TEST1Hydp1above12/31/2019
12/1/2019ABCHydp2below12/31/2019
12/1/2019XYZHydp2below12/31/2019
12/1/2019cb1blrp2within12/31/2019
12/1/2019dumblrp2below12/31/2019
12/1/2019vixblrp2within12/31/2019
12/1/2019bullblrp2within12/31/2019
11/1/2019ABCHydp1below11/30/2019
11/1/2019XYZHydp2below11/30/2019
11/1/2019TEST1Hydp1within11/30/2019
11/1/2019ABCHydp2above11/30/2019
11/1/2019XYZHydp2below11/30/2019
11/1/2019cb1blrp2within11/30/2019
11/1/2019dumblrp2below11/30/2019
11/1/2019vixblrp2within11/30/2019
11/1/2019bullblrp2within11/30/2019
1 ACCEPTED SOLUTION

Your source data doesn't have an "Above" record for Jan 31, 2020.  The measure will return "0" with no issue, but your data table doesn't have a row with "Above" on it to provide any filter context for the measure to work.

 

You may want to create a Dimension table with Above, Below, and Within, and use that table to populate your visuals, creating a One-To-Many to your Fact table, then everything will have an Above/Below/Within, and the measure would then work.



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

View solution in original post

9 REPLIES 9
farid_0709
Frequent Visitor

@amitchandak why you have used +0 at the end of Distinctcount?

prab
Employee
Employee

Thank You @edhans for quick solution. 

 

However the measure works unitl the Target is not added to the table visual.

Below in the highlighted Visual count for "Above" is missng for January. 

Is there a way to fix this.

 

Thank Again. 

 

image.png

Your source data doesn't have an "Above" record for Jan 31, 2020.  The measure will return "0" with no issue, but your data table doesn't have a row with "Above" on it to provide any filter context for the measure to work.

 

You may want to create a Dimension table with Above, Below, and Within, and use that table to populate your visuals, creating a One-To-Many to your Fact table, then everything will have an Above/Below/Within, and the measure would then work.



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

Thank You @edhans it works..

 

 

Great @prab. Glad it works for you.



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
amitchandak
Super User
Super User

try

Measure = CALCULATE(DISTINCTCOUNT('Table'[Program]),'Table'[Target (Below/Above)]="Above")+0

What does the + zero do at the end?

It converts a blank to a zero. See Handling BLANK in DAX - SQLBI - the example there is BLANK()+4 = 4, but BLANK()+0 equals 0.
Another way to do it is to use the COALESCE() function. 

COALESCE([Some Measure or Expression], 0) would return the first non-blank value. If the measure is blank, it returns zero.



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
edhans
Super User
Super User

If your measure is correctly returning what you want, and the issue is if it returns blank you want it to return 0 instead, use this:

 

Measure =
VAR ProgramCount =
    CALCULATE(
        DISTINCTCOUNT( 'Table'[Program] ),
        'Table'[Target (Below/Above)] = "Above"
    )
RETURN
    IF(
        ProgramCount = BLANK(),
        0,
        ProgramCount
    )

 

It simply says if ProgramCount (which is your measure in the VAR statement) is blank, return 0, otherwise the value of ProgramCount.



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

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.