Forum Discussion

GokilaRaviraj's avatar
GokilaRaviraj
Helper II
1 year ago
Solved

Copy and paste entire column value into another column based on current month or not

Hi    Can anyone please tell me how to copy entire column data and paste that values in another empty column based on current month name. This is how my table looks now. Since we are in Sept...
  • bhanu_gautam's avatar
    1 year ago

    GokilaRaviraj , Try using below m-code

     

    let
    // Load your data source
    Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content],

    // Add a column with the current month name
    AddCurrentMonth = Table.AddColumn(Source, "CurrentMonth", each Date.ToText(DateTime.LocalNow(), "MMMM"), type text),

    // Duplicate the "Other income" column
    DuplicateOtherIncome = Table.AddColumn(AddCurrentMonth, "CurrentMonthData", each [Other income]),

    // Add a conditional column to copy data based on the current month
    AddConditionalColumn = Table.AddColumn(DuplicateOtherIncome, "UpdatedColumn", each if [CurrentMonth] = "September" then [CurrentMonthData] else null),

    // Remove unnecessary columns
    RemoveColumns = Table.RemoveColumns(AddConditionalColumn,{"CurrentMonth", "CurrentMonthData"}),

    // Rename the updated column to the current month name
    RenameColumn = Table.RenameColumns(RemoveColumns, {{"UpdatedColumn", Date.ToText(DateTime.LocalNow(), "MMMM")}})
    in
    RenameColumn