Forum Discussion

Gangula's avatar
Gangula
Advocate II
7 years ago
Solved

Query: Add column at the beginning

When we use the add column function in power query, it adds the new column at the very last. Is there a way to create it at the very beginning? I don't want to move it to the beginning using the rig...
  • zoloturu's avatar
    7 years ago

    Gangula,

     

    To do that you would need to add two additional steps between your data table and reordering like below:

     

     

    let    
       ...
        TableData = Table.AddColumn(TransformColumnTypes, "Half Year", each if [Month] < 7 then 1 else 2),
        ColumnNames = Table.ColumnNames(TableData),
        ReorderedList = List.Combine({{"Half Year"},List.FirstN(ColumnNames,List.Count(ColumnNames)-1)}),
    Result = Table.ReorderColumns(TableData,ReorderedList)
    in
    Result

    where

     

    - TableData is the last step of your data, in my case, it is a step where I've added a new custom column

    - Half Year is my custom column which I want to be first each time

    - ColumnNames is a step which contains a list of columns from TableData

    ReorderedList is a step which represents column names where my custom column comes first and all other column next (does not matter how many)

    Result is a step represents sorted TableData based on ReorderedList 

     

    Regards,
    Ruslan
    -------------------------------------------------------------------
    Did I answer your question? Mark my post as a solution!