cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
vsurge33
Frequent Visitor

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 II
Super User II

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 II
Super User II

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.

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 II
Super User II

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

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
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Get Ready for Power BI Dev Camp

Power BI Dev Camp - June 24th

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors