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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors