How to remove first n characters of headers in Power Query?

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I'm using Power Query to extract data from a SSAS database. Each time PQ adds a column it names it as "Dimension.<Label>" ... for example, Dimension.Customer, Dimension.Product. I don't want the "Dimension." part - just the "Customer" or "Product" part.

I can't find a way to disable this behavior so I'm wondering what the best way to removing all characters up to the period "." for each column is? Preferably not manually.

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Probably not going to help much here but here goes with a suggestion.

Is it possible to bring in the data without specifying a header row?

if so then replace dimension. with null and then promote headers.

i guess im over simplifying things
 
Upvote 0
Hi cr731,

A couple of ways you could do it below.
You just need to come up with a function that does what you want to each column name, and provide that as the 2nd argument of Table.TransformColumnNames. Two possible functions in red below.

1. Remove the first 10 characters from each column name:
Code:
= Table.TransformColumnNames(PreviousStep, [COLOR=#ff0000][B]each Text.RemoveRange(_,0,10)[/B][/COLOR] )

2. Remove everything up to the first "." from each column name:
Code:
= Table.TransformColumnNames(PreviousStep , [COLOR=#ff0000][B]each Splitter.SplitTextByEachDelimiter({"."}, null, false)(_){1}[/B][/COLOR] )
 
Last edited:
Upvote 0
Hi cr731,

2. Remove everything up to the first "." from each column name:
Code:
= Table.TransformColumnNames(PreviousStep , each Splitter.SplitTextByEachDelimiter({"."}, null, false)(_){1} )

I'd like to use this approach; however, I get Expression.Error: There weren't enough elements in the enumeration to complete the operation. Details: List.
 
Last edited:
Upvote 0
Hi cr731,
I do not see your data but try this way
Code:
let
    PreviousStep = SomeTransformation,
    Headers = List.Transform(Table.ColumnNames(PreviousStep), each {_, Text.Replace(_,"Dimension.","")}),
    Renamed = Table.RenameColumns(PreviousStep, Headers)
in
    Renamed

Regards
 
Last edited:
Upvote 0
Hi cr731,
I do not see your data but try this way
Code:
let
    PreviousStep = SomeTransformation,
    Headers = List.Transform(Table.ColumnNames(PreviousStep), each {_, Text.Replace(_,"Dimension.","")}),
    Renamed = Table.RenameColumns(PreviousStep, Headers)
in
    Renamed

Regards

"Dimension." was just one example - I actually need to remove everything up to the first "." (another one could be Time.Period, etc.)
 
Upvote 0
No problem :)
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Headers = List.Transform(Table.ColumnNames(Source), each {_, Splitter.SplitTextByPositions({Text.PositionOf(_,".",Occurrence.First)+1}) (_){0}}),
    Result = Table.RenameColumns(Source,Headers)
in
    Result
 
Upvote 0
I'd like to use this approach; however, I get Expression.Error: There weren't enough elements in the enumeration to complete the operation. Details: List.

Oh, you must have some column names that don't contain "." that don't need to be changed.

To handle a mixture of column names with/without "." use something like:

Code:
= Table.TransformColumnNames(PreviousStep, each if Text.Contains(_, ".") then Splitter.SplitTextByEachDelimiter({"."}, null, false)(_){1} else _)
 
Last edited:
Upvote 0
Thanks for all the responses; I ended up with the following:

Code:
Table.TransformColumnNames(PreviousStep, each Text.End(_, Text.Length(_) - Text.PositionOf(_,".") - 1))

And yes, for some reason the Measures columns don't include a "." i.e. just "Revenue" or "LBs" not Measures.Revenue, etc.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top