Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

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 
2 ACCEPTED SOLUTIONS

@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?

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

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.

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

8 REPLIES 8
amitchandak
Super User
Super User

Please find the solution at : https://www.dropbox.com/s/s09wp65z2ma2b0j/diffwithlastOn.pbix?dl=0

Did not get logic for 36 at one place

 

Appreciate your Kudos.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi @amitchandak  

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

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

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Thank you @amitchandak . Please refer my comment below as well.

@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?

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

ImkeF
Community Champion
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.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

@ImkeF Thank you very much. I was not sure which solution to opt so asked for DAX assuming it would be faster. This linked mailbox will receive a lot of emails. Also as you had asked for each ON will there be a equivalent OFF. Yes, it it will be but it might take some days as well. Say I recieve the ON today, The OFF email might be recived even after 3 days. In those cases i would like to use todays date/time stamp (at that moment) until I get the OFF event and display the duration. Sorry I missed to mention this earlier. The intention here I would like to monitor those ON events which are active without OFF for more than 24 hours. Please help.

ImkeF
Community Champion
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?

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors