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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
isa2003
Frequent Visitor

Extracting multiple date formats from text column in Power query

Hello:

 

I have a text column within which the date can be found. However, the format of the date varies from yyyy-MM-dd, yyyy/MM/d, dd/MM/yyyy, MM-yyyy, yyyy etc. How can only the date be extracted from the column into a new column?  I am interested only in the year.  e.g. 
Liz Coleman Online 12/12/2017 Notebooks Pencils
Candy Shanner-Mail 09/2009- Folders

 

Thanks in advance. 

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

You split the date string by various delimiters, and select the one with four digits: (if you have additional delimiters, edit the Text.SplitAny function accordingly.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1MNU1MFKK1YFw9Q1M9SE8AyMwGygG4ZrqwtkQRiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    
//Type "Date" as text
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}}),

//year will be the segment with four digits
    #"Add Year" = Table.AddColumn(#"Changed Type","Year",(r)=>
        List.Select(Text.SplitAny(r[Date],"-/"), each Text.Length(_)=4){0}, Int64.Type)
in
    #"Add Year"

 

ronrsnfld_0-1715730996072.png

 

 

View solution in original post

1 REPLY 1
ronrsnfld
Super User
Super User

You split the date string by various delimiters, and select the one with four digits: (if you have additional delimiters, edit the Text.SplitAny function accordingly.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1MNU1MFKK1YFw9Q1M9SE8AyMwGygG4ZrqwtkQRiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    
//Type "Date" as text
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}}),

//year will be the segment with four digits
    #"Add Year" = Table.AddColumn(#"Changed Type","Year",(r)=>
        List.Select(Text.SplitAny(r[Date],"-/"), each Text.Length(_)=4){0}, Int64.Type)
in
    #"Add Year"

 

ronrsnfld_0-1715730996072.png

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.