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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Nepal101
Helper III
Helper III

Distinct count with two filter from different table (DAX)

Hello, 
I am new to Dax and I need some help in getting an optimized way to create these measures. When I use these measures it takes a longer time to get the result 
First measure=CALCULATE(

    DISTINCTCOUNT(factInspectionViolation[InspectionKey]),

    FILTER( 'dimInspection', dimInspection[InspectionLevelNumber] IN {3,2,1} )

    )
Second Measure =

CALCULATE(

    DISTINCTCOUNT(factInspectionViolation[InspectionKey]),

    FILTER ( factInspectionViolation, [IsOutOfService] = "Y" ),

    FILTER( dimViolation, [BASIC] IN { "Driver Fitness", "Hours of Service Compliance", "Controlled Substances / Alcohol"  }

))

Third Measure=DIVIDE([SecondMeasure],[First Measuer])

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

See if it help to filter columns rather than tables.

 

DIVIDE (
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        FILTER (
            VALUES ( 'dimInspection'[InspectionLevelNumber] ),
            dimInspection[InspectionLevelNumber] IN { 3, 2, 1 }
        )
    ),
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        FILTER (
            VALUES ( factInspectionViolation[IsOutOfService] ),
            factInspectionViolation[IsOutOfService] = "Y"
        ),
        FILTER (
            VALUES ( dimViolation[BASIC] ),
            dimViolation[BASIC]
                IN {
                "Driver Fitness",
                "Hours of Service Compliance",
                "Controlled Substances / Alcohol"
            }
        )
    )
)

 

 

It's a bit cleaner-looking like this:

 

DIVIDE (
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        KEEPFILTERS ( dimInspection[InspectionLevelNumber] IN { 3, 2, 1 } )
    ),
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        KEEPFILTERS ( factInspectionViolation[IsOutOfService] = "Y" ),
        KEEPFILTERS ( dimViolation[BASIC]
            IN {
            "Driver Fitness",
            "Hours of Service Compliance",
            "Controlled Substances / Alcohol"
        } )
    )
)

 

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super User

See if it help to filter columns rather than tables.

 

DIVIDE (
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        FILTER (
            VALUES ( 'dimInspection'[InspectionLevelNumber] ),
            dimInspection[InspectionLevelNumber] IN { 3, 2, 1 }
        )
    ),
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        FILTER (
            VALUES ( factInspectionViolation[IsOutOfService] ),
            factInspectionViolation[IsOutOfService] = "Y"
        ),
        FILTER (
            VALUES ( dimViolation[BASIC] ),
            dimViolation[BASIC]
                IN {
                "Driver Fitness",
                "Hours of Service Compliance",
                "Controlled Substances / Alcohol"
            }
        )
    )
)

 

 

It's a bit cleaner-looking like this:

 

DIVIDE (
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        KEEPFILTERS ( dimInspection[InspectionLevelNumber] IN { 3, 2, 1 } )
    ),
    CALCULATE (
        DISTINCTCOUNT ( factInspectionViolation[InspectionKey] ),
        KEEPFILTERS ( factInspectionViolation[IsOutOfService] = "Y" ),
        KEEPFILTERS ( dimViolation[BASIC]
            IN {
            "Driver Fitness",
            "Hours of Service Compliance",
            "Controlled Substances / Alcohol"
        } )
    )
)

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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