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
a1i3n
Regular Visitor

Rename Columns with IF formula

Hello Users!

I try to getin data from dynamic update HTML file. And every time column name contains unique names with constant value.
Somthing like this "Total payments 12324.00" or "Total payments 43567.00"
Last numbers in column name, it summ of column.
So for workin with data of table i need rename columns for get direct names.

So i try to make formula by my self and it dasnt work for me.

 

"#"Rename Columns" = Table.RenameColumns(Source, if([ColumnName]=(Text.Contains("Total Payments")), then {{[ColumnName], "Total Payments"}} else [ColumnName]))

I wery poor in Power Query. Please save my time.

1 ACCEPTED SOLUTION

Hello Users!

@v-ljerr-msftThank you for try to hepl me!
Whene i was try to apply your scenario i don't found solution for me.
But now i got solution!
Because i need rename several columns i entered a new variable.

let
      RenameCol = (parameter1, parameter2) => Table.RenameColumns(parameter1, List.Transform(Table.ColumnNames(parameter1), each {_, if Text.Contains(_, parameter2) then Text.TrimStart(Text.TrimEnd(parameter2)) else _})),
      Source = Web.Page(File.Contents("example.com\test.html")),
      Data0 = Source{0}[Data],
      #"Total" = RenameCol(#"Data0", "Total Payments"),
      #"Percent" = RenameCol(#"Total", "Percent")
in
      #"Percent"

Hope it solution save part of live for somebody.

View solution in original post

3 REPLIES 3
a1i3n
Regular Visitor

I found one solution

 

let
    Source = Web.Page(File.Contents("example.com\test.html")),
    Data0 = Source{0}[Data],
    #"Renamed Columns" = Table.RenameColumns(#"Data0", List.Transform(Table.ColumnNames(Data0), each {_, if Text.Contains(_, "Total") then Text.Replace(_, "NEED SELECT UNKNOWN NAME", "Solved") else Text.Replace(_, " ", "Fail")}))
in
    #"Renamed Columns"


But it did not help me completely.
It remains to find the command which allows you to replace any name of the column.
Since I do not know the exact name of the column.

Hi @a1i3n,

 

Based on my test, the formula below should work in your scenario. Smiley Happy

let
    Source = Web.Page(File.Contents("example.com\test.html")),
    Data0 = Source{0}[Data],
    #"Get Columns Names" = Table.FromList(List.Select(Table.ColumnNames(Data0),each Text.Contains(_, "Total"))),
    #"Added Custom" = Table.AddColumn(#"Get Columns Names", "Custom", each Text.Start([Column1],14) ),
    Rename = Table.RenameColumns(Data0, Record.ToList(Table.ToRecords(#"Added Custom"){0}))
    
in
    Rename

 

Regards

Hello Users!

@v-ljerr-msftThank you for try to hepl me!
Whene i was try to apply your scenario i don't found solution for me.
But now i got solution!
Because i need rename several columns i entered a new variable.

let
      RenameCol = (parameter1, parameter2) => Table.RenameColumns(parameter1, List.Transform(Table.ColumnNames(parameter1), each {_, if Text.Contains(_, parameter2) then Text.TrimStart(Text.TrimEnd(parameter2)) else _})),
      Source = Web.Page(File.Contents("example.com\test.html")),
      Data0 = Source{0}[Data],
      #"Total" = RenameCol(#"Data0", "Total Payments"),
      #"Percent" = RenameCol(#"Total", "Percent")
in
      #"Percent"

Hope it solution save part of live for somebody.

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.