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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
Anonymous
Not applicable

in PowerQueryEditor, Convert DateTime to YYYMM

I am reading a table from SQL Server and I need to convert a datetime column to YYYYMM. or YYYY-MM.  Intellisense is not helping much.  My last failed attempt was this:

 

= Table.AddColumn(dbo_OLAPJobHistory, "dtRunMonth", format('dbo_OLAPJobHistory'[dtRunDate],"YYYYMM"))

 

Help?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Unfortunately, this leaves  you with a text field.  If you want it to be useable as a date:

From the Power Query Editor, add a new column as beginning of month:

Table.AddColumn(#"Filtered Rows", "dtStartOfMonth", each Date.StartOfMonth([dtRunDate]), type datetime)

From the Data View, format the date using yyyy-mm:

dtRunMonth = FORMAT(OLAPJobHistory[dtStartOfMonth],"yyyy-mm")

 

View solution in original post

6 REPLIES 6
bcdobbs
Community Champion
Community Champion

I think you want date.totext

 

syntax in:

 

https://docs.microsoft.com/en-us/powerquery-m/date-totext

 

Date.ToText([column], "yyyyMM")



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
Anonymous
Not applicable

That makes sense, except I am having trouble with the addcolumn syntax.  I stepped thru the "Custom Column" in the Power Query Editor and it built this, which throws an error:

 

= Table.AddColumn(dbo_OLAPJobHistory, "dtRunMonth", each DateTime.ToText(dbo_OLAPJobHistory[dtRunDate],"YYYYMM"))

 

What am I missing? 

 

I'm not by a computer but I think your problem is power query doesn't want the table reference in front of the colum (that's dax syntax).

 

try 

 

[dtRunDate] instead of dbo_OLAPJobHistory[dtRunDate]



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
Anonymous
Not applicable

This worked for me:

= Table.AddColumn(dbo_OLAPJobHistory, "dtRunMonth", each DateTime.ToText([dtRunDate],"yyyyMM"))

 

Thank you

Anonymous
Not applicable

Unfortunately, this leaves  you with a text field.  If you want it to be useable as a date:

From the Power Query Editor, add a new column as beginning of month:

Table.AddColumn(#"Filtered Rows", "dtStartOfMonth", each Date.StartOfMonth([dtRunDate]), type datetime)

From the Data View, format the date using yyyy-mm:

dtRunMonth = FORMAT(OLAPJobHistory[dtStartOfMonth],"yyyy-mm")

 

Missed that you said datetime in question. Glad it's working.



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

Helpful resources

Announcements
Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.