Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Rename a Column based on the Column Position

I have pulled multiple files into PowerBI from a folder, the files are all .txt files so getting them into a usable format is a pain, but I've got it working, the only thing is, my first column says ...
  • Zang_Mi's avatar
    2 years ago

    Hello,

     

    There is an idea to solve your problem:

    1. Make the query capturing automatically the name of the first column
    2. Save it as variable
    3. Use this variable for the renaming

    Let's say that we want to rename the first column of the following table by replacing the text '202400.xlsx' by 'Date'.

    By doing it manually, Power BI will add the following step to the query, where the original column name is a fixed text. 

     

    We can replace this text by a variable that captures the first column name, which may change.

    In this new query, the yellow line shows how can we get all the column names as a list with the function Table.ColumnNames, and the expression {0} to get the first item from the list (in that case, the first column name).

     

    Then, in the last step, we can just replace the fixed text by this variable to make the query works in the same way, but able to update the name automatically even though the input files change.

     

    Regards