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

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

Reply
Edmundas
Frequent Visitor

Countifs formula in Power BI

Please help me to write an excel COUNTIFS formula in Power BI table column. I've tried various ways to make it work with FILTER, COUNTROWS and etc. but didn't get desired result.

I have a table (below) and need to find patients who checked out or checked in at the clinic during January 2022 - March 2022. 

Thanks in advance!

 

Patients.jpg

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @Edmundas ,

Here are the steps you can follow:

1. In Power query. Add Column – Index Column – From 1.

vyangliumsft_0-1663206584926.png

2. Create calculated column

Countifs =
VAR _count =
    COUNTX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[Doctor] = EARLIER ( 'Table'[Doctor] )
                && YEAR ( 'Table'[Period] ) = YEAR ( EARLIER ( 'Table'[Period] ) )
                && MONTH ( 'Table'[Period] ) = MONTH ( EARLIER ( 'Table'[Period] ) )
        ),
        [Doctor]
    )
VAR _if =
    IF (
        MONTH ( 'Table'[Period] ) >= 1
            && MONTH ( 'Table'[Period] ) <= 3
            && YEAR ( 'Table'[Period] ) = 2022,
        _count,
        0
    )
RETURN
    IF (
        _if < 2,
        _if,
        IF (
            _if >= 2
                && 'Table'[Index]
                    = MINX (
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Doctor] = EARLIER ( 'Table'[Doctor] )
                                && YEAR ( 'Table'[Period] ) = YEAR ( EARLIER ( 'Table'[Period] ) )
                                && MONTH ( 'Table'[Period] ) = MONTH ( EARLIER ( 'Table'[Period] ) )
                        ),
                        [Index]
                    ),
            CALCULATE (
                DISTINCTCOUNT ( 'Table'[Doctor] ),
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[Doctor] = EARLIER ( 'Table'[Doctor] )
                        && YEAR ( 'Table'[Period] ) = YEAR ( EARLIER ( 'Table'[Period] ) )
                        && MONTH ( 'Table'[Period] ) = MONTH ( EARLIER ( 'Table'[Period] ) )
                )
            ),
            0
        )
    )

3. Result:

vyangliumsft_1-1663206584930.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

2 REPLIES 2
Edmundas
Frequent Visitor

It worked!!! Thank you so much

Anonymous
Not applicable

Hi  @Edmundas ,

Here are the steps you can follow:

1. In Power query. Add Column – Index Column – From 1.

vyangliumsft_0-1663206584926.png

2. Create calculated column

Countifs =
VAR _count =
    COUNTX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[Doctor] = EARLIER ( 'Table'[Doctor] )
                && YEAR ( 'Table'[Period] ) = YEAR ( EARLIER ( 'Table'[Period] ) )
                && MONTH ( 'Table'[Period] ) = MONTH ( EARLIER ( 'Table'[Period] ) )
        ),
        [Doctor]
    )
VAR _if =
    IF (
        MONTH ( 'Table'[Period] ) >= 1
            && MONTH ( 'Table'[Period] ) <= 3
            && YEAR ( 'Table'[Period] ) = 2022,
        _count,
        0
    )
RETURN
    IF (
        _if < 2,
        _if,
        IF (
            _if >= 2
                && 'Table'[Index]
                    = MINX (
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Doctor] = EARLIER ( 'Table'[Doctor] )
                                && YEAR ( 'Table'[Period] ) = YEAR ( EARLIER ( 'Table'[Period] ) )
                                && MONTH ( 'Table'[Period] ) = MONTH ( EARLIER ( 'Table'[Period] ) )
                        ),
                        [Index]
                    ),
            CALCULATE (
                DISTINCTCOUNT ( 'Table'[Doctor] ),
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[Doctor] = EARLIER ( 'Table'[Doctor] )
                        && YEAR ( 'Table'[Period] ) = YEAR ( EARLIER ( 'Table'[Period] ) )
                        && MONTH ( 'Table'[Period] ) = MONTH ( EARLIER ( 'Table'[Period] ) )
                )
            ),
            0
        )
    )

3. Result:

vyangliumsft_1-1663206584930.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.