Forum Discussion

amon151's avatar
amon151
Frequent Visitor
4 years ago
Solved

Countif for dynamic criteria range

Hello!

 

I'm trying to replicate the excel COUNTIF function in DAX. I basically want to count the number of instances each value of column B appears in column A range.

 

Excel Formula

=COUNTIF($A:$A,B2)

 

 

I came across the DAX formula below, but it doesn't seem to be doing what I want or perhaps I'm implementing it incorrectly.

=CALCULATE(COUNT('Table'[Column A]),ALLEXCEPT('Table','Table'[Column B]))
 
Any help is greatly appreciated!!!
Thanks
  • amon151  if a column then this is the code: 

     

    Output = 
    VAR _b = 'Table'[Column B]
    VAR _result = 
    COUNTROWS(
        FILTER(
            'Table',
            'Table'[Column A] = _b
        )
    )
    RETURN
        _result

     

     

     

    If a measure then this:

     

    Output Measure = 
    VAR _b = SELECTEDVALUE('Table'[Column B])
    VAR _result = 
    COUNTROWS(
        FILTER(
            ALL('Table'),
            'Table'[Column A] = _b
        )
    )
    RETURN
        COALESCE(_result, "")

     

     





          

    Showcase Report – Contoso By SpartaBI

6 Replies

  • amon151 , Check update from SpartaBI 

     

    Try a new column

    = countx(filter(Table, Table[ColumnA] = earlier(Table[Column B]) ), [ColumnA])+ 0

     

    A new measure

    = countx(filter(allselected(Table), Table[ColumnA] = max(Table[Column B]) ), [ColumnA])+ 0

    • amon151's avatar
      amon151
      Frequent Visitor

      Thank you!

       

      This approach seemed to work for the most part but wasn't able to handle when Column B was blank (ignore the third column, the output of your formula is in column D).

       

       

  • SpartaBI's avatar
    SpartaBI
    Icon for Community Champion rankCommunity Champion

    amon151  if a column then this is the code: 

     

    Output = 
    VAR _b = 'Table'[Column B]
    VAR _result = 
    COUNTROWS(
        FILTER(
            'Table',
            'Table'[Column A] = _b
        )
    )
    RETURN
        _result

     

     

     

    If a measure then this:

     

    Output Measure = 
    VAR _b = SELECTEDVALUE('Table'[Column B])
    VAR _result = 
    COUNTROWS(
        FILTER(
            ALL('Table'),
            'Table'[Column A] = _b
        )
    )
    RETURN
        COALESCE(_result, "")

     

     





          

    Showcase Report – Contoso By SpartaBI