Power Query - change Text to Date

illusionek

Board Regular
Joined
Jun 21, 2014
Messages
104
Hello

I import & consolidate multiple files from the same folder using Power Query. The file name is "XXX 210618.xlsx" and I use below code to extract date from the name of the file when importing data.

Code:
Text.Replace(Text.End([Source.Name],11),".xlsx","")


My issue is that once imported 210618 is seen as text value if I change the data type to Date, I get below error. I have also tried various functions like Date.FromText but still come across the same error message.

Any suggestions?


Many thanks.

Code:
DataFormat.Error: We couldn't parse the input provided as a Date value.
Details:
    210618
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try something like this:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ConvertToTextDate = Table.AddColumn(Source, "DateText", each "20"&Text.Replace(Text.End([Name],11),".xlsx","")),
    ConvertToDate = Table.TransformColumnTypes(ConvertToTextDate,{{"DateText", type date}})
in
    ConvertToDate

Is that something you can work with?
 
Upvote 0
Well....I mucked up my first response. I didn't notice that the YEAR is the last 2 digits (Not the first 2 digits)

Try something like this:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ConvertToTextDate = Table.AddColumn(Source, "DateText", 
        each "20"
        &Text.Start(Text.Replace(Text.End([Name],11),".xlsx",""),2)
        &Text.Range(Text.Replace(Text.End([Name],11),".xlsx",""),2,2)
        &Text.End  (Text.Replace(Text.End([Name],11),".xlsx",""),2)),
    ConvertToDate = Table.TransformColumnTypes(ConvertToTextDate,{{"DateText", type date}})
in
    ConvertToDate

Better?
 
Last edited:
Upvote 0
(Sigh) I'll go take a nap after this post....
I *still* got the order incorrect.
This is the last in a series of final solutions:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    ConvertToTextDate = Table.AddColumn(Source, "DateText", 
        each "20"
        &Text.End(Text.Replace(Text.End([Name],11),".xlsx",""),2)
        &Text.Range(Text.Replace(Text.End([Name],11),".xlsx",""),2,2)
        &Text.Start(Text.Replace(Text.End([Name],11),".xlsx",""),2)
),
    ConvertToDate = Table.TransformColumnTypes(ConvertToTextDate,{{"DateText", type date}})
in
    ConvertToDate
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,222
Members
448,877
Latest member
gb24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top