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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Syndicate_Admin
Administrator
Administrator

How To Dynamic Rename Column Headers: [Month] & [Current Year] in P. Query

I am new beginner of Power Query. My current raw data (1st screenshot below) consists of column headers (Column B - M) without [Year Name]. 

 
Column AColumn BColumn CColumn DColumn EColumn FColumn GColumn HColumn IColumn JColumn KColumn LColumn M
 JanFebMarAprMayJuneJuly AugSeptOctNovDec
Sales100100100100100100100100100100100100
Expense100100100100100100100100100100100100

 

 

I manually added in [2022] in Query Editor for all months (Jan - Dec). However, when come to next year, i will have to do the similiar manual exercise to replace col. header with [2023], which bit inefficient. 

 

 

 

Table.RenameColumns(#"Removed Columns2",{"Jan", "Jan 2022"}, {"Feb", "Feb 2022"}, {"Mar", "Mar 2022}, {"Apr", "Apr 2022"}, {"May", "May 2022", {"June", "Jun 2022"}, {"Jul", "Jul 2022"}, {"Aug", "Aug 2022"}, {"Sep", "Sep 2022"}, {"Oct", "Oct 2021"}, {"Nov", "Nov 2022"}, {"Dec", "Dec 2022"}})

 

 

 

Wonder if any efficient way to write in M language that can auto rename "Year" In Column header, base on input in Excel cell below? 

 

let's assume i key in "2023" in Excel Cell A1 in another worktab within same workbook. 

Year
2023
 

Table.RenameColumns(#"Removed Columns2",{{{"Jan", "Jan 2022"}, {"Feb", "Feb 2022"}, {"Mar", "Mar 2022"}, {"Apr", "Apr 2022"}, {"May", "May 2022"}, {"Jun", "Jun 2022"}, {"Jul", "Jul 2022"}, {"Aug", "Aug 2022}, {"Sep", "Sep 2022"}, {"Oct", "Oct 2022"}, {"Nov", "Nov 2022"}, {"Dec", "Dec 2022"}})

 

wonder if any better way which i can dynamic col. header name in P.Query Editor, base on [Year] selection from excel cell A1, and concentrate with [Month]? 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @Syndicate_Admin ,

According to your description, here's my solution. Add the step in Advanced Editor:

#"Rename"=let 
_Year=Text.From(Date.Year(Date.From(DateTime.LocalNow()))) 
in 
Table.RenameColumns(#"Removed Columns2",{{"Jan", "Jan "&_Year}, {"Feb", "Feb "&_Year}, {"Mar", "Mar "&_Year}, {"Apr", "Apr "&_Year}, {"May", "May "&_Year}, {"Jun", "Jun "&_Year}, {"Jul", "Jul "&_Year}, {"Aug", "Aug "&_Year}, {"Sep", "Sep "&_Year}, {"Oct", "Oct "&_Year}, {"Nov", "Nov "&_Year}, {"Dec", "Dec "&_Year}})

I define a variable _Year in the code, which will automatically get the current year. When year 2023 comes, it will automatically changes to 2023 without enter in the Excel.

Here's the whole M syntax, you can copy-paste in a blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiUyA2A2JzILYAYksgNjQAESCVhkZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Jan = _t, Feb = _t, Mar = _t, Apr = _t, May = _t, Jun = _t, Jul = _t, Aug = _t, Sep = _t, Oct = _t, Nov = _t, Dec = _t]),
    #"Removed Columns2" = Table.TransformColumnTypes(Source,{{"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}, {"Apr", Int64.Type}, {"May", Int64.Type}, {"Jun", Int64.Type}, {"Jul", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}),
    #"Rename"=let _Year=Text.From(Date.Year(Date.From(DateTime.LocalNow()))) in Table.RenameColumns(#"Removed Columns2",{{"Jan", "Jan "&_Year}, {"Feb", "Feb "&_Year}, {"Mar", "Mar "&_Year}, {"Apr", "Apr "&_Year}, {"May", "May "&_Year}, {"Jun", "Jun "&_Year}, {"Jul", "Jul "&_Year}, {"Aug", "Aug "&_Year}, {"Sep", "Sep "&_Year}, {"Oct", "Oct "&_Year}, {"Nov", "Nov "&_Year}, {"Dec", "Dec "&_Year}})
in
    #"Rename"

Result:

vkalyjmsft_0-1668060111325.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yanjiang-msft
Community Support
Community Support

Hi @Syndicate_Admin ,

According to your description, here's my solution. Add the step in Advanced Editor:

#"Rename"=let 
_Year=Text.From(Date.Year(Date.From(DateTime.LocalNow()))) 
in 
Table.RenameColumns(#"Removed Columns2",{{"Jan", "Jan "&_Year}, {"Feb", "Feb "&_Year}, {"Mar", "Mar "&_Year}, {"Apr", "Apr "&_Year}, {"May", "May "&_Year}, {"Jun", "Jun "&_Year}, {"Jul", "Jul "&_Year}, {"Aug", "Aug "&_Year}, {"Sep", "Sep "&_Year}, {"Oct", "Oct "&_Year}, {"Nov", "Nov "&_Year}, {"Dec", "Dec "&_Year}})

I define a variable _Year in the code, which will automatically get the current year. When year 2023 comes, it will automatically changes to 2023 without enter in the Excel.

Here's the whole M syntax, you can copy-paste in a blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiUyA2A2JzILYAYksgNjQAESCVhkZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Jan = _t, Feb = _t, Mar = _t, Apr = _t, May = _t, Jun = _t, Jul = _t, Aug = _t, Sep = _t, Oct = _t, Nov = _t, Dec = _t]),
    #"Removed Columns2" = Table.TransformColumnTypes(Source,{{"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}, {"Apr", Int64.Type}, {"May", Int64.Type}, {"Jun", Int64.Type}, {"Jul", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}),
    #"Rename"=let _Year=Text.From(Date.Year(Date.From(DateTime.LocalNow()))) in Table.RenameColumns(#"Removed Columns2",{{"Jan", "Jan "&_Year}, {"Feb", "Feb "&_Year}, {"Mar", "Mar "&_Year}, {"Apr", "Apr "&_Year}, {"May", "May "&_Year}, {"Jun", "Jun "&_Year}, {"Jul", "Jul "&_Year}, {"Aug", "Aug "&_Year}, {"Sep", "Sep "&_Year}, {"Oct", "Oct "&_Year}, {"Nov", "Nov "&_Year}, {"Dec", "Dec "&_Year}})
in
    #"Rename"

Result:

vkalyjmsft_0-1668060111325.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors