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
al4x
Frequent Visitor

DAX - Perform filter on table based on row counts

Hi,

 

Having only just gotten my head around how CALCULATE filter i have a question on how do best achieve what i want.

 

I have a data table in the form

 

Cat1, Cat2, Country, Data

 

B,Z,France,10

B,Y,France,20

B,X,France,10

C,Z,France,20

C,Y,France,20

...

Where Cat1 can only contain B and C but rest are free, and so is the table length.

 

I want to only display an AVERAGE of [Data] for B

when the DISTINCTCOUNT of [Cat2] where [Cat1] = B

is 1 GREATER THAN

the DISTINCTCOUNT of [Cat2] where [Cat1] = C

 

But always display AVERAGE of [Data] for C

 

This is displayed on a bar plot

 

ie in the demo table, only a bar for AVERGAE [Data] for 'C' would be displayed as the DISTINCTCOUNT of  B for Cat2 is 3

vs the DISTINCTCOUNT of  C for Cat2 is 2

and 3-2 = 1

 

Is the best way to do this with varibles? for example s variables in the form of the below would give me the two numbers

 

    CALCULATE(
        DISTINCTCOUNT(Dat_Table[cat2]),
        FILTER(
            Dat_Table,
            Dat_Table[cat1] = "B"
        )
    )

 

or is there a better way?

 

I should add that this table has a number of filters attached to it and the above scenario needs to operate from the ouput of these.

1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @al4x,

 

Please try this meaure:

Average =
IF (
    LASTNONBLANK ( Dat_Table[Cat1], 1 ) = "B",
    IF (
        CALCULATE (
            DISTINCTCOUNT ( Dat_Table[Cat2] ),
            FILTER ( ALL ( Dat_Table ), Dat_Table[Cat1] = "B" )
        )
            - CALCULATE (
                DISTINCTCOUNT ( Dat_Table[Cat2] ),
                FILTER ( ALL ( Dat_Table ), Dat_Table[Cat1] = "C" )
            )
            >= 2,
        CALCULATE (
            AVERAGE ( Dat_Table[Data] ),
            FILTER ( Dat_Table, Dat_Table[Cat1] = "B" )
        ),
        BLANK ()
    ),
    CALCULATE (
        AVERAGE ( Dat_Table[Data] ),
        FILTER ( Dat_Table, Dat_Table[Cat1] = "C" )
    )
)

1.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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

1 REPLY 1
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @al4x,

 

Please try this meaure:

Average =
IF (
    LASTNONBLANK ( Dat_Table[Cat1], 1 ) = "B",
    IF (
        CALCULATE (
            DISTINCTCOUNT ( Dat_Table[Cat2] ),
            FILTER ( ALL ( Dat_Table ), Dat_Table[Cat1] = "B" )
        )
            - CALCULATE (
                DISTINCTCOUNT ( Dat_Table[Cat2] ),
                FILTER ( ALL ( Dat_Table ), Dat_Table[Cat1] = "C" )
            )
            >= 2,
        CALCULATE (
            AVERAGE ( Dat_Table[Data] ),
            FILTER ( Dat_Table, Dat_Table[Cat1] = "B" )
        ),
        BLANK ()
    ),
    CALCULATE (
        AVERAGE ( Dat_Table[Data] ),
        FILTER ( Dat_Table, Dat_Table[Cat1] = "C" )
    )
)

1.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

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.

Top Solution Authors
Top Kudoed Authors