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
dsjaff
New Member

Date.fromtext passing too early of a date to dataverse

I'm trying to sanitize some records with bad dates that were originally stored as text by copying them into a new field. For the records that are stored as alpha characters, this line is correctly passing a null to the new date column :

try Date.FromText([cr09e_column11]) otherwise null
 
but if the date it tries to pass is something like 12/31/159, dataverse errors out as the date it too far in the past. How do I add another error check that passes null if the date is prior to dataverse's maximum date of 01/01/1753?
2 ACCEPTED SOLUTIONS
watts_jim
Helper II
Helper II

You'll want something along the lines of this :

if Number.FromText(Text.AfterDelimiter([cr09e_column11],"/",1)) < 1953 then null else [cr09e_column11]

View solution in original post

watts_jim
Helper II
Helper II

Null is a special case - try Googling 'power query null'.

 

You'll want something like this:
if [cr09e_column15] = null then null else if Number.FromText([cr09e_column15]) < 1753 then null else try Date.FromText(Text.Combine({[cr09e_column14],[cr09e_column13],[cr09e_column15]},"/")) otherwise null

View solution in original post

8 REPLIES 8
dsjaff
New Member

Can you tell me why "try Number.FromText(194*)  otherwise null" fails? If that value it come across isn't a number, because of the astericks, why is the code failing? Is "194*" a number to power query?

Because * is character for multply. I you try this one - it will return null.

 

= try Number.FromText("194*") otherwise null

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

dufoq3
Super User
Super User

Hi @dsjaff, what about this?

 

Result

dufoq3_0-1710253157346.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTSNzbUNzIwMlaK1YFxDU0twTxkIUtTAzDXwFDfAMwFaogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    ReplacedValue = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Date"}),
    AddedCustom = Table.AddColumn(ReplacedValue, "Custom", each try if Date.From([Date], "en-US") < #date(1953,1,1) then null else Date.From([Date], "en-US") otherwise null, type date)
in
    AddedCustom

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

This has way too much going on for me to understand. I've only used power query a handful of times.

Of cource if you do not use PQ often, it is not easy to understand some queries. Were you able to use my query? If no check note below my posts or just use part of code form AddedCustom step and edit with your needs (column name). Add it as custom column.

try if Date.From([Date], "en-US") < #date(1953,1,1) then null else Date.From([Date], "en-US") otherwise null

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

watts_jim
Helper II
Helper II

Null is a special case - try Googling 'power query null'.

 

You'll want something like this:
if [cr09e_column15] = null then null else if Number.FromText([cr09e_column15]) < 1753 then null else try Date.FromText(Text.Combine({[cr09e_column14],[cr09e_column13],[cr09e_column15]},"/")) otherwise null

watts_jim
Helper II
Helper II

You'll want something along the lines of this :

if Number.FromText(Text.AfterDelimiter([cr09e_column11],"/",1)) < 1953 then null else [cr09e_column11]

I got it to work as this:

if Number.FromText(Text.AfterDelimiter([cr09e_column11],"/",1)) <1753 then null else try Date.FromText([cr09e_column11]) otherwise null
 
I thought this would be easy to adapt for my next field, but I'm getting errors when testing another field. This one I am trying to make into a full date based on separate month/day/year columns:
if Number.FromText([cr09e_column15]) < 1753 then null else try Date.FromText(Text.Combine({[cr09e_column14],[cr09e_column13],[cr09e_column15]},"/")) otherwise null
 
I'm unclear why this errors if column 15 is null. Isn't null less than 1753?

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