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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
amon151
Frequent Visitor

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)

 

amon151_0-1658850100283.png

 

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
1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community 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

 

 

SpartaBI_0-1658852795886.png

 

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

 

 

SpartaBI_1-1658852970132.png


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

View solution in original post

6 REPLIES 6
SpartaBI
Community Champion
Community 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

 

 

SpartaBI_0-1658852795886.png

 

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

 

 

SpartaBI_1-1658852970132.png


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

The solution you provided for the column is just what I needed!!

 

Thank you so much for the quick response 👍

SpartaBI
Community Champion
Community Champion

@amon151 my pleasure 🙂

Check out my showcase report - got some high level stuff there. Sure you will find there a lot of cool ideas. 
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
Give it a thumbs up over there if you liked it 🙂

amitchandak
Super User
Super User

@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

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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

 

amon151_0-1658861546337.png

 

SpartaBI
Community Champion
Community Champion

@amon151 you need this as a measure or as a calculate column?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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
Top Kudoed Authors