Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

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
  • Anonymous 

     

    Try with following

     

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

3 Replies

  • Zubair_Muhammad's avatar
    Zubair_Muhammad
    Community Champion

    Anonymous 

     

    Try with following

     

    Column =
    CALCULATE (
        COUNTROWS ( TableName ),
        FILTER (
            ALLEXCEPT ( TableName, TableName[Items] ),
            PATHLENGTH ( SUBSTITUTE ( [Items], "_", "|" ) ) - 1 = 2
        )
    )
    
    • Anonymous's avatar
      Anonymous
      Not applicable

      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 ,.

  • Anonymous's avatar
    Anonymous
    Not applicable

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

    Create 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!