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

Converting null values into default date in power Query

Hi there,

 

I have a script that converts the date column into local standard time and also considers the daylight saving. The date column (I want to convert) has also got null values. I used the script as a query and creating a new date column using that script. I tried to include the if statement to replace the null value with '1/1/1900' as the default value but it looks like it did not work. Here is my code:

 

= (datetimecolumn as datetime) =>

let

date = DateTime.Date(datetimecolumn),
time = DateTime.Time(datetimecolumn),
lastSundayOfSeptember = Date.StartOfWeek(#date(Date.Year(date), 9, 28), Day.Sunday),
FirstSundayOfApril = Date.StartOfWeek(#date(Date.Year(date), 4, 7), Day.Sunday),


isSummerTime = (date = lastSundayOfSeptember and time >= #time(1,0,0))
or
(date > FirstSundayOfApril and date < lastSundayOfSeptember)
or
(date = FirstSundayOfApril and time >= #time(1,0,0)),


timeZone = (-13 - Number.From(isSummerTime))*-1,

MDT = if date <> null then
DateTime.From(date)
+ #duration(0,Time.Hour(time),Time.Minute(time),Time.Second(time))
+ #duration(0, timeZone, 0, 0)
else
Date.FromText("1/1/1900")

in
MDT

 

After running the above code, it still returns the error for the null values. The error says that I could not convert the null value into the DateTime type. I have limited knowledge of power query. Could anyone help me where I made the mistake?

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@Dunner2020  I'm not sure if t is d it be the as haven't dug into this yet, but could it be that you're using Date.FromText instead of DateTime.FromText? 

 

Alternatively, you could do a replace values for null to 1/1/1900 first, then run the custom function.


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

1 REPLY 1
AllisonKennedy
Super User
Super User

@Dunner2020  I'm not sure if t is d it be the as haven't dug into this yet, but could it be that you're using Date.FromText instead of DateTime.FromText? 

 

Alternatively, you could do a replace values for null to 1/1/1900 first, then run the custom function.


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

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.