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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
LostintheBIu
Helper II
Helper II

How to Duplicate Rows and Change Values

Hello kind community!

 

I need help with coding to transform some HR data.
We have some employees, who start working for my organisation as a trial employee and after some time they become regular employees. When this happens, in the HR data, they are given a date "trial-to-full employment date" and their "job title" is updated to their specific new job title.

 

For my data purposes, I need to split this person's record to be split into two rows, so the 1st row shows their time period as a "trial employee", then the 2nd row shows the time period in their new non-trial job title. For example in the screenshot below, highlighted yellow John Brown.

 

I have written out the data transformation steps required, but I lack the coding skills to be able to do this myself. Please help!

 

Power BI Forums Request regarding Duplicating Rows.PNG

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Employee ID", Int64.Type}, {"Employment Start Date", type date}, {"Employment End Date", type date}, {"Job Title", type text}, {"Trial to full employment Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Rows to create", each if [Trial to full employment Date]=null then 1 else 2),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each {1..[Rows to create]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Custom"),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom", "End date", each if [Custom]=1 and [Trial to full employment Date]<>null then Date.AddDays([Trial to full employment Date],-1) else [Employment End Date]),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Job title.1", each if [Custom]=1 and [Trial to full employment Date]<>null then "Trial Employee" else [Job Title]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Employment End Date", "Job Title", "Rows to create", "Custom"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Employee ID", Int64.Type}, {"Employment Start Date", type date}, {"Employment End Date", type date}, {"Job Title", type text}, {"Trial to full employment Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Rows to create", each if [Trial to full employment Date]=null then 1 else 2),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each {1..[Rows to create]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Custom"),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom", "End date", each if [Custom]=1 and [Trial to full employment Date]<>null then Date.AddDays([Trial to full employment Date],-1) else [Employment End Date]),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Job title.1", each if [Custom]=1 and [Trial to full employment Date]<>null then "Trial Employee" else [Job Title]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Employment End Date", "Job Title", "Rows to create", "Custom"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Brilliant, it worked, thanks Ashish!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...


https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors