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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

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
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors