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
roscoegray
Frequent Visitor

Slicing and Filtering records with duration

Hi All, 

 

I have a flat file event log that I am importing that has records for many assets over may days. I would like to visualise for any given time frame, whole or part of a day, how much duration that event was active for. I created a calculated column to calculate the duration of each event, however, I have an issue when filtering and I can't figure out how I would do it. 

 

The issue comes when i filter, for example, by day. An event that started before midnight would be filtered out. The same applies if i was looking at a period of the day, any events starting before this time would not have their duration included.

 

To demonstrate here is some data..

 

AssetEvent DateEvent TimeEventDuration
128/07/201822:00:00ONLINE15:00:00
129/07/201813:00:00OFFLINE01:00:00
129/07/201814:00:00ONLINE11:00:00
130/07/201801:00:00OFFLINE 

 

If i was to sum the duration and filter by 29/07/2018 i would get 11H ONLINE and 1H OFFLINE, when what i would like to get is 23H ONLINE and 1H OFFLINE. And if i filter between 12:00:00 29/07/2018 and 22:00:00 29/07/2018 i would like to get 9H ONLINE and 1H OFFLINE.

 

In my real dataset, i have more than two types of events and more assets. 

 

Any help would be greatly appreciated!!

1 ACCEPTED SOLUTION
roscoegray
Frequent Visitor

Not my best work but this seems to give me the desired result...

 

FilteredDuration =
SUMX (
    Events,
    SWITCH (
        TRUE (),
        Events[DateTime] > [MinDateTime]
            && Events[NextDateTime] < [MaxDateTime], Events[Durations (Seconds)],
        Events[DateTime] < [MinDateTime]
            && Events[NextDateTime] > [MinDateTime]
            && Events[NextDateTime] < [MaxDateTime], DATEDIFF ( [MinDateTime], Events[NextDateTime], SECOND ),
        Events[DateTime] > [MinDateTime]
            && Events[DateTime] < [MaxDateTime]
            && Events[NextDateTime] > [MaxDateTime], DATEDIFF ( Events[DateTime], [MaxDateTime], SECOND ),
        0
    )
)
MaxDateTime = MAXX((DateTable),[Date]) + MAXX((TimeTable),[Time])
MinDateTime = MINX((DateTable),[Date]) + MINX((TimeTable),[Time])

I have three cases for each depending on if the start or finish time is outside the filter to recalculate the duration. 

 

Please let me know if there is a better way but hopefully, this might help someone at the very least. 

View solution in original post

2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @roscoegray,

 

Thanks for sharing the solution.

 

Regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
roscoegray
Frequent Visitor

Not my best work but this seems to give me the desired result...

 

FilteredDuration =
SUMX (
    Events,
    SWITCH (
        TRUE (),
        Events[DateTime] > [MinDateTime]
            && Events[NextDateTime] < [MaxDateTime], Events[Durations (Seconds)],
        Events[DateTime] < [MinDateTime]
            && Events[NextDateTime] > [MinDateTime]
            && Events[NextDateTime] < [MaxDateTime], DATEDIFF ( [MinDateTime], Events[NextDateTime], SECOND ),
        Events[DateTime] > [MinDateTime]
            && Events[DateTime] < [MaxDateTime]
            && Events[NextDateTime] > [MaxDateTime], DATEDIFF ( Events[DateTime], [MaxDateTime], SECOND ),
        0
    )
)
MaxDateTime = MAXX((DateTable),[Date]) + MAXX((TimeTable),[Time])
MinDateTime = MINX((DateTable),[Date]) + MINX((TimeTable),[Time])

I have three cases for each depending on if the start or finish time is outside the filter to recalculate the duration. 

 

Please let me know if there is a better way but hopefully, this might help someone at the very least. 

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.