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

Change value by header name

Hello, 

 

Is there any way to replace the value of a cell with the Header Name for different columns at the same time? 

 

I have multiple columns when the value is Yes, and I need to change this Yes with the name of column. I know how to do it one by one, and copying and pasting the header name, but not for all at the same time and naming with code the header name directly in M Query. 

= Table.ReplaceValue(#"Replaced Value1","Yes","Header Name 1",Replacer.ReplaceText,{"Header Name 1"})

 

Thanks

2 ACCEPTED SOLUTIONS
MBreden
Helper I
Helper I

how about this solution?

let
    Quelle = Excel.CurrentWorkbook(){[Name="PFC"]}[Content],
    #"Tiefer gestufte Header" = Table.DemoteHeaders(Quelle),
    #"Transponierte Tabelle" = Table.Transpose(#"Tiefer gestufte Header"),
    ListHeader = List.RemoveFirstN(Table.ColumnNames(#"Transponierte Tabelle"),1),
    ErsetzterWert = Table.ReplaceValue(#"Transponierte Tabelle","Yes", each _[Column1],Replacer.ReplaceText,ListHeader),
    #"Transponierte Tabelle1" = Table.Transpose(ErsetzterWert),
    #"Höher gestufte Header" = Table.PromoteHeaders(#"Transponierte Tabelle1", [PromoteAllScalars=true])
in
    #"Höher gestufte Header"

 Melanie

 

View solution in original post

OK. Then instead of

(col) => {col, each _ & " " & col, type text}

use

(col) => {col, each if _ = "Yes" then col else _, type text}

View solution in original post

10 REPLIES 10
MBreden
Helper I
Helper I

how about this solution?

let
    Quelle = Excel.CurrentWorkbook(){[Name="PFC"]}[Content],
    #"Tiefer gestufte Header" = Table.DemoteHeaders(Quelle),
    #"Transponierte Tabelle" = Table.Transpose(#"Tiefer gestufte Header"),
    ListHeader = List.RemoveFirstN(Table.ColumnNames(#"Transponierte Tabelle"),1),
    ErsetzterWert = Table.ReplaceValue(#"Transponierte Tabelle","Yes", each _[Column1],Replacer.ReplaceText,ListHeader),
    #"Transponierte Tabelle1" = Table.Transpose(ErsetzterWert),
    #"Höher gestufte Header" = Table.PromoteHeaders(#"Transponierte Tabelle1", [PromoteAllScalars=true])
in
    #"Höher gestufte Header"

 Melanie

 

Anonymous
Not applicable

This is also working, but I have taken the solution from Alexis cause the necessary steps are less. 

 

Thanks for your reply in any case

AlexisOlson
Super User
Super User

You can transform multiple columns by using List.Transform to build a list of column transformations like this:

= Table.TransformColumns(
      #"Changed Type",
      List.Transform(
          {"Boolean", "Letter"},
          (col) => {col, each _ & " " & col, type text}
      )
  )

 

Starting table:

AlexisOlson_1-1645118476308.png

Transformed table:

AlexisOlson_0-1645118361084.png

 

Full sample query you can paste into the Advanced Editor of a new blank query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikwtVtJRcgRiQ6VYHRjfCYiNwHy/fCDTGYiNlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Boolean = _t, Letter = _t, Number = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Boolean", type text}, {"Letter", type text}, {"Number", Int64.Type}}),
    #"Embedded Headers" = Table.TransformColumns(#"Changed Type", List.Transform({"Boolean", "Letter"}, (col) => {col, each _ & " " & col, type text}))
in
    #"Embedded Headers"
Anonymous
Not applicable

Thanks for your answer. My idea was changing the Yes by the Header Name, not include in all the columns the header name. Is it a way to perform this? 

Can you give a simple example demonstrating what you mean?

Anonymous
Not applicable

Yes sure: 

 

Initial data

 

TitleName 1Name 2Name 3Name 4
AYesNoNoYes
BNoYesNoYes

 

Expected outcome

 

TitleName 1Name 2Name 3Name 4
AName 1NoNoName 4
BNoName 2NoName 4

OK. Then instead of

(col) => {col, each _ & " " & col, type text}

use

(col) => {col, each if _ = "Yes" then col else _, type text}
Anonymous
Not applicable

Thanks, this is correctly working

jennratten
Super User
Super User

Hello - you can unpivot the columns in which the values needing to be replaced, which will give you one column for all of the column names and one column for all of their corresponding values.  Then apply the replacements function to the values column and pivot back.

Anonymous
Not applicable

Thanks for your answer. I was searching something to be done without pivot and unpivot in order to do it with the replace function, although this is also having the same result. I will leave the issue open in case someone knows how to do it and if not I will accept this solution. 

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