cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

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
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

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

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors