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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
BurgerToPro
Frequent Visitor

Sum of TotalHours between reoccuring activity name in DAX

Thanks in advance for helping a new member out!


I am working in the transport sector and would like to create an overview that shows how many hours a driver has spent in a country during a certain week for each time he has been in a country in chronological order. So far, I only achieved an overview of the total hours a driver has spent in each country and not every time it has been in a country.


The result would look something like this and (if possible) should be solved in DAX:

Slicer: Driver ID = 301
Slicer: Weeknr = 13

CountryIDTotal Hours
31 10
325
313
328
3312
326


My data
I have 1 big table that consists of the following:

ActivityStartDateTimeActivityIDDriverIDTotalHoursActivityNameMany more columns....
18-05-21 15:09:001001233163,6Driving 
18-05-21 15:39:00101243160,5Pause 
18-05-21 16:39:00101253161Driving 
18-05-21 16:54:00101263160,25Border 
18-05-21 16:59:00101273161,05Driving 
18-05-21 19:09:00101283162,12Border 
+1.499.999 rows     

We can assume that ActivityStartDate is in chronological order for all activities, this is not the case for ActivityID.
This table has 100+ different drivers and in total has 1.5 million rows.

The problem

When I select a week and a driver with a slicer, I need an overview of his total hours. The first activity for that driver in that week decides the start country. To select this activity I thought about min(ActivityStartDateTime).

From this activity, I would like to Sum(TotalHours) until I reached the first ActivityName = "Border".

For the second country I have to Sum(TotalHours) from the first ActivityName = "Border" until the second ActivityName = "Border".

This has to continue until I reached the Max(ActivityStarteDateTime) in that week.
It is possible that a driver will cross 8 borders in one week. Then I would need a result that lists 9 different countries. (Start country + 8 new ones)

P.S.

This is my first post, so let me know if I need to change/add anything. I have spent a lot of hours trying to solve this problem using the shared knowledge in the community. Unfortunately, I did not achieve the desired result.
Good luck!

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

Hi, @BurgerToPro 

Is there a field ‘CountryID’ in your data table? Does it allow multiple duplicate values in your result?

veasonfmsft_0-1653289326390.png

If so, add the following calculated column for grouping and sorting, than apply it to your table:

Sort_Country =
CALCULATE (
    COUNT ( 'Table'[ActivityName] ),
    FILTER (
        ALLEXCEPT ( 'Table', 'Table'[DriverID] ),
        'Table'[ActivityStartDateTime] < EARLIER ( 'Table'[ActivityStartDateTime] )
            && 'Table'[ActivityName] = "Border"
    )
) + 1

Please share more details if I misunderstood.

 

Best Regards,
Community Support Team _ Eason

View solution in original post

1 REPLY 1
v-easonf-msft
Community Support
Community Support

Hi, @BurgerToPro 

Is there a field ‘CountryID’ in your data table? Does it allow multiple duplicate values in your result?

veasonfmsft_0-1653289326390.png

If so, add the following calculated column for grouping and sorting, than apply it to your table:

Sort_Country =
CALCULATE (
    COUNT ( 'Table'[ActivityName] ),
    FILTER (
        ALLEXCEPT ( 'Table', 'Table'[DriverID] ),
        'Table'[ActivityStartDateTime] < EARLIER ( 'Table'[ActivityStartDateTime] )
            && 'Table'[ActivityName] = "Border"
    )
) + 1

Please share more details if I misunderstood.

 

Best Regards,
Community Support Team _ Eason

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors