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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
jwb3d
Frequent Visitor

Add duplicate row based on column

I have a database with steps applied to it on Power Query, I would like Add the number of rows
to my table based on the "phaseCount" . Example of what I want with a before and after:

before

fidphaseCountphaseextractDatedeviceName
2018007223ABC03-Mar-26Device1
2014585411A03-Mar-26Device2
2018012301B03-Mar-26Device3
1102209282BC03-Mar-26Device4


After

fidphaseCountphaseextractDatedeviceName
2018007223ABC03-Mar-26Device1
2018007223ABC03-Mar-26Device1
2018007223ABC03-Mar-26Device1
2014585411A03-Mar-26Device2
2018012301B03-Mar-26Device3
1102209282BC03-Mar-26Device4
1102209282BC03-Mar-26Device4

 

Here is my Power Query

 

let
Source = Sql.Databases("SQLUTLCLP-123\PROD02,00000"),
DMS = Source{[Name="DMS"]}[Data],
eccl_gisDeviceChanges = DMS{[Schema="eccl",Item="gisDeviceChanges"]}[Data],
#"Extracted Date" = Table.TransformColumns(eccl_gisDeviceChanges,{{"extractDate", DateTime.Date, type date}}),
#"Filtered Rows" = Table.SelectRows(#"Extracted Date", each ([extractDate] <> null and [extractDate] <> #date(2025, 7, 7))),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"fid", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Inserted Cleaned Text" = Table.AddColumn(#"Added Index", "Clean", each Text.Clean(Text.From([Index], "en-US")), type text),
#"Inserted Trimmed Text" = Table.AddColumn(#"Inserted Cleaned Text", "Trim", each Text.Trim([Clean]), type text),
#"Added Index1" = Table.AddIndexColumn(#"Inserted Trimmed Text", "Index.1", 0, 1, Int64.Type),
#"Inserted Trimmed Text1" = Table.AddColumn(#"Added Index1", "Trim.1", each Text.Trim(Text.From([Index.1], "en-US")), type text),
AddedCustom =
Table.AddColumn (
ChangedType,
"Custom",
each Table.Repeat ( Table.FromRecords ( { _ } ), _[phaseCount] ),
ColumnAndTypes
),
RemovedColumns = Table.RemoveColumns ( AddedCustom, { "deviceName", "fid", "phaseCount" } ),
ExpandedCustom =
Table.ExpandTableColumn (
RemovedColumns,
"Custom",
ColumnNames,
ColumnNames
),
#"Kept Duplicates" = let columnNames = {"fid"}, addCount = Table.Group(#"Added Index", columnNames, {{"Count", Table.RowCount, type number}}), selectDuplicates = Table.SelectRows(addCount, each [Count] > 1), removeCount = Table.RemoveColumns(selectDuplicates, "Count") in Table.Join(#"Added Index", columnNames, removeCount, columnNames, JoinKind.Inner),
#"Filtered Rows1" = Table.SelectRows(#"Kept Duplicates", each true)
in
#"Kept Duplicates"

 

 

3 ACCEPTED SOLUTIONS
Natarajan_M
Solution Sage
Solution Sage

Hi @jwb3d , from your query, I beleive you need to duplicate same rows if the Phase count is more than one value .

IP :

Natarajan_M_0-1775516638400.png



Use Table .Repeat fundtion to handle this scenario

let
    Source = #"IP Table", // -> Source table

    ColNames = Table.ColumnNames(Source),

    // Nest each row as a repeated table
    AddedRepeated = Table.AddColumn(
        Source,
        "RepeatedRows",
        each Table.Repeat( Table.FromRecords({ _ }), [phaseCount] )
    ),

    // the id is causing error so drop originals BEFORE expanding to avoid duplicate field error
    OnlyNested = Table.SelectColumns(AddedRepeated, {"RepeatedRows"}),

    Expanded = Table.ExpandTableColumn(
        OnlyNested, "RepeatedRows", ColNames
    ),
    #"Added Index" = Table.AddIndexColumn(Expanded, "Index", 1, 1, Int64.Type)

in
    #"Added Index"



OP:

Natarajan_M_1-1775516835152.png

 


Sample : 
Row Expand.pbix


Thanks
If you found this helpful, please consider giving it a kudo and marking it as the accepted solution — it goes a long way in helping others facing the same issue.

For more Power BI tips and discussions, let’s connect on LinkedIn:
https://www.linkedin.com/in/natarajan-manivasagan

Cheers!

View solution in original post

ZhangKun
Super User
Super User

if you want to add duplicate rows, you can add a list.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtDAwMDcyUtJRMgZiRydnIGlgrOubWKRrZAZku6SWZSanGirF6oBVm5hamJoYAsVB2BGrWiOYWgsDQyNjA6haJ6xqjcFqDQ0NjIwMLI0sgOIgl+BwhIlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fid = _t, phaseCount = _t, phase = _t, extractDate = _t, deviceName = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"fid", Int64.Type}, {"phaseCount", Int64.Type}, {"phase", type text}, {"extractDate", type date}, {"deviceName", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,null,(old,new,z)=>List.Repeat({old}, Text.Length(old)),{"phase"}),
    #"Expanded {0}" = Table.ExpandListColumn(#"Replaced Value", "phase")
in
    #"Expanded {0}"

View solution in original post

Ahmedx
Super User
Super User

pls try

let
 Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtDAwMDcyUtJRMgZiRydnIGlgrOubWKRrZAZku6SWZSanGirF6oBVm5hamJoYAsVB2BGrWiOYWgsDQyNjA6haJ6xqjcFqDQ0NjIwMLI0sgOIgl+BwhIlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fid = _t, phaseCount = _t, phase = _t, extractDate = _t, deviceName = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"phaseCount", type number}}),
 lst = Table.ColumnNames(#"Changed Type"),
 from = List.TransformMany(Table.ToRows(#"Changed Type"), (x)=> {1..x{1}},(x,y)=>x),
 tb = Table.FromRows(from,lst)

in
    tb

View solution in original post

6 REPLIES 6
dufoq3
Super User
Super User

Hi @jwb3d, another solution:

Result:

dufoq3_0-1775637586508.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtDAwMDcyUtJRMgZiRydnIGlgrOubWKRrZAZku6SWZSanGirF6oBVm5hamJoYAsVB2BGrWiOYWgsDQyNjA6haJ6xqjcFqDQ0NjIwMLI0sgOIgl+BwhIlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fid = _t, phaseCount = _t, phase = _t, extractDate = _t, deviceName = _t]),
    PhaseCountToList = Table.TransformColumns(Source,{{"phaseCount", each let n = Number.From(_) in List.Repeat({n}, n) , type {number}}}),
    ExpandedPhaseCount = Table.ExpandListColumn(PhaseCountToList, "phaseCount")
in
    ExpandedPhaseCount

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Ahmedx
Super User
Super User

This is optimized code, try it.

let
 Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtDAwMDcyUtJRMgZiRydnIGlgrOubWKRrZAZku6SWZSanGirF6oBVm5hamJoYAsVB2BGrWiOYWgsDQyNjA6haJ6xqjcFqDQ0NjIwMLI0sgOIgl+BwhIlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fid = _t, phaseCount = _t, phase = _t, extractDate = _t, deviceName = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"phaseCount", type number}}),
 from = Table.FromRecords( List.TransformMany(
     Table.ToRecords(#"Changed Type"),(x)=> {1.. Record.FieldValues(x){1}},(x,y)=>x))
in
    from

 

Ahmedx
Super User
Super User

pls try

let
 Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtDAwMDcyUtJRMgZiRydnIGlgrOubWKRrZAZku6SWZSanGirF6oBVm5hamJoYAsVB2BGrWiOYWgsDQyNjA6haJ6xqjcFqDQ0NjIwMLI0sgOIgl+BwhIlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fid = _t, phaseCount = _t, phase = _t, extractDate = _t, deviceName = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"phaseCount", type number}}),
 lst = Table.ColumnNames(#"Changed Type"),
 from = List.TransformMany(Table.ToRows(#"Changed Type"), (x)=> {1..x{1}},(x,y)=>x),
 tb = Table.FromRows(from,lst)

in
    tb
ZhangKun
Super User
Super User

if you want to add duplicate rows, you can add a list.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtDAwMDcyUtJRMgZiRydnIGlgrOubWKRrZAZku6SWZSanGirF6oBVm5hamJoYAsVB2BGrWiOYWgsDQyNjA6haJ6xqjcFqDQ0NjIwMLI0sgOIgl+BwhIlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fid = _t, phaseCount = _t, phase = _t, extractDate = _t, deviceName = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"fid", Int64.Type}, {"phaseCount", Int64.Type}, {"phase", type text}, {"extractDate", type date}, {"deviceName", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,null,(old,new,z)=>List.Repeat({old}, Text.Length(old)),{"phase"}),
    #"Expanded {0}" = Table.ExpandListColumn(#"Replaced Value", "phase")
in
    #"Expanded {0}"
Natarajan_M
Solution Sage
Solution Sage

Hi @jwb3d , from your query, I beleive you need to duplicate same rows if the Phase count is more than one value .

IP :

Natarajan_M_0-1775516638400.png



Use Table .Repeat fundtion to handle this scenario

let
    Source = #"IP Table", // -> Source table

    ColNames = Table.ColumnNames(Source),

    // Nest each row as a repeated table
    AddedRepeated = Table.AddColumn(
        Source,
        "RepeatedRows",
        each Table.Repeat( Table.FromRecords({ _ }), [phaseCount] )
    ),

    // the id is causing error so drop originals BEFORE expanding to avoid duplicate field error
    OnlyNested = Table.SelectColumns(AddedRepeated, {"RepeatedRows"}),

    Expanded = Table.ExpandTableColumn(
        OnlyNested, "RepeatedRows", ColNames
    ),
    #"Added Index" = Table.AddIndexColumn(Expanded, "Index", 1, 1, Int64.Type)

in
    #"Added Index"



OP:

Natarajan_M_1-1775516835152.png

 


Sample : 
Row Expand.pbix


Thanks
If you found this helpful, please consider giving it a kudo and marking it as the accepted solution — it goes a long way in helping others facing the same issue.

For more Power BI tips and discussions, let’s connect on LinkedIn:
https://www.linkedin.com/in/natarajan-manivasagan

Cheers!

I would like to include a column that is derived from the deviceName and phase. This would involve appending A-B-C to each of the duplicated rows.

Example

fidphaseCountphaseextractDatedeviceNameIndexNew Column
2018007223ABC3/3/2026Device11Device1A
2018007223ABC3/3/2026Device12Device1B
2018007223ABC3/3/2026Device13Device1C
8210472932AC3/3/2026Device1019Device1A
8210472932AC3/3/2026Device1020Device1B
7201836501D3/3/2026Device918Device9D

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.