Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Row and column wise conditional difference

I have created a power BI dashboard to monitor the emails coming to a shared mailbox (MS office 365 account/exchange). I need help to write a dax query to create a new column (Duration in hours). As shown below for every OFF event of same name and serial I need to check the subsequent rows for an ON event (same name and serial) and then find the difference between Date/time stamp. This will result in getting the duration between ON/OFF even of each name / serial.

 

Example: The OFF event of Alarm 1 Hi (serial 1234) @ 3/10/2020 0:00 has a equivalent ON event in the subsequent rows @ 3/8/2020 12:00 (Alarm 1 Hi , serial 1234). Subtracting the date/time stamp of these two give me 36 hours.

 

EmailSerialNameONOFFDateDuration hrs
Email 121234Alarm 1 HiOFF3/12/2020 0:0024.00
Email 111234Alarm 1 HiON3/11/2020 0:00 
Email 101234Alarm 1 HiOFF3/10/2020 0:0036.00
Email 91234Alarm 2 HiOFF3/9/2020 0:0046.50
Email 81234Alarm 1 HiON3/8/2020 12:00 
Email 71234Alarm 2 HiON3/7/2020 1:30 
Email 61234Alarm 1 LoOFF3/6/2020 0:0024.00
Email 51234Alarm 1 LoON3/5/2020 0:00 
Email 45678Alarm 1 HiOFF3/4/2020 0:0024.00
Email 35678Alarm 1 HiON3/3/2020 0:00 
Email 25678Alarm 1 HiOFF3/2/2020 19:0924.03
Email 15678Alarm 1 HiON3/1/2020 19:07 
  • Anonymous , is there any change in the solution you want in the initial post. I posted a solution where email 10 was not matching as email 9 off and email 10 is off too. Have you reviewed that?

  • ImkeF's avatar
    ImkeF
    6 years ago

    Hi Anonymous 

    please check attached solution.

    It contains a DAX and a Power Query solution.

    If you're importing hundreds/thousands of emails, PQ might not play out it's advantages here. 

    Would be interested to hear which version runs faster for your case.

     

     

8 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi Anonymous  

    may I ask why you want a DAX-solution for it?

    A solution in the query editor would most likely be faster and compress better.

    Also: Is it guaranteed that every OFF-event has a corresponding ON-event like in your sample data (and vice versa) or could there be single ON or OFFs without a counterpart?

     

    • ImkeF's avatar
      ImkeF
      Community Champion

      Hi amitchandak  

      the match has to be on "Name"-level as well.

      Then you'd have a pair with email 10 and email 8.

       

      • ImkeF's avatar
        ImkeF
        Community Champion

        Attaching the file with the adjusted DAX - solution from amitchandak ,

        if you run into performance issues, please come back with my questions answered .

         

        core of that solution is a new column that contains the corresponding "ON"-Value in each "OFF"-row:

         

        Last Close =
        IF (
            Sheet1[ONOFF] = "OFF",
            MAXX (
                FILTER (
                    Sheet1,
                    ( Sheet1[Date] ) < EARLIER ( Sheet1[Date] )
                        && Sheet1[ONOFF] = "ON"
                        && Sheet1[Serial] = EARLIER ( Sheet1[Serial] )
                        && Sheet1[Name] = EARLIER ( Sheet1[Name] )
                ),
                Sheet1[Date]
            ),
            BLANK ()
        )
        

        But with 3 EARLIERs it will probably not run fast on large datasets.