Forum Discussion

roscoegray's avatar
roscoegray
Frequent Visitor
8 years ago
Solved

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!!

  • 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. 

2 Replies

  • roscoegray's avatar
    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.