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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

How to make column numbering not consecutive over the whole data set but just for equal column names

Hello everyone,

 

I am loading data into my Power Bi file from a folder, using one of the csv files from that folder as example file.

There are multiple duplicated column names in my data, they get renamed as follows:

 

Let's say I have 3 columns named "helpercolumn" and 3 colums named "datacolumn". Automatically they get renamed from

  • helpercolumn, helpercolumn, helpercolumn, datacolumn, datacolumn, datacolumn

to:

  • helpercolumn, helpercolumn_1, helpercolumn_2,
    datacolumn, datacolumn_3, datacolumn_4

 

But I want them to be:

  • helpercolumn, helpercolumn_1, helpercolumn_2,
    datacolumn, datacolumn_1, datacolumn_2

 

I don't know where the renaming happens, maybe that would be a hint. For me it appears to happen automatically.
Where does that happen and/or how can I change that to the way I want it to be?

Thank you!

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

Hi @Anonymous ,

Pls refer the following :

import data:

vluwangmsft_0-1652779013310.png

After transform:

vluwangmsft_1-1652779036115.png

 

 

let
    Source = Excel.Workbook(File.Contents("\\filelink\importtest.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Added Index1" = Table.AddIndexColumn(Sheet1_Sheet, "IndexALL", 1, 1, Int64.Type),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Added Index1", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6"}, "Attribute", "Value"),
    #"Added Index" = Table.AddIndexColumn(#"Unpivoted Only Selected Columns", "Index", 1, 1, Int64.Type),
    #"Grouped Rows" = Table.Group(#"Added Index", {"Value"}, {{"all", each _, type table [Attribute=text, Value=any, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([all],"Index1",1)),
    #"Expanded Custom1" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"IndexALL", "Attribute", "Value", "Index", "Index1"}, {"Custom.IndexALL", "Custom.Attribute", "Custom.Value", "Custom.Index", "Custom.Index1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom1",{"all", "Custom.Value", "Custom.Index"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns", "newvalue", each if Value.Is([Value],type text)=true then [Value]&"_"&Number.ToText([Custom.Index1]) else [Value]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Value", "Custom.Index1"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns1", each true),
    #"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Custom.Attribute]), "Custom.Attribute", "newvalue"),
    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Custom.IndexALL"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns2", [PromoteAllScalars=true])
in
    #"Promoted Headers"

 

 

And if you need to add column like the below ,you only need to add in one step:

 

vluwangmsft_2-1652779196649.png

vluwangmsft_3-1652779460426.pngvluwangmsft_4-1652779469420.png

 

 

Adjust the code according your need!

Did I answer your question? Mark my post as a solution!


Best Regards

Lucien

 

View solution in original post

4 REPLIES 4
v-luwang-msft
Community Support
Community Support

Hi @Anonymous ,

Pls refer the following :

import data:

vluwangmsft_0-1652779013310.png

After transform:

vluwangmsft_1-1652779036115.png

 

 

let
    Source = Excel.Workbook(File.Contents("\\filelink\importtest.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Added Index1" = Table.AddIndexColumn(Sheet1_Sheet, "IndexALL", 1, 1, Int64.Type),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Added Index1", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6"}, "Attribute", "Value"),
    #"Added Index" = Table.AddIndexColumn(#"Unpivoted Only Selected Columns", "Index", 1, 1, Int64.Type),
    #"Grouped Rows" = Table.Group(#"Added Index", {"Value"}, {{"all", each _, type table [Attribute=text, Value=any, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([all],"Index1",1)),
    #"Expanded Custom1" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"IndexALL", "Attribute", "Value", "Index", "Index1"}, {"Custom.IndexALL", "Custom.Attribute", "Custom.Value", "Custom.Index", "Custom.Index1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom1",{"all", "Custom.Value", "Custom.Index"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns", "newvalue", each if Value.Is([Value],type text)=true then [Value]&"_"&Number.ToText([Custom.Index1]) else [Value]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Value", "Custom.Index1"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns1", each true),
    #"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Custom.Attribute]), "Custom.Attribute", "newvalue"),
    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Custom.IndexALL"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns2", [PromoteAllScalars=true])
in
    #"Promoted Headers"

 

 

And if you need to add column like the below ,you only need to add in one step:

 

vluwangmsft_2-1652779196649.png

vluwangmsft_3-1652779460426.pngvluwangmsft_4-1652779469420.png

 

 

Adjust the code according your need!

Did I answer your question? Mark my post as a solution!


Best Regards

Lucien

 

Anonymous
Not applicable

Thank you very much, that does solve my problem.

It took a bit to understand what happend, some serious braing gymnastics ^^
Thank you!

Anonymous
Not applicable

Thank you for your answer.
Yes, that's how I do that currently. But it being hundreds of names to change every time makes me wonder if there is an easier, an automated way.

Kind regards

speedramps
Super User
Super User

Go into the query editor

copy your query (as a backup) before making changes

edit the query and click the advance editor.

you can edit the column names in the advance editor.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.