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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

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
    )
)

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
    )
)

@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
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors