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
Dunner2020
Post Prodigy
Post Prodigy

Date difference when condition met

Hi there, 
 
I am calculating the date difference between two columns with a condition that if Date A column has '1/01/1900' then do not take the difference. Here is my code:
 
Diff_in_days  =
Var retail_notify = MAX('Outages'[Date B])
Var interuption_start = MAX('Outages'[Date A])
Var test_con = FORMAT(retail_notify, "dd/mm/yyyy")
RETURN
CALCULATE( DATEDIFF(retail_notify,interuption_start,DAY), FILTER('Outages and Interruptions',test_con <> FORMAT(DATEVALUE("1/01/1900"),"dd/mm/yyyy"))
)
 
However, it does take the date difference when the Date A column has "1/01/1900". I used the format function because Date A and Date B column has a date/time format. Could anyone help me where am I making the mistake?

 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Please try this expression

Diff_in_days =
VAR retail_notify =
    MAX ( 'Outages'[Date B] )
VAR interuption_start =
    MAX ( 'Outages'[Date A] )
RETURN
    IF (
        YEAR ( retail_notify ) > 1900,
        DATEDIFF ( interuption_start, retail_notify, DAY )
    )

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Employee
Employee

Please try this expression

Diff_in_days =
VAR retail_notify =
    MAX ( 'Outages'[Date B] )
VAR interuption_start =
    MAX ( 'Outages'[Date A] )
RETURN
    IF (
        YEAR ( retail_notify ) > 1900,
        DATEDIFF ( interuption_start, retail_notify, DAY )
    )

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


amitchandak
Super User
Super User

@Dunner2020 , Try like

Diff_in_days =
Var retail_notify = MAX('Outages'[Date B])
Var interuption_start = MAX('Outages'[Date A])
RETURN
CALCULATE( DATEDIFF(retail_notify,interuption_start,DAY), FILTER('Outages and Interruptions',not(isblank(retail_notify)) && retail_notify <> DATE(1900,01,01))
)

@amitchandak ,

 

I tried that but no luck. Here is the output

 

myasir_0-1596141134016.png

 

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.