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.

  • 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.

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    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.

    • Anonymous's avatar
      Anonymous
      Not applicable

      I had to make some tweaks, but your reply got me there using the date variable and CROSSJOIN, which I was not familiar with. Thanks

    • amaronda's avatar
      amaronda
      Regular Visitor

      How would the DAX change if trying to add dates for every day in the year, instead of every month of the year?

    • Anonymous's avatar
      Anonymous
      Not applicable

      I can't provide the actual data, but below is a sample of what I have, and what I would like it to look like:

       

      RegionThingAmount
      AsiaThing11000
      FranceThing25000
      USThing32000

       

      What I would like:

       

      RegionThingsAmountDate
      AsiaThing110001/1/2022
      AsiaThing110002/1/2022
      AsiaThing110003/1/2022
      AsiaThing110004/1/2022
      AsiaThing110005/1/2022
      AsiaThing110006/1/2022
      AsiaThing110007/1/2022
      AsiaThing110008/1/2022
      AsiaThing110009/1/2022
      AsiaThing1100010/1/2022
      AsiaThing1100011/1/2022
      AsiaThing1100012/1/2022
      FranceThing250001/1/2022
      FranceThing250002/1/2022
      FranceThing250003/1/2022
      FranceThing250004/1/2022
      FranceThing250005/1/2022
      FranceThing250006/1/2022
      FranceThing250007/1/2022
      FranceThing250008/1/2022
      FranceThing250009/1/2022
      FranceThing2500010/1/2022
      FranceThing2500011/1/2022
      FranceThing2500012/1/2022
      USThing320001/1/2022
      USThing320002/1/2022
      USThing320003/1/2022
      USThing320004/1/2022
      USThing320005/1/2022
      USThing320006/1/2022
      USThing320007/1/2022
      USThing320008/1/2022
      USThing320009/1/2022
      USThing3200010/1/2022
      USThing3200011/1/2022
      USThing3200012/1/2022
      • Ashish_Mathur's avatar
        Ashish_Mathur
        Icon for Super User rankSuper User

        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.