Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Rename columns in Power Query based on current year and future years

Hello, 

I am newer to PBI / Power Query, and new to this community (this is my very first post, so please excuse if this is not the right forum or if this has already been answered).

 

I am trying to have my column name(s) be dynamically updated based on the current year. For instance, rather than have the title "Current Year" below, I would like for it to say 2021. 

 

 

I created a query for today's date:

= DateTime.Date (DateTime.LocalNow())

 

But when I try and reference that query

#"Renamed Columns" = Table.RenameColumns(#"Added Custom1",{{"Current Year", Date.Year(DateToday) }})

 

I get the following error:

 

Any input would be appreciated! 

 

 

  • I think you can use a different function:

     

    Table.TransformColumnNames(#"Added Custom1", 
    (colName as text) as text => Text.Replace(colName, "Current Year", Text.From(Date.Year(DateTime.LocalNow ()))))

4 Replies

  • HotChilli's avatar
    HotChilli
    Community Champion

    I think you can use a different function:

     

    Table.TransformColumnNames(#"Added Custom1", 
    (colName as text) as text => Text.Replace(colName, "Current Year", Text.From(Date.Year(DateTime.LocalNow ()))))
    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you, HotChilli! One other question. How would that function above be modified if I wanted to have another column for the CurrentYear + 1 (i.e. 2022) and so forth?

  • HotChilli's avatar
    HotChilli
    Community Champion

    I may be a little cruel but I think you have to have a go first.

     

    Also be careful with this strategy.  You might think you want column headings with different years but this isn't usually a good data storage idea in powerbi.  You can always use a matrix visual to display different years.

    There are exceptions, of course.

    • Anonymous's avatar
      Anonymous
      Not applicable

      I'm also not convinced that this is dynamic column titles in Power Query is indeed an ideal strategy.

       

      What I was originally looking to accomplish is to build a table in Power BI that has some static columns defining a given product, and then some dynamic columns (2021, 2022, etc.) that show the sales for that product in a given year. I can't dynamically name a measure to be based on the current year, so was thinking of doing something in Power Query.

       

      Anyhow, thanks for the tips.