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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
icassiem
Post Partisan
Post Partisan

Calculate Date Durations between 2 dates

Good day,

 

i would like to calculate the duration between 2 dates, so my visual will be per stage and its duration

1. Do I create a calculated column for duration between the 2 dates?

2. then a calculated measure to sum the duration and convert in days or hours

 

please help with the duration column & measure calculations

i hope the method of a calculated column then calculated measure is correct

please advise and help with calculation scripts

 

icassiem_0-1733321489543.png

 

Regards

1 ACCEPTED SOLUTION
rajendraongole1
Super User
Super User

Hi @icassiem - your suggested method of using a calculated column for individual durations and a calculated measure for aggregation is correct.

 

using datediff. calculated column calculate duration days:

 

Duration_Days =
DATEDIFF(
TableName[StartDate],
TableName[EndDate],
DAY
)

create a measure for total duration as 

Total_Duration =
SUM(TableName[Duration_Days])

 

Convert Duration to Hours or Mixed Format

Duration_Format =
VAR Days = INT(TableName[Duration_Hours] / 24)
VAR Hours = MOD(TableName[Duration_Hours], 24)
RETURN
Days & " days " & Hours & " hours"

 

In your visual e.g., a table or matrix, add the "Stage" field to the rows.Add the Total_Duration measure to display the summed duration per stage. 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @icassiem 

 

Please try this:

Table:

vzhengdxumsft_0-1733365101079.png

Then add 2 calculated columns:

duration_Day =
ABS (
    DATEDIFF ( 'Table'[HistoryClosedDateMax], 'Table'[HistoryCreatedDateMin], DAY )
)
duration_Hour =
ABS (
    DATEDIFF ( 'Table'[HistoryClosedDateMax], 'Table'[HistoryCreatedDateMin], HOUR )
)

And 2 Measures:

_duration_Day =
ABS (
    DATEDIFF (
        MAX ( 'Table'[HistoryClosedDateMax] ),
        MAX ( 'Table'[HistoryCreatedDateMin] ),
        DAY
    )
)
_duration_Hour =
ABS (
    DATEDIFF (
        MAX ( 'Table'[HistoryClosedDateMax] ),
        MAX ( 'Table'[HistoryCreatedDateMin] ),
        HOUR
    )
)

The result is as follow:

vzhengdxumsft_1-1733365221569.pngvzhengdxumsft_2-1733365256630.png

Here for your reference:

DATEDIFF function (DAX) - DAX | Microsoft Learn

ABS function (DAX) - DAX | Microsoft Learn

 

Best Regards

Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

rajendraongole1
Super User
Super User

Hi @icassiem - your suggested method of using a calculated column for individual durations and a calculated measure for aggregation is correct.

 

using datediff. calculated column calculate duration days:

 

Duration_Days =
DATEDIFF(
TableName[StartDate],
TableName[EndDate],
DAY
)

create a measure for total duration as 

Total_Duration =
SUM(TableName[Duration_Days])

 

Convert Duration to Hours or Mixed Format

Duration_Format =
VAR Days = INT(TableName[Duration_Hours] / 24)
VAR Hours = MOD(TableName[Duration_Hours], 24)
RETURN
Days & " days " & Hours & " hours"

 

In your visual e.g., a table or matrix, add the "Stage" field to the rows.Add the Total_Duration measure to display the summed duration per stage. 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Thank You both @rajendraongole1  and @Anonymous 

 

i used @rajendraongole1 approach, @Anonymous  as i was unsure of calculations having both max when i have in pivot visual slicing and dicing

 

I think i have it incorrect with the total and it needs to be an average time per stage or sub stage

How would I change the total duration to average if it is by opportunity, stage and sub stage, does it mean i have to calc the distinct opportuntity first "sales force data"?

 

please help

 

icassiem_0-1733398217491.png

 

ended up doing: AVERAGE(SalesForce_SalesStageVelocity[duration_Day])
 
i am now trying to place the average between 2 dates (created and modified)
 
DurationAVG_1 =
var _rangeStart = SalesForce_SalesStageVelocity[SalesStageVelocitySelectedStartDate]
var _rangeEnd = SalesForce_SalesStageVelocity[SalesStageVelocitySelectedEndDate]    

return
//if(
    //SELECTEDVALUE(SalesForce_SalesStageVelocity[HistoryCreatedDateMin])>=_rangeStart
    //&& SELECTEDVALUE(SalesForce_SalesStageVelocity[HistoryModifiedDateMax])>_rangeEnd
//,AVERAGE(SalesForce_SalesStageVelocity[duration_Day])
//,0)

CALCULATE(AVERAGE(SalesForce_SalesStageVelocity[duration_Day]),FILTER / SELECTEDVALUE(SalesForce_SalesStageVelocity[HistoryCreatedDateMin])>=_rangeStart )
 
please help, i am trying to filter then only avg on the filtered date range between the create and mod date

Hi,

 

Any update on calculating the average between 2 date fields "from and to"?

 

Regards

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors