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
Anonymous
Not applicable

M Language - Transform value when it contains a particular character

Guys,

 

Need help with the following:

 

I have the following table in PowerQuery (sorry...but I don't know how to attach pictures when writing a new post):

Column 1     Column 2       Column 3         Column 4

Employee#   2018/001       2018/002         2018/003

1234567           $5000              $8000           $10,000

2345677           $8000              $9000           $14,000

 

What I want to do is automatically rename the column header based on the value of the first row.

 

Example: Column 2 would be renamed "Month 1" based on the value "2018/001".

 

The reason I want to do this is because at each refresh, the number of column might not be the same. You could have 5 or 10 months, and some months in between might even be missing. So I want a dynamic re-naming of the column so that I can later use a DAX relationship to link the money column to the master data.

 

Any ideas ?

 

Thanks,

 

Jason

1 ACCEPTED SOLUTION

Please try this code (although I still would recommend the unpivot-way):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs0tyMmvTE1VVtJRMjIwtNA3MDBEMI0QTGOlWJ1oJUMjYxNTM3OgsIqpgYEBiLaA0oYGOiAWSBVEkTmyrCVMlQlEVSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    FirstRow = #"Changed Type"{0},
    RecordToList = Record.FieldValues(FirstRow),
    SkipFirstItem = List.Skip(RecordToList,1),
    CreateNewColNames = List.Transform(SkipFirstItem, each "Column" & Text.Split(_, "/"){1}),
    CombineWithFirstCol = {List.First( RecordToList)} & CreateNewColNames,
    Rename = Table.RenameColumns(Source, List.Zip({Table.ColumnNames(Source), CombineWithFirstCol})),
    SkipFirstRow = Table.Skip(Rename,1)
in
    SkipFirstRow

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

5 REPLIES 5
ImkeF
Super User
Super User

Just to make sure my understanding is correct: You want to rename all columns except the "Employee#"-Column?

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Have you considered unpivoting the months-columns? This is usually a better practice for DAX. Column names are identifieres and if you cannot specify them in beforehand, you would have problems to refer to them in DAX.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Really sorry, I re-formatted my initial message because I was using Chrome and they whole line breaks and indentations got erased...

Please try this code (although I still would recommend the unpivot-way):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs0tyMmvTE1VVtJRMjIwtNA3MDBEMI0QTGOlWJ1oJUMjYxNTM3OgsIqpgYEBiLaA0oYGOiAWSBVEkTmyrCVMlQlEVSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    FirstRow = #"Changed Type"{0},
    RecordToList = Record.FieldValues(FirstRow),
    SkipFirstItem = List.Skip(RecordToList,1),
    CreateNewColNames = List.Transform(SkipFirstItem, each "Column" & Text.Split(_, "/"){1}),
    CombineWithFirstCol = {List.First( RecordToList)} & CreateNewColNames,
    Rename = Table.RenameColumns(Source, List.Zip({Table.ColumnNames(Source), CombineWithFirstCol})),
    SkipFirstRow = Table.Skip(Rename,1)
in
    SkipFirstRow

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Thanks ImkeF.

It works.

 

But I used the unpivot-way.

You are right. It is much better.

 

Jason.

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.