cancel
Showing results for 
Search instead for 
Did you mean: 
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
RicoZhou
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
RicoZhou
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
Super User
Super User

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
Super User
Super User

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
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.

Top Solution Authors
Top Kudoed Authors