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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Datazen
Helper I
Helper I

Error when trying to convert a date to a month name

I have a date field named 'Date Full'. I'm trying to create a field that displays the month name from the 'Date_Full' but I'm getting an error. The M Code for the step is this:

 

= Table.AddColumn(#"Removed Errors", "Month_Name", each Date.MonthName(#"Removed Errors",[Date_Full]))

 

The error is this:

Expression.Error: We cannot convert the value #date(2019, 3, 28) to type Text.
Details:
Value=3/28/2019
Type=Type

 

Any help on the error is appreciated.

 

The larger script is pulling 2019 baseball stats for Kevin Pillar from an ESPN data table. the date comes as just 'MMM DD' so I am then appending this with '2019' and then successfully turning this into a date field (this is 'Date _Full'). Then I run into this problem when trying to create a new field named 'Month_Name'. Here is the full M code so far. 'Month_Name' returns errors for each row and this is the last step shown:  

 

let
Source = Web.Page(Web.Contents("https://www.mlb.com/player/kevin-pillar-607680?stats=gamelogs-r-hitting-mlb&year=2019")),
Data6 = Source{6}[Data],
#"Changed Type" = Table.TransformColumnTypes(Data6,{{"Date", type text}, {"Team", type text}, {"OPP", type text}, {"AB", Int64.Type}, {"R", Int64.Type}, {"H", Int64.Type}, {"TB", Int64.Type}, {"2B", Int64.Type}, {"3B", Int64.Type}, {"HR", Int64.Type}, {"RBI", Int64.Type}, {"BB", Int64.Type}, {"IBB", Int64.Type}, {"SO", Int64.Type}, {"SB", Int64.Type}, {"CS", Int64.Type}, {"AVG", type number}, {"OBP", type number}, {"SLG", type number}, {"HBP", Int64.Type}, {"SAC", Int64.Type}, {"SF", Int64.Type}, {"sort", Int64.Type}}),
#"Added Custom Column" = Table.AddColumn(#"Changed Type", "Custom", each Text.Combine({Date.ToText(Date.From([Date]), "%M"), "/", Date.ToText(Date.From([Date]), "%d"), "/", Date.ToText(Date.From([Date]), "yy"), "19"}), type text),
#"Renamed Columns" = Table.RenameColumns(#"Added Custom Column",{{"Custom", "Date_Full"}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date_Full", type date}}),
#"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type1", {"Date_Full"}),
#"Added Custom" = Table.AddColumn(#"Removed Errors", "Month_Name", each Date.MonthName(#"Removed Errors",[Date_Full]))
in
#"Added Custom"

  

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi the first argument of the Date.MonthName-function has to be of type date. In your case, you're referencing a table. The second argument is optional, but expects a text-string. That's what triggers the current error-message, because you've referenced a date-value there.

So simply using it like so should do the trick:

 

Table.AddColumn(#"Removed Errors", "Month_Name", each Date.MonthName([Date_Full]))

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Super User
Super User

Hi the first argument of the Date.MonthName-function has to be of type date. In your case, you're referencing a table. The second argument is optional, but expects a text-string. That's what triggers the current error-message, because you've referenced a date-value there.

So simply using it like so should do the trick:

 

Table.AddColumn(#"Removed Errors", "Month_Name", each Date.MonthName([Date_Full]))

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Ah! Thank you! I'm not sure how I missed that! It's working now. 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.