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
UKNSI-Powerbi
Frequent Visitor

Table.RenameColumn from an index of another table

 Hi all,

 

I have an issue how to manage more dynamically columns heades in a table.

 

I have table 1 with current month, next month and next month +1. In a separate (table 2) I was able to select month I would like to appear. 

MONTH

Aug 2022
Sep 2022
Oct 2022

 

I would like to have table 1 as the following

Table1.CurrentMonth = Aug 2022 Demand

Table1.NextMonth = Sep 2022 Supply

Table1.NextMonth1 = Oct 2022 Textxyz

 

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @UKNSI-Powerbi 

 

Assume you only have the three months in the separate table 2, you can try my method shown as below. 

 

Table2: add a date type column and sort it ascendingly. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixNVzAyMDJSitWJVgpOLUBw/JNLoJxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MONTH = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"MONTH", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "MONTH", "Month Start"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Month Start", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Month Start", Order.Ascending}})
in
    #"Sorted Rows"

vjingzhang_0-1662097373517.png

 

Table1: get sorted MONTH column from Table2, add corresponding suffix strings to each month value, and rename selected columns. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI1MACRpiDSDMiOjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, CurrentMonth = _t, NextMonth = _t, NextMonth1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"CurrentMonth", Int64.Type}, {"NextMonth", Int64.Type}, {"NextMonth1", Int64.Type}}),
    #"Table2 Months" = Table2[MONTH],
    #"New Column Headers" = List.Transform(List.Zip({#"Table2 Months", {"Demand", "Supply", "Textxyz"}}), each Text.Combine(_, " ")),
    #"Rename Columns" = Table.RenameColumns(#"Changed Type", List.Zip({Table.ColumnNames(Table.SelectColumns(#"Changed Type", {"CurrentMonth", "NextMonth", "NextMonth1"})), #"New Column Headers"}))
in
    #"Rename Columns"

vjingzhang_1-1662097500295.png

 

Hope this helps. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

1 REPLY 1
v-jingzhang
Community Support
Community Support

Hi @UKNSI-Powerbi 

 

Assume you only have the three months in the separate table 2, you can try my method shown as below. 

 

Table2: add a date type column and sort it ascendingly. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcixNVzAyMDJSitWJVgpOLUBw/JNLoJxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MONTH = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"MONTH", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "MONTH", "Month Start"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Month Start", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Month Start", Order.Ascending}})
in
    #"Sorted Rows"

vjingzhang_0-1662097373517.png

 

Table1: get sorted MONTH column from Table2, add corresponding suffix strings to each month value, and rename selected columns. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI1MACRpiDSDMiOjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, CurrentMonth = _t, NextMonth = _t, NextMonth1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"CurrentMonth", Int64.Type}, {"NextMonth", Int64.Type}, {"NextMonth1", Int64.Type}}),
    #"Table2 Months" = Table2[MONTH],
    #"New Column Headers" = List.Transform(List.Zip({#"Table2 Months", {"Demand", "Supply", "Textxyz"}}), each Text.Combine(_, " ")),
    #"Rename Columns" = Table.RenameColumns(#"Changed Type", List.Zip({Table.ColumnNames(Table.SelectColumns(#"Changed Type", {"CurrentMonth", "NextMonth", "NextMonth1"})), #"New Column Headers"}))
in
    #"Rename Columns"

vjingzhang_1-1662097500295.png

 

Hope this helps. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

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