Forum Discussion

IEG's avatar
IEG
Frequent Visitor
4 years ago
Solved

Getting the date from the file name in PowerQuery

Hi,   I do have a sofware generated forecast file, however the column names are useless. I want to change a column name automatically when the data is uploaded to PowerBI. I was using below however...
  • v-jingzhang's avatar
    4 years ago

    Hi IEG 

     

    Is this column always at the same position in different files? If so, you can change its column name based on position.

     

    For example, there are 4 columns in below table.

     

    Table.ColumnNames function will get back all column names in a table. It returns a list of names, so the index starts from 0. You can use Table.ColumnNames(#"previous step name"){0} to get the name of the first column. If the column you want to rename is the fifth column, you just need to change 0 to 4. 

     

    Then when you rename a column, you can use 

    = Table.RenameColumns(#"Changed Type",{{Table.ColumnNames(#"Changed Type"){4}, "new column name"}})

     

    Based on BA_Pete 's solution, you already get the date from file name, so you can add a custom step after that. 

    = Table.RenameColumns(#"Changed Type",{{Table.ColumnNames(#"Changed Type"){4}, Table.Column(#"Changed Type", "Name"){0}}})

     

    Renaming A Column In Power Query Based On Position — ehansalytics

     

    Best Regards,
    Community Support Team _ Jing
    If this post helps, please Accept it as Solution to help other members find it.