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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.