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
maneschr2022
Helper II
Helper II

Count text values and return 0 if blank

Hi everyone!

 

I´m looking for a DAX that allows me to count the total number of a specific text value in a column, but if it is none/blank I want to show 0 instead of blank.

 

I was trying to use this DAX formula:

 

Count of Red colour= COUNTROWS(FILTER(TABLE, TABLE[Colour]="Red")) +0

 

But for some reason when I add this measure as a value in a table it doesn´t load (my table already has a lot of values, not sure if that is related to that issue) when I remove the +0 it loads alright in the table, but I would like to show the 0 if there blank.

 

Is there another DAX that is better to do this? thanks!

1 ACCEPTED SOLUTION

@maneschr2022 

Please try

Count of Red colour =
IF (
    NOT ISBLANK ( COUNTROWS ( TABLE ) ),
    IF (
        ISBLANK ( COUNTROWS ( FILTER ( TABLE, TABLE[Colour] = "Red" ) ) ),
        0,
        COUNTROWS ( FILTER ( TABLE, TABLE[Colour] = "Red" ) )
    )
)

View solution in original post

6 REPLIES 6
maneschr2022
Helper II
Helper II

Hi @tamerj1 and @Aditya_Meshram both Dax work in terms of getting 0 instead of blank on my table visual, the only problem is that I get multiple rows instead of concentrated in one row, I`m using a column ID that generates the rows, but with this formula, I'm getting multiple times the same ID for 0, is there any DAX that can cover that issue as well all together?.

For the creation of my table, I'm using columns that belong to 2 different tables but they share in common the ID column.

@maneschr2022 

Please try

Count of Red colour =
IF (
    NOT ISBLANK ( COUNTROWS ( TABLE ) ),
    IF (
        ISBLANK ( COUNTROWS ( FILTER ( TABLE, TABLE[Colour] = "Red" ) ) ),
        0,
        COUNTROWS ( FILTER ( TABLE, TABLE[Colour] = "Red" ) )
    )
)

Thanks a lot! works perfect now!

Aditya_Meshram
Solution Supplier
Solution Supplier

Hi @maneschr2022 ,
this seems a very apt use case for the COALESCE() function. You can use it like :

 

Count of Red colour NOBLANK =
COALESCE(COUNTROWS(FILTER(TABLE, TABLE[Colour]="Red")),0)

 

 
Regards

This approach of using Coalesce() is much cleaner and easier to maintain as calculation is not repeated.

tamerj1
Super User
Super User

Hi @maneschr2022 

use 

Count of Red colour= IF ( ISBLANK (COUNTROWS(FILTER(TABLE, TABLE[Colour]="Red"))), 0, COUNTROWS(FILTER(TABLE, TABLE[Colour]="Red")))

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.