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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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. 

 

vsurge33_0-1617219068314.png

 

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:

vsurge33_1-1617219365577.png

 

Any input would be appreciated! 

 

 

1 ACCEPTED SOLUTION
HotChilli
Super User
Super User

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 ()))))

View solution in original post

4 REPLIES 4
HotChilli
Super User
Super User

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
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. 

HotChilli
Super User
Super User

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
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?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors