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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
dhanekula1996
Helper I
Helper I

Dax to group by and count with Filter

Hello,
I am trying to count the distinct stop numbers which are "On time".  I need to group by driver name, date, and stop number and get the count of On-Time deliveries. 
I got the required Solution using this DAX: 

On_Time_Del = COUNTROWS(SUMMARIZE(FILTER('Samp_table,'Samp_table'[Type of Stop]="On Time Stop"),'Samp_table'[Date],'Samp_table'[stop_number]))


But this is extremely slow!! Is there any other way I can get the solution? 
Thank you for your time!!

This is a sample driver's data for example This driver made 14 stops in a day and on 13 the top he delivered 2 order.

Sample_Data:

IdNameDateStop NumberOrder NumberType of Stop
1Mark Hudson8/23/2022        1123On Time
2Mark Hudson8/23/2022        2456On Time
3Mark Hudson8/23/2022        3789On Time
4Mark Hudson8/23/2022       4134On Time
5Mark Hudson8/23/2022       5156On Time
6Mark Hudson8/23/2022       645546On Time
7Mark Hudson8/23/2022       756566Delayed
8Mark Hudson8/23/2022       86567Delayed
9Mark Hudson8/23/2022       9178On Time
10Mark Hudson8/23/2022       1046677On Time
11Mark Hudson8/23/2022       11465665On Time
12Mark Hudson8/23/2022       123434On Time
13Mark Hudson8/23/2022       13232344On Time
14Mark Hudson8/23/2022       1325554On Time
15Mark Hudson8/23/2022       1487900Delayed

 

@amitchandak 

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @dhanekula1996 ,

 

I suggest you to try this code to create a measure.

On_Time_Del =
CALCULATE (
    COUNT ( Samp_table[Id] ),
    FILTER (
        ALLEXCEPT (
            Samp_table,
            Samp_table[Name],
            Samp_table[Date],
            Samp_table[Stop Number]
        ),
        'Samp_table'[Type of Stop] = "On Time Stop"
    )
)

 

Best Regards,
Rico Zhou

 

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

3 REPLIES 3
v-rzhou-msft
Community Support
Community Support

Hi @dhanekula1996 ,

 

I suggest you to try this code to create a measure.

On_Time_Del =
CALCULATE (
    COUNT ( Samp_table[Id] ),
    FILTER (
        ALLEXCEPT (
            Samp_table,
            Samp_table[Name],
            Samp_table[Date],
            Samp_table[Stop Number]
        ),
        'Samp_table'[Type of Stop] = "On Time Stop"
    )
)

 

Best Regards,
Rico Zhou

 

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

Whitewater100
Solution Sage
Solution Sage

Hi: A faster way can be to go to Transform Data and use GroupBy:

Whitewater100_0-1662213639739.png

Then your table can look like this:

Whitewater100_1-1662213707387.png

It's not DAX but a way to solve. I hope this helps!

Adescrit
Impactful Individual
Impactful Individual

Hi @dhanekula1996 ,

 

How about this:

 

# On-Time = 
COUNTROWS (
    SUMMARIZECOLUMNS (
        'Table'[Name],
        'Table'[Date],
        'Table'[Stop Number],
        FILTER ( 'Table', 'Table'[Type of Stop] = "On Time" )
    )
)

 

When you want to filter a calculated/summarized table, SUMMARIZECOLUMNS is a nice solution.


Did I answer your question? Mark my post as a solution!
My LinkedIn

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors