Power Query (Excel) convert date to mm/dd/yyyy The month and day has to be two characters.

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I have an excel Power query where I added a field where I wanted todays date. It works and produces todays date (8/18/2021). But where I am using the data it needs to be in a format where the month and day are both two characters (08/18/2021).

I am using :
Custom column formula
=DateTime.LocalNow()

how do I change this so I get the needed format? I am OK even if I need to create a second new field that converts this if needed. I just cant figure out any way to do this. Any help is very much appreciated.

= Table.AddColumn(#"Added Custom Resource", "Created", each DateTime.LocalNow())

Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I get this below, which makes me wonder if the formula returns the format defined by the operating system in the regional settings.

1629287626983.png

Why do you need it converted to that format mm/dd/yyyy inside PQ? This can be done when the data is loaded in Excel.

EDIT:
Power Query:
Date.ToText(date, "mm/dd/yyyy")
allows to force the formats, but the date is then text. Not sure you want that.
 
Last edited:
Upvote 0
Thanks.

The data/table that is created is moved to a Template that is used to upload information into another software program. Thats the program that needs the date in a specific format. I tried to change the format on the excel tab / table but that didn't work when I tried to push the data to the other Software.

just for clarity, where I created the new column I should change the formula from: = =DateTime.LocalNow() to: Date.ToText(date, "mm/dd/yyyy")

Sorry, relatively new to Excel Power Query.

I really appreciate your help
 
Upvote 0
I see, so the actual value matters, not simply the format. Seems then it must be text.
I meant like this
Power Query:
=Date.ToText(Date.From(DateTime.LocalNow()), "mm/dd/yyyy")
 
Upvote 0
Solution
= Table.AddColumn(#"Added Custom Resource", "Created", each Date.ToText(Date.From(DateTime.LocalNow()), "mm/dd/yyyy"))

In the applied steps (adding a new column under the formula, I added this: =Date.ToText(Date.From(DateTime.LocalNow()), "mm/dd/yyyy")

But this formula is resulting in a value of 00/18/2021 it should be 08/18/2021

Not sure what I did wrong.

Thanks
 
Upvote 0
OK

I changed it to:

= Table.AddColumn(#"Added Custom Resource", "Created", each Date.ToText(Date.From(DateTime.LocalNow()), "MM/dd/yyyy"))

capitalizing the MM made a difference

THANK YOU FOR YOUR HELP
 
Upvote 0
Most welcome. Thanks for the feedback. Never thought "MM" makes such difference. Learned from this too. ?
 
Upvote 0
Thanks for the Intel, that's good to know. Strangely enough, it seemed to work on my end with just mm not MM.
Need to look into it some more.
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,765
Members
449,121
Latest member
Vamshi 8143

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