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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors