Forum Discussion

kb177's avatar
kb177
Icon for Helper II rankHelper II
2 years ago
Solved

rename column name

I want to rename column names after the first column to a default value

 

eg first column should be named Default column then all the subsequent columns and when new columns get added to base data they should be renamed ad subsequent column1, 2, 3 etc.

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi kb177 ,

     

    Here I create a sample to have a test.

    Name Table:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcjJCQAgDETRXnJWcInb0Tok/bdhhsnh8+G9JzdXSXJLwY5XxRK4kbHtteBOxpbXg5WMTU+DBxljZh8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Department = _t, Employee = _t, Score = _t, Ranking = _t]),
        #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Department", type text}, {"Employee", type text}, {"Score", Int64.Type}, {"Ranking", Int64.Type}}),
        #"Column Name" = Table.ColumnNames(#"Changed Type1"),
        #"Converted to Table" = Table.FromList(#"Column Name", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "From"}}),
        #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "To", each if [Index] = 0 then "Default" else "Subsequent Column"&" "&Number.ToText([Index])),
        #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"To", type text}, {"From", type text}})
    in
        #"Changed Type2"

    Data Table:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcjJCQAgDETRXnJWcInb0Tok/bdhhsnh8+G9JzdXSXJLwY5XxRK4kbHtteBOxpbXg5WMTU+DBxljZh8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Department = _t, Employee = _t, Score = _t, Ranking = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Department", type text}, {"Employee", type text}, {"Score", Int64.Type}, {"Ranking", Int64.Type}}),
        #"Column Name" = Table.ColumnNames(#"Changed Type"),
        #"Converted to Table" = Table.FromList(#"Column Name", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Merged Queries" = Table.NestedJoin(#"Converted to Table", {"Column1"}, #"Name Table", {"From"}, "Query1", JoinKind.LeftOuter),
        #"Expanded Query1" = Table.ExpandTableColumn(#"Merged Queries", "Query1", {"To"}, {"To"}),
        #"Merged Columns" = Table.CombineColumns(#"Expanded Query1",{"Column1", "To"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
        #"Added Custom" = Table.AddColumn(#"Merged Columns", "Custom", each Text.Split([Merged], ",")),
        NewColumnNames = #"Added Custom"[Custom],
        #"New Table"= Table.RenameColumns(Source, NewColumnNames)
    in
        #"New Table"

    Default Table:

    New Table:

    Everytime you add a new column, it will show new column name dynamicly.

    For reference:Rename column names in a dynamic way with #Excel #PowerQuery | wmfexcel

     

    Best Regards,
    Rico Zhou

     

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

     

     

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi kb177 ,

    In Power Query editor, 1st column has position 0, 2nd column has position 1 and so on.


    You can use the below code to rename a column by its position to a default value
    = Table.RenameColumns(#"Promoted Headers",{{Table.ColumnNames(#"Promoted Headers"){0}, "Default"}})
    This code will rename the first column to "Default" irrespective of its original name.

     

    Hope this helps.

    Please accept the solution if this answers your query.

     

    Thanks,

    Avantika Thakur

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi kb177 ,

     

    Here I create a sample to have a test.

    Name Table:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcjJCQAgDETRXnJWcInb0Tok/bdhhsnh8+G9JzdXSXJLwY5XxRK4kbHtteBOxpbXg5WMTU+DBxljZh8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Department = _t, Employee = _t, Score = _t, Ranking = _t]),
        #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Department", type text}, {"Employee", type text}, {"Score", Int64.Type}, {"Ranking", Int64.Type}}),
        #"Column Name" = Table.ColumnNames(#"Changed Type1"),
        #"Converted to Table" = Table.FromList(#"Column Name", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "From"}}),
        #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "To", each if [Index] = 0 then "Default" else "Subsequent Column"&" "&Number.ToText([Index])),
        #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"To", type text}, {"From", type text}})
    in
        #"Changed Type2"

    Data Table:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcjJCQAgDETRXnJWcInb0Tok/bdhhsnh8+G9JzdXSXJLwY5XxRK4kbHtteBOxpbXg5WMTU+DBxljZh8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Department = _t, Employee = _t, Score = _t, Ranking = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Department", type text}, {"Employee", type text}, {"Score", Int64.Type}, {"Ranking", Int64.Type}}),
        #"Column Name" = Table.ColumnNames(#"Changed Type"),
        #"Converted to Table" = Table.FromList(#"Column Name", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Merged Queries" = Table.NestedJoin(#"Converted to Table", {"Column1"}, #"Name Table", {"From"}, "Query1", JoinKind.LeftOuter),
        #"Expanded Query1" = Table.ExpandTableColumn(#"Merged Queries", "Query1", {"To"}, {"To"}),
        #"Merged Columns" = Table.CombineColumns(#"Expanded Query1",{"Column1", "To"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
        #"Added Custom" = Table.AddColumn(#"Merged Columns", "Custom", each Text.Split([Merged], ",")),
        NewColumnNames = #"Added Custom"[Custom],
        #"New Table"= Table.RenameColumns(Source, NewColumnNames)
    in
        #"New Table"

    Default Table:

    New Table:

    Everytime you add a new column, it will show new column name dynamicly.

    For reference:Rename column names in a dynamic way with #Excel #PowerQuery | wmfexcel

     

    Best Regards,
    Rico Zhou

     

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