Forum Discussion

o59393's avatar
o59393
Icon for Post Prodigy rankPost Prodigy
6 years ago
Solved

transform a year into a date with power query editor

Hi all

 

I have a CSV with targets for different locations per year 2019 and 2020. To make it easier that csv contains only the column for year, since the target wont change per month.

 

How can I transform that single year column into a date with PBI's power query?

 

In other words go from 

 

 

to 

 

 

thanks!

  • Anonymous's avatar
    Anonymous
    6 years ago

    You could try this:   

    Add a column to generate a list of month numbers:

    = Table.AddColumn(#"Changed Type", "Month List", each List.Numbers (1, 12))

     

    Choose "Expand to New Rows" to expand the list.

    Add a new column to combine the Year value with the month number value.  I used the "Column from Examples" feature with the Year and Month List columns selected.  Power Query inserted the following code:

    = Table.AddColumn(#"Expanded Month List", "Month", each Text.Combine({Text.From([Month List], "en-US"), "/1/", Text.From([Year], "en-US")}), type text)

     

    Finally, transform the Date column from text to date.  

13 Replies

  • o59393's avatar
    o59393
    Icon for Post Prodigy rankPost Prodigy

    Hi all

     

    I have a CSV with targets for different locations per year 2019 and 2020. To make it easier that csv contains only the column for year, since the target wont change per month.

     

    How can I transform that single year column into a date with PBI's power query?

     

    In other words go from 

     

     

    to 

     

     

    thanks!

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

    You use the #date() function, so #date([Year],1,1) will convert the integer in the [Year] field to Jan 1, 2019. But you didn't say how you expected it to do other days. You can put math in the month and day fields. The #date() function is identical to the DATE() function in Excel - #date(year,month,day)

      • Anonymous's avatar
        Anonymous
        Not applicable

        You could try this:   

        Add a column to generate a list of month numbers:

        = Table.AddColumn(#"Changed Type", "Month List", each List.Numbers (1, 12))

         

        Choose "Expand to New Rows" to expand the list.

        Add a new column to combine the Year value with the month number value.  I used the "Column from Examples" feature with the Year and Month List columns selected.  Power Query inserted the following code:

        = Table.AddColumn(#"Expanded Month List", "Month", each Text.Combine({Text.From([Month List], "en-US"), "/1/", Text.From([Year], "en-US")}), type text)

         

        Finally, transform the Date column from text to date.