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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
nichanallseg
Frequent Visitor

Write query to insert copied rows, plus one custom column with running number

Hi all,

 

I need help with Power Query. I need to write a query that will help inserting new rows.

 

So here is the structure of the original data table

It contains employee information with the number of work hours per day

 

nichanallseg_0-1650019178949.png

 


For each employee, I want to insert another 11 copied rows with a new column called "Month" which will be a running number from 1 to 12 (this will be used later to create a computed column that will calculate working hours per month).

The output table should look something like this

 

nichanallseg_2-1650019256428.png

 


Please kindly help as real data contains thousands of employees!

 

Thank you so much in advance.

 

Regards,

Nichanal

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Just inset a custom column named Month and put following there

={1..12}

Then expand this column to new rows.

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMDBU0lHySsxLVfAGMsyVYnUgwkZAnnNGUWYxmrgxkBeSWJmTX6TgDmSawSVMgDzfzOSM1JxUBWeQFj1TuJwpyI7MXAUPiI5YAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Emp ID" = _t, #"Emp Name" = _t, #"Availability per day" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp ID", Int64.Type}, {"Emp Name", type text}, {"Availability per day", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Month", each {1..12}),
    #"Expanded Month" = Table.ExpandListColumn(#"Added Custom", "Month")
in
    #"Expanded Month"

View solution in original post

3 REPLIES 3
nichanallseg
Frequent Visitor

It works! Thank you

HotChilli
Super User
Super User

Add a custom column {1..12}

Then Expand it to new rows.

Vijay_A_Verma
Super User
Super User

Just inset a custom column named Month and put following there

={1..12}

Then expand this column to new rows.

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMDBU0lHySsxLVfAGMsyVYnUgwkZAnnNGUWYxmrgxkBeSWJmTX6TgDmSawSVMgDzfzOSM1JxUBWeQFj1TuJwpyI7MXAUPiI5YAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Emp ID" = _t, #"Emp Name" = _t, #"Availability per day" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp ID", Int64.Type}, {"Emp Name", type text}, {"Availability per day", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Month", each {1..12}),
    #"Expanded Month" = Table.ExpandListColumn(#"Added Custom", "Month")
in
    #"Expanded Month"

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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