Forum Discussion

Sachintha's avatar
Sachintha
Helper III
3 years ago
Solved

Calculate difference between rows based on slicer selection

I have two tables as follows.   First, a list of alamrs occurred on a bunch of machines, ordered by time stamp.   TimeStamp,MachineID,Alarm 2022/08/13 12:32:24,P211,A1 2022/08/14 23:45:00,D566,A...
  • HoangHugo's avatar
    3 years ago

    Hi

    Your index colum is fix number, so it canot be correct if you use slicer. In this case, create a measure to calculate Diff which can be dynamic apply slicer.

     

    Diff =

    var time = SELECTEDVALUE(Timestamp column)

    return DATEDIFF(time,MAXX(CALCULATETABLE(Alarm table,REMOVEFILTER(Timestamp column),Timestamp column < Time),Timestamp column)

     

    then we calculate Average

    MITI = AVERAGEX(SUMMARIZE(Timestamp Column),Diff)

  • Sachintha's avatar
    Sachintha
    3 years ago

    I just rfigured out that this can be achieved with a much simpler method without having to sequentially calculate the time differences using an index.

     

    First, grab the MAX and MIN time stamps of the dataset. This will ensure data adheres to any user selected filters based on slicers.

     


    _maxTime = MAX(Alarms[TimeStamp])

    _minTime = MIN(Alarms[TimeStamp])
    Calculate time differences between the two using DATEDIFF.
      

    _timeDiff = DATEDIFF([_minTime], [_maxTime], HOUR)

    Then divide the time difference by number of rows minus 1.

     

    MTTI = DIVIDE([_timeDiff], [_countRows] - 1)

    This calculates the desired results while adhering to filter selections, and does not take exponentially long time.