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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jereaallikko
Helper III
Helper III

How to calculate difference between wanted date and today with filters

Hi all,

 

I have a slight problem to solve. I am handling requests sent by our customers and want to calculate the duration of open requests.

I have "Status" column and and a column indicating the date of the sent request [Request sent at]. I would like to calculate the duration in days until today, when [Status]="Open". Requests with any other [Status], I would like to show "null".

 

I have tried many DAX expressions with columns and measures but nothing seems to work... 

 

Can anyone provide me a simple code, which helps me to solve the problem?

 

Thenks,

Jere

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @jereaallikko 

 

add a new column in Power Query with this formula

if _[Status]="Open" then Duration.TotalDays(Date.From(DateTime.FixedLocalNow())- [Request sent at]) else null

 

Here the complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7JL05NUdJRMjDUAyIjA6VYnWgl/4LUPJCYiZ6BKRYxCzQxQwM9IIKKIUy00DMwBovGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Status = _t, #"Request sent at" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Status", type text}, {"Request sent at", type date}}, "de-DE"),
    AddDurationOfOpenRequests = Table.AddColumn
    (
        #"Changed Type",
        "Duration if open",
        each if _[Status]="Open" then Duration.TotalDays(Date.From(DateTime.FixedLocalNow())- [Request sent at]) else null
    )
in
    AddDurationOfOpenRequests

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Hello @jereaallikko 

 

add a new column in Power Query with this formula

if _[Status]="Open" then Duration.TotalDays(Date.From(DateTime.FixedLocalNow())- [Request sent at]) else null

 

Here the complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7JL05NUdJRMjDUAyIjA6VYnWgl/4LUPJCYiZ6BKRYxCzQxQwM9IIKKIUy00DMwBovGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Status = _t, #"Request sent at" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Status", type text}, {"Request sent at", type date}}, "de-DE"),
    AddDurationOfOpenRequests = Table.AddColumn
    (
        #"Changed Type",
        "Duration if open",
        each if _[Status]="Open" then Duration.TotalDays(Date.From(DateTime.FixedLocalNow())- [Request sent at]) else null
    )
in
    AddDurationOfOpenRequests

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Hi @Jimmy801 

 

Solved the problem very quickly. Thanks a lot!

 

Br,

Jere

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors