cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JohnSalt
Frequent Visitor

copy and insert a row with one change

Hi 

I have a large table (83 Cols, 2M+ Rows)  is that large these days?, I need to find the rows where the field 'productcode' is between 8000 and 9000, generate a duplicate row for each but with the 'createdDate' field set to one month earlier, and then again prior to that etc all the way back to the start of the uk fiscal year i.e. 1st April.

 

I sort of have parts of the picture using [createdDate = Date.AddMonths([createdDate],-1)] to reduce the month by one and using Table.insertrows for an additional row however with 83 cols i dont fancy specifying each and every single one! Is there a way in power query to just duplicate the entire row and change the createdDate to the month before?

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

Hi @JohnSalt,

You can try to manipulate the rows as records, but I think that a simpler approach will be to:
1. Filter the rows you need to duplicate.
2. Change the values.
3. Create a union of the original table with the changed rows.

look at this code

 

let
    Source = #table( type table [value1=text,value2=text,date=date], {{"a","aa",#date(2000,1,1)},{"b","bb",#date(2000,2,1)},{"c","cc",#date(2000,3,1)}}),
    #"Filtered Rows" = Table.SelectRows(Source, each ([date] >= #date(2000, 2, 1))),
    #"Replaced Value" = Table.ReplaceValue(#"Filtered Rows",each _[date],each Date.AddMonths(_[date],-1),Replacer.ReplaceValue,{"date"}),
    RestoreType = Value.ReplaceType(#"Replaced Value",Value.Type(#"Filtered Rows")),
    #"Appended Query" = Table.Combine({Source, RestoreType})
in
    #"Appended Query"

 

 Start:

SpartaBI_0-1655993043041.png
End:

SpartaBI_1-1655993072665.png


You will need to check the performance of this operation for your case.




2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

View solution in original post

1 REPLY 1
SpartaBI
Community Champion
Community Champion

Hi @JohnSalt,

You can try to manipulate the rows as records, but I think that a simpler approach will be to:
1. Filter the rows you need to duplicate.
2. Change the values.
3. Create a union of the original table with the changed rows.

look at this code

 

let
    Source = #table( type table [value1=text,value2=text,date=date], {{"a","aa",#date(2000,1,1)},{"b","bb",#date(2000,2,1)},{"c","cc",#date(2000,3,1)}}),
    #"Filtered Rows" = Table.SelectRows(Source, each ([date] >= #date(2000, 2, 1))),
    #"Replaced Value" = Table.ReplaceValue(#"Filtered Rows",each _[date],each Date.AddMonths(_[date],-1),Replacer.ReplaceValue,{"date"}),
    RestoreType = Value.ReplaceType(#"Replaced Value",Value.Type(#"Filtered Rows")),
    #"Appended Query" = Table.Combine({Source, RestoreType})
in
    #"Appended Query"

 

 Start:

SpartaBI_0-1655993043041.png
End:

SpartaBI_1-1655993072665.png


You will need to check the performance of this operation for your case.




2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors