Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Duplicate Rows & Add Dates

I have a data set with annual info, wondering if it's easy in Power Query to duplicate each row 11x and add a separate column with a corresponding month?
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous ,

     

    Please try to create a new table using DAX:

    New Table = 
    var _date=FILTER(CALENDAR(DATE(YEAR(TODAY()),1,1),DATE(YEAR(TODAY()),12,31)),DAY([Date])=1)
    return CROSSJOIN('Table',_date)

    Or 

    New Table2 = 
    var _date=SELECTCOLUMNS( GENERATESERIES(1,12,1),"Date",CONVERT( YEAR(TODAY())&"-"&[Value]&"-1",DATETIME))
    return CROSSJOIN('Table',_date)

    Output:

     

    Best Regards,
    Eyelyn Qin
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Ashish_Mathur's avatar
    Ashish_Mathur
    4 years ago

    Hi,

    This M code works

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcizOTFTSUQrJyMxLNwQyDA0MDJRidaKV3IoS85JTYVJGQIYpTCo0GCZsDGQYgYVjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Region = _t, Thing = _t, Amount = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Region", type text}, {"Thing", type text}, {"Amount", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each {1..12}),
        #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
        #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Date", each "1/"&Number.ToText([Custom])&"/2022"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom1",{{"Date", type date}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Custom"})
    in
        #"Removed Columns"

    Hope this helps.