Convert Date Text to Date Text with Different Format
- This works for me. It required searching through many webpages. I'm not complaining, but emphasizing how much of a newb I am. There may be better methods, but I hope this helps you meet your requirements.
Sample Data in .csv File
| Dates |
| yyyymmdd |
| 20251019 |
| 20251013 |
| 20240505 |
| dd/mm/yyyy hh:mm:ss |
| 14/03/2022 05:12:33 |
| 23/06/2025 12:45:22 |
| 24/02/2023 14:23:11 |
Expected Result
| Dates |
Initial |
|
yyyymmdd |
| 19-Oct-25 |
20251019 |
| 13-Oct-25 |
20251013 |
| 05-May-24 |
20240505 |
|
dd/mm/yyyy hh:mm:ss |
| Mar-22 |
14/03/2022 05:12:33 |
| Jun-25 |
23/06/2025 12:45:22 |
| Feb-23 |
24/02/2023 14:23:11 |
M-Code
let
// setup (hardly relevant)
Source = Csv.Document(File.Contents("C:\Test\Sample.csv"),[Delimiter=",", Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Removed Bottom Rows" = Table.RemoveLastN(#"Promoted Headers",1),
#"Duplicated Column" = Table.DuplicateColumn(#"Removed Bottom Rows", "Dates", "Dates - Copy"),
#"Renamed Dupe Column" = Table.RenameColumns(#"Duplicated Column",{{"Dates - Copy", "Initial"}}),
// Transform Dates
// Format1 = "yyyymmdd" to "ddmmmyy"
// Format2 = "dd/mm/yyyy hh:mm:ss" to "mmmyy" ' the time is irrelevant in this solution
// 'Test.Range' retrieves the two digits for each date part which is enough for the day and year.
// To get the month in 'MMM' format, the digits are converted to a number using 'Number.FromText'.
// which is fed into '#date' which is fed into 'Date.ToText'.
// Define constants.
DateDelimiter = "-", // looks nicer with a dash (you decide)
MonthFormat = "MMM",
// Replace "Renamed Dupe Column" with your previous step, and "Dates" with your column title.
#"Transformed Dates" = Table.TransformColumns(#"Renamed Dupe Column",
{"Dates",
each if Text.Length(_) = 8 then Text.Combine({
Text.Range(_,6,2) & DateDelimiter,
Date.ToText(#date(1,Number.FromText(Text.Range(_,4,2)),1),MonthFormat) & DateDelimiter,
Text.Range(_,2,2)})
else if Text.Length(_) = 19 then Text.Combine({
Date.ToText(#date(1,Number.FromText(Text.Range(_,3,2)),1),MonthFormat) & DateDelimiter,
Text.Range(_,8,2)})
else _
})
// Don't forget the trailing comma above if it's not the last step!
// hardly relevant
in
#"Transformed Dates"
Power Query

Excel
