Forum Discussion

McSarah's avatar
McSarah
Helper I
6 years ago
Solved

Conditional change to column name? - inconsistent source naming

Hi,   I'm working with excel report output as my Power Query report input (not fun), and my report needs to be able to consume old reports as well as recent reports. Today I was hit with a surprise...
  • McSarah's avatar
    McSarah
    5 years ago

    Final solution --

     

    I removed this clause from the code:

    each Text.Contains(_, "ChangeThisSubstring"))),

     

    This is the condition that was causing the failure, since my source data didn't always contain any instances of "ChangeThisSubstring". With the condition removed, this meant that my "#Added Custom" column name replacement table now had a line for every column in my data, not just the columns that needed replacing -- but for other columns, the "new" name was the same as the old name since the Text.Replace rules didn't affect them. 

     

    Then, realizing that {0} was an index for which column to replace, I hardcoded the column I wanted to update using the index for that column:

    Rename = Table.RenameColumns(#"Promoted Headers", Record.ToList(Table.ToRecords(#"Added Custom"){8}))

     

    Now, the name for the index is always "changed" to the Text.Replace version every time data is loaded. If a column name is needed, it's changed to the updated version. If it's already the updated version, it just changes to a copy of the original name.