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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Date difference measure in DAX

Hi All,

I am calculating the difference in seconds between two dates in my data model. For calculation purpose I have converted two of my date columns from fact to measure using the below :

Created = if(HASONEVALUE('Fact'[Submitted]),values('Fact'[sys_created_on]),BLANK())
Closed = if(HASONEVALUE('Fact'[closed_at]),values('Fact'[closed_at]),BLANK())
Now I am trying to create a measure for no of seconds between the above two measures as :
Measure = CALCULATE(DATEDIFF([Resolved_Date],[Created],DAY),
filter(DimDate,DimDate[Weekday]="Weekday"))
But the issue is that I am the measure is returning a constant value of 43951 as seconds
 

 

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Or do like this.

1. Cretae 4 calculated columns.

Weekday_sub = WEEKDAY([submitted], 2)
Weekday_res = WEEKDAY([resolved], 2) 
Weeknum_sub = WEEKNUM( [submitted], 2 ) 
Weeknum_res = WEEKNUM( [resolved], 2 ) 

2. Create a measure.

Measure = 
CALCULATE(
    DATEDIFF(
        MAX('Fact'[submitted]), MAX( 'Fact'[resolved]),
        DAY
    ),
    FILTER(
        'Fact',
        ( 'Fact'[Weekday_sub] in {6, 7} || 'Fact'[Weekday_res] in {6, 7} ) && 
        ( 'Fact'[Weeknum_sub] = 'Fact'[Weeknum_res] ) && 
        ( ( 'Fact'[Weekday_res] in {6, 7} && 'Fact'[Weekday_sub] in {1, 2, 3, 4, 5} ) || ( 'Fact'[Weekday_sub] in {6, 7} && 'Fact'[Weekday_res] in {1, 2, 3, 4, 5}) )
    )
)

 

Best regards,
Lionel Chen

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

View solution in original post

7 REPLIES 7
Pascal_KTeam
Resolver I
Resolver I

Take a look at the blue card visual in this file https://www.dropbox.com/s/ww9063que0bcvrn/datediff%20in%20second.pbix?dl=0

If I understood your requirement correctly, this should hopefully help

Anonymous
Not applicable

@Pascal_KTeam  thanks but this does not solve my purpose. I have to calculate the duration in seconds based on certain conditions that I have mentioned in my earlier message.

 

amitchandak
Super User
Super User

@Anonymous , date dim is not joined to dates and date diff has no role there. so it is not working

Refer to this file how to get working day diff

https://www.dropbox.com/s/y47ah38sr157l7t/Order_delivery_date_diff.pbix?dl=0

 

Blog has instructions : https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Difference-Across/ba-p/934397#M451

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

Maybe I was not clear with my requirements so, My fact table has two date columns submitted and resolved_at now I want to calculate the number of seconds between these two dates based on the conditions below

a. submitted and resolved on weekends

b.submitted and resolved in the same week

c. submitted on weekends and resolved on a weekday

d.submitted on a weekday and resolved on weekend

I have a date dimension that is connected to the fact on submitted and resolved_at dates.

 

thanks

 

Hi @Anonymous ,

 

Or do like this.

1. Cretae 4 calculated columns.

Weekday_sub = WEEKDAY([submitted], 2)
Weekday_res = WEEKDAY([resolved], 2) 
Weeknum_sub = WEEKNUM( [submitted], 2 ) 
Weeknum_res = WEEKNUM( [resolved], 2 ) 

2. Create a measure.

Measure = 
CALCULATE(
    DATEDIFF(
        MAX('Fact'[submitted]), MAX( 'Fact'[resolved]),
        DAY
    ),
    FILTER(
        'Fact',
        ( 'Fact'[Weekday_sub] in {6, 7} || 'Fact'[Weekday_res] in {6, 7} ) && 
        ( 'Fact'[Weeknum_sub] = 'Fact'[Weeknum_res] ) && 
        ( ( 'Fact'[Weekday_res] in {6, 7} && 'Fact'[Weekday_sub] in {1, 2, 3, 4, 5} ) || ( 'Fact'[Weekday_sub] in {6, 7} && 'Fact'[Weekday_res] in {1, 2, 3, 4, 5}) )
    )
)

 

Best regards,
Lionel Chen

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

Anonymous
Not applicable

Thanks @v-lionel-msft , It worked like a charm

Anonymous
Not applicable

instead of

Measure = CALCULATE(DATEDIFF([Resolved_Date],[Created],DAY),

try

Measure = CALCULATE(DATEDIFF([Resolved_Date],[Created],SECOND),

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.