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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Calculate time difference between specific rows

Hello Power BI Community,

 

I was hoping you may offer some help to a novice Power BI User.

 

I need to calculate how long a pump has been running for. If 'Pump1 RN' = 1 that means it is running, if 'Pump1 RN' = 0 it means it isn't running. 

 

How do I work out the total time that the pump has been running?

 

Power2BI_0-1630067784822.png

 

 

1 ACCEPTED SOLUTION
Jos_Woolley
Solution Sage
Solution Sage

Ok, thanks. Try this measure:

 

MyMeasure =
VAR MyTable =
    ADDCOLUMNS (
        'Table',
        "Previous Date.Time",
            CALCULATE (
                MAX ( 'Table'[Date.Time] ),
                FILTER ( 'Table', 'Table'[Date.Time] < EARLIER ( 'Table'[Date.Time] ) )
            ),
        "Previous Pump 1 RN",
            LOOKUPVALUE (
                'Table'[Pump 1 RN],
                'Table'[Date.Time],
                    CALCULATE (
                        MAX ( 'Table'[Date.Time] ),
                        FILTER ( 'Table', 'Table'[Date.Time] < EARLIER ( 'Table'[Date.Time] ) )
                    )
            )
    )
RETURN
    SUMX (
        MyTable,
        IF ( [Previous Pump 1 RN] = 1, [Date.Time] - [Previous Date.Time] )
    )

 

though this will return 14:00 for the data you give, not 13:57. I personally think this makes more sense (i.e. assuming the pump runs right up until the next 0); returning 13:57 would be a bit trickier.

This measure will respond to filters on the Date.Time field, e.g. filtering between 01/04/2020 04:00 and 01/04/2020 11:00 will produce a result of 07:00.

Regards

View solution in original post

7 REPLIES 7
Jos_Woolley
Solution Sage
Solution Sage

Hi,

Actually that result is perfectly correct for the filtering you've applied. A filter such as the one you've applied does not automatically include the entries in between the ones you've selected - it simply filters for those two dates.

I'll let you play about with other filter visuals and options (for example, sliders) until you've found the right one for you.

Regards

Jos_Woolley
Solution Sage
Solution Sage

Ok, thanks. Try this measure:

 

MyMeasure =
VAR MyTable =
    ADDCOLUMNS (
        'Table',
        "Previous Date.Time",
            CALCULATE (
                MAX ( 'Table'[Date.Time] ),
                FILTER ( 'Table', 'Table'[Date.Time] < EARLIER ( 'Table'[Date.Time] ) )
            ),
        "Previous Pump 1 RN",
            LOOKUPVALUE (
                'Table'[Pump 1 RN],
                'Table'[Date.Time],
                    CALCULATE (
                        MAX ( 'Table'[Date.Time] ),
                        FILTER ( 'Table', 'Table'[Date.Time] < EARLIER ( 'Table'[Date.Time] ) )
                    )
            )
    )
RETURN
    SUMX (
        MyTable,
        IF ( [Previous Pump 1 RN] = 1, [Date.Time] - [Previous Date.Time] )
    )

 

though this will return 14:00 for the data you give, not 13:57. I personally think this makes more sense (i.e. assuming the pump runs right up until the next 0); returning 13:57 would be a bit trickier.

This measure will respond to filters on the Date.Time field, e.g. filtering between 01/04/2020 04:00 and 01/04/2020 11:00 will produce a result of 07:00.

Regards

Anonymous
Not applicable

Hi Jos, thanks so much for taking the time to try and work on this. Unfortunately it doesn't seem to work, I have tried on my data and the sample data which I provided. For example, if I have no filters selected it will show 14:00 as you stated, however if I put a filter in (as per the below) it then shows 18:00 which isn't correct. I have it in the format of (hh:mm)

 

Power2BI_0-1630490426662.png

 

Jos_Woolley
Solution Sage
Solution Sage

Then since your Date.Time intervals are consistenly of length 1 hour, you can just sum the Pump 1 RN column. If in reality the Date.Time intervals are not as you posted, please post a more realistic example. Also, perhaps confirm your expected result for the sample data you posted.

Regards

Anonymous
Not applicable

Hi Jos, yes sorry I understand what you mean. I have updated the post with a more realistic example of the data. The pump is/isn't running until the next time stamp. For example, the pump has ran for a total of 13hrs and 57 minutes from 01/04/2020 00:00:00 to 01/05/2020 02:30:00

Jos_Woolley
Solution Sage
Solution Sage

Hi,

If the pump was running at 06:00 but not at 07:00, what is the assumption about the time at which it stopped running? 06:01? 06:59?

Regards

Anonymous
Not applicable

Hello, it would be 06:59

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.