Forum Discussion

a1i3n's avatar
a1i3n
Regular Visitor
8 years ago
Solved

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.

  • a1i3n's avatar
    a1i3n
    8 years ago

    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.

3 Replies

  • a1i3n's avatar
    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.

    • v-ljerr-msft's avatar
      v-ljerr-msft
      Microsoft Employee

      Hi a1i3n,

       

      Based on my test, the formula below should work in your scenario. :smileyhappy:

      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

      • a1i3n's avatar
        a1i3n
        Regular Visitor

        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.