Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

time duration between time frames

Hi all, I'm trying to calculate the duration of an event, but expressed as its parts falling between specific time frames. There are two tables, stops_table and hours_table: stops_table shows the ...
  • v-lili6-msft's avatar
    6 years ago

    hi Anonymous 

    You could try this way as below:

    Step1:

    Add two columns in the stops_table as below:

    _maxhoursstart = CALCULATE(MAX(hours_table[hour start]),FILTER(hours_table,hours_table[hour start]<=stops_table[Stop start]))
    _minhoursend = CALCULATE(MIN(hours_table[hour end]),FILTER(hours_table,hours_table[hour end]>=stops_table[Stop end]))

    Step2:

    Then use this formula to create a measure

    Result = 
    VAR _table =
        FILTER (
            CROSSJOIN ( stops_table, hours_table ),
            stops_table[_maxhoursstart] <= hours_table[hour start]
                && stops_table[_minhoursend] >= hours_table[hour end]
        )
    RETURN
        SUMX (
            _table,
            IF (
                [Stop start] >= [hour start]
                    && [Stop end] <= [hour end],
                DATEDIFF ( [Stop start], [Stop end], MINUTE ),
                IF (
                    [Stop start] >= [hour start]
                        && [Stop end] > [hour end],
                    DATEDIFF ( [Stop start], [hour end], MINUTE ),
                    IF (
                        [Stop start] < [hour start]
                            && [Stop end] < [hour end],
                        DATEDIFF ( [hour start], [Stop end], MINUTE )
                    )
                )
            )
        )

    Result:

    Regards,

    Lin