cancel
Showing results for 
Search instead for 
Did you mean: 
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.

View solution in original post

Helpful resources

Announcements
MBAS on Demand

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

July 2021 Update 768x460.png

Check it out!

Click here to read more about the July 2021 Updates

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Urdu Hindi D365 Bootcamp 768x460.png

Urdu Hindi D365 Bootcamp

Dont miss our very own April Dunnam’s The Developer Guide to the Galaxy! Find out what the Power Platform has to offer for the traditional developer.

Top Solution Authors
Top Kudoed Authors