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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
TeoTreny
Frequent Visitor

Calculate duration between undefined amount of dates

Hi,

I need to calculate the total amount of duration between an unknow amount of start/end dates. I also need to make sure not to include a duration if the current time is before the start time, or to include the duration between start and current if it has started but not ended.

 Here is a sample of my data :

Area (text)Plot (number)StartDate (datetime)EndDate (datetime)
A121/12/2023 05:40:1021/12/2023 06:40:06
A221/12/2023 08:06:1321/12/2023 08:48:32
A321/12/2023 9:20:0021/12/2023 10:00:00
A121/12/2023 9:20:0021/12/2023 10:00:00
B121/12/2023 6:33:1421/12/2023 6:50:56


Moreover, I would like to group the result by Plot-Area so that I only have one row for each Plot-Area. I specify that my table is in DirectQuery mode, which limits access to a number of functions.

Please forgive my poor choice of words, english is not my mother tongue.

Téo T.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @TeoTreny ,

You can create a measure as below to get it, please find the details in the attachment.

Note: My current date time is on 28/12/2023 11:15:00~

TotalDuartion = 
VAR _tab =
    SUMMARIZE (
        'Table',
        'Table'[Area],
        'Table'[Plot],
        'Table'[StartDate],
        'Table'[EndDate],
        "@duration",
            SWITCH (
                TRUE (),
                NOW () < 'Table'[StartDate], 0,
                'Table'[StartDate] < NOW ()
                    && 'Table'[EndDate] > NOW (), DATEDIFF ( 'Table'[StartDate], NOW (), MINUTE ),
                'Table'[EndDate] < NOW (), DATEDIFF ( 'Table'[StartDate], 'Table'[EndDate], MINUTE )
            )
    )
RETURN
    SUMX ( _tab, [@duration] )

vyiruanmsft_0-1703733427936.png

Best Regards

View solution in original post

9 REPLIES 9
Anonymous
Not applicable

Hi @TeoTreny ,

You can create a calculated column as below to get it:

Column = 
VAR _Seconds =
    DATEDIFF ( 'Table'[StartDate], 'Table'[EndDate], SECOND )
VAR _Minutes =
    INT ( DIVIDE ( _Seconds, 60 ) )
VAR _RemainingSeconds =
    MOD ( _Seconds, 60 )
VAR _Hours =
    INT ( DIVIDE ( _Minutes, 60 ) )
VAR _RemainingMinutes =
    MOD ( _Minutes, 60 )
RETURN
    FORMAT ( _Hours, "00" ) & ":"
        & FORMAT ( _RemainingMinutes, "00" ) & ":"
        & FORMAT ( _RemainingSeconds, "00" )

vyiruanmsft_0-1703216204821.png

Best Regards

Thank you for your answer.
It works perfectly but doesn't consider the current time, it just calculates the duration between the dates. I need to compare the start date to current time to check if the break has started, check if the current time is lower than end date to know if the break is finished, and calculate the correct duration considering the situation. I know how to do it in DAX but I was wondering if it was possible with DirectQuery considering that my table is in Direct Query mode.

Later I need to group by Area - Plot in order to have only one row for each Area - Plot.

Is it possible ? If not I will accept your post as a solution.

Anonymous
Not applicable

Hi @TeoTreny ,

Could you please explain and provide the expected result with some sample data for the following sentences? It would be helpful to find the solution. Thank you.

1. Compare the start date to current time to check if the break has started, check if the current time is lower than end date to know if the break is finished, and calculate the correct duration considering the situation.  


2. Later I need to group by Area - Plot in order to have only one row for each Area - Plot.


Best Regards

Hi, I would like to answer your request but my post keeps getting deleted without telling me why...
Is it because it includes 3 tables ?

EDIT : Here is a picture of the example to show you my desired output. (I would like to include table so you can copy but my post keeps getting deleted).

TeoTreny_0-1703675050624.png

 

For the first step, I have to consider the actual time (as a datetime, let's name it ActualDate).
The logic looks like this :
IF ActualDate < StartDate : 0 minute have passed
IF StartDate < ActualDate < EndDate : [ActualDate - StartDate] minutes have passed
IF EndDate < ActualDate : [EndDate - StartDate] minutes have passed.

For the second step, I just want to have one row for each Area - Plot. It is much easier for me to deal with later on.

Please note that the input table is in DirectQuery mode.

Anonymous
Not applicable

Hi @TeoTreny ,

You can create a measure as below to get it, please find the details in the attachment.

Note: My current date time is on 28/12/2023 11:15:00~

TotalDuartion = 
VAR _tab =
    SUMMARIZE (
        'Table',
        'Table'[Area],
        'Table'[Plot],
        'Table'[StartDate],
        'Table'[EndDate],
        "@duration",
            SWITCH (
                TRUE (),
                NOW () < 'Table'[StartDate], 0,
                'Table'[StartDate] < NOW ()
                    && 'Table'[EndDate] > NOW (), DATEDIFF ( 'Table'[StartDate], NOW (), MINUTE ),
                'Table'[EndDate] < NOW (), DATEDIFF ( 'Table'[StartDate], 'Table'[EndDate], MINUTE )
            )
    )
RETURN
    SUMX ( _tab, [@duration] )

vyiruanmsft_0-1703733427936.png

Best Regards

Thanks a lot !
I had to modify it a little but it does the job perfectly !

VijayP
Super User
Super User

@TeoTreny 

Hopefully it should work

VijayP_0-1703152004974.png

 




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Hi,
Thanks for your quick answer. Indeed, it works but only in Import mode, DirectQuery doesn't allow this :

TeoTreny_0-1703152501950.png

Translated in english it means : "This step generates a query that is not supported in DirectQuery mode --> Switch table to Import mode" 

This is mostly my biggest problem, DirectQuery is making this really hard.

Forgot to include the error, my bad :

TeoTreny_0-1703152837054.png

Translated : "Sorry... Couldn't use the < operator with Date and DateTime"

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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