Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
cpiolla
Regular Visitor

How to Duplicate Rows and Change Values

Hi everyone!

I'm currently working on a dashboard that disposes informations about the delivery of laboratory samples at my company.

For example, the sample '2000022183' was supposed to be delivered at April 29th but it arrived only at August 15th.
What I need is to duplicate the row of that sample and add plus one month on Expected Date for the necessary times until the Month from Expected and Actual dates are the same. It needs to be done automatic by Power BI because the table for this dash has thousands of different samples registered and it's impossible to do it manually.

Is it possible to do it using Power Query or something like that???


Here is an image of what i need:

cpiolla_0-1670934476247.png

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @cpiolla 

 

First add a custom column with below code to have a list of dates on each row. 

let startDate = Date.AddMonths(Date.StartOfMonth([Expected Date]),1), endDate = [Actual Date] in List.Combine({{[Expected Date]}, List.Generate(()=> startDate, each _ <= endDate, each Date.AddMonths(_, 1))})

vjingzhang_1-1671000910264.png

 

Then expand above list column to new rows. You will have all expected dates in a new column. Remove the old "Expected Date" column and rename the new column. 

vjingzhang_0-1671000740642.png

 

Full code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIytDBW0lEy0Tey1DcyMDICsi30DU0h7FidaCUTI3MjE6CoMVxUR8lc3xCqIBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID Sample" = _t, #"Expected Date" = _t, #"Actual Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID Sample", Int64.Type}, {"Expected Date", type date}, {"Actual Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let startDate = Date.AddMonths(Date.StartOfMonth([Expected Date]),1), endDate = [Actual Date] in List.Combine({{[Expected Date]}, List.Generate(()=> startDate, each _ <= endDate, each Date.AddMonths(_, 1))})),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom")
in
    #"Expanded Custom"

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. Highly appreciate your Kudos!

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @cpiolla 

 

First add a custom column with below code to have a list of dates on each row. 

let startDate = Date.AddMonths(Date.StartOfMonth([Expected Date]),1), endDate = [Actual Date] in List.Combine({{[Expected Date]}, List.Generate(()=> startDate, each _ <= endDate, each Date.AddMonths(_, 1))})

vjingzhang_1-1671000910264.png

 

Then expand above list column to new rows. You will have all expected dates in a new column. Remove the old "Expected Date" column and rename the new column. 

vjingzhang_0-1671000740642.png

 

Full code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIytDBW0lEy0Tey1DcyMDICsi30DU0h7FidaCUTI3MjE6CoMVxUR8lc3xCqIBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID Sample" = _t, #"Expected Date" = _t, #"Actual Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID Sample", Int64.Type}, {"Expected Date", type date}, {"Actual Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let startDate = Date.AddMonths(Date.StartOfMonth([Expected Date]),1), endDate = [Actual Date] in List.Combine({{[Expected Date]}, List.Generate(()=> startDate, each _ <= endDate, each Date.AddMonths(_, 1))})),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom")
in
    #"Expanded Custom"

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. Highly appreciate your Kudos!

Thank you @v-jingzhang ! It worked perfectly!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.