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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Convert text field that may have a date to a date field

I have data that is text that I want to convert to a date field. The text usually has a date that can be written in different formats. E.g., 3/4/20 could be written as 3/4, 03/04, 03/4/2020, etc. There is sometimes random text before and after the text. I want to convert this text field so that it is displayed just as a date.

 

The below screen shot shows some examples. The custom column (last column) is the final 'correct' value...but I manually typed these as an example.  The columns that are yellow are columns I added.

 

I think the final step is to evaluate all three yellow columns for dates and pick the first column that is a "valid" date.

 

Any ideas?

 

date clean.png

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

You can create one measure as below:

  1. Get the number of start position of text “/”using SEARCH function
  2. Get the date using MID function

 

Date = var firstSlashL= search("/",FIRSTNONBLANK('ConvertText'[Clean Date],0)) 
var isNumberP1=MID(FIRSTNONBLANK('ConvertText'[Clean Date],0),firstSlashL-1,1)
var secondSlashL=IFERROR(SEARCH("@",SUBSTITUTE(FIRSTNONBLANK('ConvertText'[Clean Date],0),"/","@",2),1),0)
var fdate=if(ISNUMBER(IFERROR(value(isNumberP1),isNumberP1)),MID(FIRSTNONBLANK('ConvertText'[Clean Date],0),firstSlashL-1,5),MID(FIRSTNONBLANK('ConvertText'[Clean Date],0),secondSlashL-1,5)) 
return trim(fdate)

 

convertText.JPG

 

The above formula is written based on the sample data you provided , and may not be fully applicable to your scenario. Later you may need to adjust this according to your situation.

 

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

If your problem has been resolved, could you please mark it as Answered? So it will help others if they face the similar problem with you. Thank you.

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

You can create one measure as below:

  1. Get the number of start position of text “/”using SEARCH function
  2. Get the date using MID function

 

Date = var firstSlashL= search("/",FIRSTNONBLANK('ConvertText'[Clean Date],0)) 
var isNumberP1=MID(FIRSTNONBLANK('ConvertText'[Clean Date],0),firstSlashL-1,1)
var secondSlashL=IFERROR(SEARCH("@",SUBSTITUTE(FIRSTNONBLANK('ConvertText'[Clean Date],0),"/","@",2),1),0)
var fdate=if(ISNUMBER(IFERROR(value(isNumberP1),isNumberP1)),MID(FIRSTNONBLANK('ConvertText'[Clean Date],0),firstSlashL-1,5),MID(FIRSTNONBLANK('ConvertText'[Clean Date],0),secondSlashL-1,5)) 
return trim(fdate)

 

convertText.JPG

 

The above formula is written based on the sample data you provided , and may not be fully applicable to your scenario. Later you may need to adjust this according to your situation.

 

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

Well, use SEARCH to search for "/". Subtract 2 from that value and grab to the LEN of the string. Then you should be able to convert it to a Date.

 

Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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