1

I want to use Power Query to change a column or add a custom column that contain 2 different date formats individually to each format.

My column curently has dates in 2 formats: yyyymmdd and dd/mm/yyyy hh:mm:ss. I want to transfrom them so that the yyyymmdd will turn into ddmmmyy while the other form turn into mmmyy. It will still be a single column and the position of each row doesn't change. All are currently in text from

0

3 Answers 3

2

You should be able to adapt the below to your actual data:

The code below assumes your data comes from an Excel table. It should work whether your data is stored as numbers and formatted dates, or as text. And a simple change in the first lines can be made if the data source is a *.csv file.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

 #"Add Reformatted Dates" = Table.AddColumn(#"Source","Reformatted Dates", (r)=>
        [a=try Number.From(r[Dates]) otherwise DateTime.From(r[Dates],"en-150"),
         b=try Date.ToText(Date.From(a),"MMMyy")
            otherwise [yr=Number.IntegerDivide(a,10000),
                       m=Number.IntegerDivide(Number.Mod(a,10000),100),
                       d=Number.Mod(a,100),
                       dt=#date(yr,m,d),
                       out=Date.ToText(dt,"ddMMMyy")][out]
         
         ][b], type text)
in
    #"Add Reformatted Dates"

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You can try to use a custom text strings with this command :

Date.ToText( #date( yyyy, mm, dd ), [ Format = "ddmmmyy" ] )

This dashboard can help you for the format that you want !

Format  Description                 December 31, 2023          February 1, 2003
%d  Single Digit Day (1-31)         31                     1
dd  Double Digit Day (01-31)    31                     01
ddd Short Weekday Name          Sun                    Sat
dddd    Full Weekday Name           Sunday                     Saturday
%M  Single Digit Month (1-12)   12                     2
MM  Double Digit Month (01-12)  12                     02
MMM Short Month Name            Dec                    Feb
MMMM    Full Month Name                 December               February
%y  Year (0-99)                 23                     3
yy  Year (00-99)                    23                     03
yyy Year with at least three digits 2023                       2003
yyyy    Four-Digit Year                 2023                       2003
yyyyy   Five-Digit Year                 02023                      02003
m, M    Day followed by Full Month Name December 31            February 1
y, Y    Standard Long Date          December 2023              February 2003
d   Standard Short Date         12/31/2023             2/1/2003
D   Full Long Date                  Sunday, December 31, 2023  Saturday, February 1, 2003
%g, gg  The Period of an Era            A.D.                       A.D.

Comments

0

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

Power Query Screenshot

Excel

Excel Screenshot

5 Comments

FWIW, you can evaluate the efficiency of your algorithm by using the Diagnostic Tools in Power BI Desktop. There is a parameter named Exclusive Duration that can help with this.
Thanks for the info, but I think I've had enough of Power Query for a while. I tried your solution (it was way after midnight), and it gave me only half of the results. I tried now, and it works like a charm. Did adding "en-150" fix it, or was I too tired the first time?
Yes. Because I am in the US, Excel converted the base dates to MDY format and I had overlooked that. The culture argument makes the conversion locale-independent.
I spent some time on Culture and I meant to use en-US but the information I found seemed inconsistent to me. Is en-150 not the same as en-US, i.e., what would I need to use to make my solution locale-independent?
You use the culture of the source data. Since the data was specified as being in DMY format, you need to use a matching culture. en-150 is one of them. en-US should be used only if the source data is MDY format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.