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
Ade1991
Helper I
Helper I

Add a filter to Calculate Countrow

 

Hi,
I want to translate the "COUNTIF" function from Excel in DAX. In DAX, I use
=CALCULATE(COUNTROWS(TableName); ALLEXCEPT(TableName, TableName[Items]))
However, I would like to add a filter : my data are like that :

 

word1
word2
word2_word3
word4_word5_word6
word9_word10_word11
 
I want a filter with the number of underscore _ contained in the cell. Any tips to add this kind of filter to my formula ?
(For example, in order to select all the cell with 2 underscore :
word4_word5_word6
word9_word10_word11)
 
Thanks
1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

@Ade1991 

 

Try with following

 

Column =
CALCULATE (
    COUNTROWS ( TableName ),
    FILTER (
        ALLEXCEPT ( TableName, TableName[Items] ),
        PATHLENGTH ( SUBSTITUTE ( [Items], "_", "|" ) ) - 1 = 2
    )
)

Regards
Zubair

Please try my custom visuals

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

1) Create Index Table for Slicer from What-if-Parameter

Create Index TableCreate Index Table

2) Create below Measure

 

FilterByNumberofCharacters =
VAR Indexs = [IndexTable Value] -- Index Table Measure (automattically created based on name given)
RETURN
CALCULATE(SELECTEDVALUE(TextFilter[TextHeader],BLANK()),FILTER(VALUES(TextFilter[TextHeader]),LEN(TextFilter[TextHeader])-LEN(SUBSTITUTE(TextFilter[TextHeader],"_","")) = Indexs))
3) Add TextFilter[TextHeader] in matrix, FilterByNumberofCharacters in filter panel and filter on "is not blank"
4) Now when you filter by index slicer you will only see text with number of Underscore you selected
 
Let me know if this works!
Zubair_Muhammad
Community Champion
Community Champion

@Ade1991 

 

Try with following

 

Column =
CALCULATE (
    COUNTROWS ( TableName ),
    FILTER (
        ALLEXCEPT ( TableName, TableName[Items] ),
        PATHLENGTH ( SUBSTITUTE ( [Items], "_", "|" ) ) - 1 = 2
    )
)

Regards
Zubair

Please try my custom visuals

@Zubair_MuhammadThanks very much, it's workin very well.

 

However, can you explain me the formula :

PATHLENGTH ( SUBSTITUTE ( [Items], "_", "|" ) ) - 1 = 2

Not sure to understand it very well.

 

Moreover, you need sometimes to use ; instead of ,.

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.