Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Date formatting text to date

Hi All

 

Background:

I am importing a folder with multiple files with exactly the same data structure in each of them. Files have ".xlsx" format. In Power BI I do have a column called "Source.Name" which is the name of each file (012018_6120; 022018,6120, etc.). First two characters stand for a month number.

 

Need:

1. I would like to have first two characters from column "Source.Name" transformed into "MMM" format (Jan, Feb, Mar, Apr, etc.).

2. I would like to have this in a date format, because later I will leverage those months in some visualizations.

 

Summary:

Before: "012018_6120.xlsx"

After: "Jan" [date format]

 

thanks!

  • Here is how I would do this, all in Power Query before bringing it in to Power BI's data model:

    • Create a data table, and ensure it has a month column and a month name column. 

    It might look something like this in Power Query's advanced editor:

    let
        Source = {Number.From(#date(2019,1,1))..Number.From(#date(2019,12,31))},
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
        #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
        #"Inserted Month" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([Date]), Int64.Type),
        #"Inserted Month Name" = Table.AddColumn(#"Inserted Month", "Month Name", each Text.Start(Date.MonthName([Date]),3), type text)
    in
        #"Inserted Month Name"

    That last step will convert January to "Jan" for you, just showing 3 letters instead of the full month name.

    • In your data table, if "012018_6120.xlsx" is in the FileName field, use this in a new column:
      • Number.From(Text.Start([FileName],2))
      • Change that column type to an integer/whole number.
    • Import both the date table and the data table with file names into your model.
    • Mark the date table as a Dates Table, using the Date field.
    • Releate the Month field in the date table to the Month field in your Data table. It would look like this:

    For any visual or measures, use the Month field in the Dates table and it will automatically do any date intelligence correctly, and correctly pull related fields from your data table, which is Table1 in my example.

4 Replies

  • edhans's avatar
    edhans
    Icon for Community Champion rankCommunity Champion

    Here is how I would do this, all in Power Query before bringing it in to Power BI's data model:

    • Create a data table, and ensure it has a month column and a month name column. 

    It might look something like this in Power Query's advanced editor:

    let
        Source = {Number.From(#date(2019,1,1))..Number.From(#date(2019,12,31))},
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
        #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
        #"Inserted Month" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([Date]), Int64.Type),
        #"Inserted Month Name" = Table.AddColumn(#"Inserted Month", "Month Name", each Text.Start(Date.MonthName([Date]),3), type text)
    in
        #"Inserted Month Name"

    That last step will convert January to "Jan" for you, just showing 3 letters instead of the full month name.

    • In your data table, if "012018_6120.xlsx" is in the FileName field, use this in a new column:
      • Number.From(Text.Start([FileName],2))
      • Change that column type to an integer/whole number.
    • Import both the date table and the data table with file names into your model.
    • Mark the date table as a Dates Table, using the Date field.
    • Releate the Month field in the date table to the Month field in your Data table. It would look like this:

    For any visual or measures, use the Month field in the Dates table and it will automatically do any date intelligence correctly, and correctly pull related fields from your data table, which is Table1 in my example.

    • Anonymous's avatar
      Anonymous
      Not applicable

      it works - thank you edhans