Forum Discussion

deanbland's avatar
deanbland
Icon for Helper III rankHelper III
5 years ago
Solved

Combining files with different column titles

Hi,    I have a SharePoint folder that I will be dropping a file in monthly. The column headers within each file are named after the current month and year and I want to avoid manually changing the...
  • PhilipTreacy's avatar
    5 years ago

    Hi deanbland 

    Download sample PBIX with code

    Yes you can refer to table columns using the index number (position) in the table, indexed from 0. 

    So to refer to the 2nd column you would write TableName{1}

     

    If it's always the same columns that have different names, what I would do is to load the table into Power Query then rename those columns to something standard that you can refer to in all subsequent steps.

    For example, this code renames the 2nd and 3rd columns by referring to their position.

    I don't know what your data looks like but it shouldn't be hard to modify this code to suit your data. 

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY/LDcAgDEN34Vy55Adhlqr7r1FQScQpkp/s2M9T6KabK9dyFRbYPFTea+kcuihona1L6NThPq9uoAGagJeDN7AA6qh2gJYOho4jqmenAfOjlGdUw+ADjAC9QfSoSzWzHLq+W5Bc7v/ytOR08z9sWt4P", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Amount = _t, #"Customer ID" = _t]),
        RenameFirstCol = Table.RenameColumns(Source, {Table.ColumnNames(Source){1}, "Col2"}),
        RenameSecondCol = Table.RenameColumns(RenameFirstCol, {Table.ColumnNames(RenameFirstCol){2}, "Col3"})
    in
        RenameSecondCol

     

     

     

    Regards

    Phil