Forum Discussion

magnus_b's avatar
magnus_b
Advocate II
6 years ago

Duplicate last column

Hi,

I have a source file with a dynamic number of columns, e.g.:

Year, Month, Budget revision 1, Budget revision 2, Budget revision 3...

I want to create a column called "Latest budget", that simply is a duplicate of the right most column in this table. I want to do this in the M query, and not in DAX or a measure.

I have found a function that will return the right most column name:

List.Last(Table.ColumnNames(#"Name of last step"))

 

However, using "Add column" with this function will simply print the column name, e.g. "Budget version 3". Instead, I want to get the values from that column. How can I acheive that?

4 Replies

    • magnus_b's avatar
      magnus_b
      Advocate II

      amitchandak See sample input and output below. The tricky part here is that the number of "Budget revision X" columns can vary, and the function needs to always duplicate the right-most column.

      Sample input 1:

      YearMonthBudget revision 1Budget revision 2
      202019039201008930
      20202239450345481
      20203802380802389
      20204239509299900


      Sample output 1:

      YearMonthBudget revision 1Budget revision 2Last budget
      2020190392010089301008930
      20202239450345481345481
      20203802380802389802389
      20204239509299900299900

       



      Sample input 2:

      YearMonthBudget revision 1Budget revision 2Budget revision 3
      202019039201008930901512
      20202239450345481437457
      20203802380802389568852
      20204239509299900345634

       


      Sample output 2:

      YearMonthBudget revision 1Budget revision 2Budget revision 3Last budget
      202019039201008930901512901512
      20202239450345481437457437457
      20203802380802389568852568852
      20204239509299900345634345634
  • Anonymous's avatar
    Anonymous
    Not applicable

    This worked!! Thanks.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Use the below step

    =Table.DuplicateColumn(previous step, List.Last(Table.ColumnNames(previous step)),"Newcolumnname")