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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
CJ_96601
Helper V
Helper V

Date Calculation with validation

Good afternoon, 

 

Please find below sample table as well as what's required.

 

Need to have this in advance editor

 

Date1Date2 (assuming current date is May 20, 2021)# of Days
15-May-21 6
1-Feb-205-Feb-204
1-Jul-2030-Jun-21324
   
   
if date 2 is null then current date minus date 1,  
 else if date 2 is greater than current date then current date - date 1 
 else date 2 - date 1   

 

I appreciate if somenone could provide a solution

2 ACCEPTED SOLUTIONS
Fowmy
Super User
Super User

@CJ_96601 

Paste below code in the Avdanced Editor and check the Add Column step:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTV9U2s1DUyVNJRUorViVYyMNR1S03SNTIAChiYwthQGa/SHIiMsQGQnQfSFhsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each  if [Date2]=null then Date.From(DateTime.LocalNow()) - [Date1] else if [Date2] > Date.From(DateTime.LocalNow()) then Date.From(DateTime.LocalNow()) - [Date1] else [Date2]-[Date1]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Int64.Type}})
in
    #"Changed Type1"

 

Fowmy_0-1621507846609.png

 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

Anonymous
Not applicable

Hi @CJ_96601,

You can also try to create a calculated column for calculation the datediff of the two date fields:

Diff =
DATEDIFF (
    [Date1],
    IF ( MIN ( [Date2], TODAY () ) <> BLANK (), [Date2], TODAY () ),
    DAY
)

Regards,

Xiaoxin Sheng

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @CJ_96601,

You can also try to create a calculated column for calculation the datediff of the two date fields:

Diff =
DATEDIFF (
    [Date1],
    IF ( MIN ( [Date2], TODAY () ) <> BLANK (), [Date2], TODAY () ),
    DAY
)

Regards,

Xiaoxin Sheng

Fowmy
Super User
Super User

@CJ_96601 

Paste below code in the Avdanced Editor and check the Add Column step:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTV9U2s1DUyVNJRUorViVYyMNR1S03SNTIAChiYwthQGa/SHIiMsQGQnQfSFhsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each  if [Date2]=null then Date.From(DateTime.LocalNow()) - [Date1] else if [Date2] > Date.From(DateTime.LocalNow()) then Date.From(DateTime.LocalNow()) - [Date1] else [Date2]-[Date1]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Int64.Type}})
in
    #"Changed Type1"

 

Fowmy_0-1621507846609.png

 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thanks..

Thanks, i have the same code but it's giving me an error on null values

Thanks, i have the same code but it's giving me an error.

 

What is the error? 
Please share the screenshot of the error.

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors