PQ repeat rows n times

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
In Power Query, if I have a table list this,

[TABLE="width: 500"]
<tbody>[TR]
[TD]Item[/TD]
[TD]Times to repeat[/TD]
[TD]Other columns[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]3[/TD]
[TD]ABC[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD]DEF[/TD]
[/TR]
</tbody>[/TABLE]


I want to repeat each Item by the number of times in the 'Times to repeat' column, with every other column just repeating the same values.

So given the above, I want an output like this,

[TABLE="width: 500"]
<tbody>[TR]
[TD]Item[/TD]
[TD]Times to repeat[/TD]
[TD]Other columns[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]3[/TD]
[TD]ABC[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]3[/TD]
[TD]ABC[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]3[/TD]
[TD]ABC[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD]DEF[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD]DEF[/TD]
[/TR]
</tbody>[/TABLE]

Can I use List.Generate or Table.Repeat for this? Or what's the best way?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It can easily be done with List.Numbers.
With PQ in Excel:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Typed = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Times", Int64.Type}, {"Other", type text}}),
    Repeated = Table.TransformColumns(Typed, {"Times", each List.Numbers(_,_,0), type list}),
    Expanded = Table.ExpandListColumn(Repeated, "Times")
in
    Expanded
 
Upvote 0
Hello,

also

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each {1..[Times to repeat]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Custom"})
in
    #"Removed Columns"
 
Upvote 0
Hello Marcel,

your code snippet has done so well:

It can easily be done with List.Numbers.
With PQ in Excel:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Typed = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Times", Int64.Type}, {"Other", type text}}),
    Repeated = Table.TransformColumns(Typed, {"Times", each List.Numbers(_,_,0), type list}),
    Expanded = Table.ExpandListColumn(Repeated, "Times")
in
    Expanded

But now i need a single empty row after each changing EAN Number to seperate them from each other.

So my code in Power Query M language is:
Code:
  [FONT=Calibri]let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each {1..[Summe]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Custom"})
in
    #"Removed Columns"
[/FONT]

Now i want to add one empty row after every new data. The Result should look like this:
Somehow a Table.AddRows() function should do the trick. But i just dont know the parameters to put in to fit my needs.

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]EAN
[/TD]
[TD]SKU
[/TD]
[TD]Spalte1
[/TD]
[TD]blablab
[/TD]
[TD]bla
[/TD]
[TD]blub
[/TD]
[TD]blubl
[/TD]
[TD]Anzahl der zu druckenden Stickers
[/TD]
[TD]Summe
[/TD]
[/TR]
[TR]
[TD]4051234567890[/TD]
[TD]1234567[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]4051234567890[/TD]
[TD]1234567[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]4051234567890[/TD]
[TD]1234567[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]4051234567890[/TD]
[TD]1234567[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]4051234567890[/TD]
[TD]1234567[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4051234567891[/TD]
[TD]1234568[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4051234567891[/TD]
[TD]1234568[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4051234567891[/TD]
[TD]1234568[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Ah iam sorry it was "Useful" who replied this:

Hello,

also

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each {1..[Times to repeat]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Custom"})
in
    #"Removed Columns"

I just finished this with a macro:
Code:
Public Sub Leere_Zeile_bei_Wechsel() 
    Dim lngRow As Long 
    Application.ScreenUpdating = False 
    For lngRow = Cells(Rows.Count, 1).End(xlUp).Row To 4 Step -1 
        If Cells(lngRow, 1).Value <> Cells(lngRow - 1, 1).Value And _ 
            Not IsEmpty(Cells(lngRow, 1)) And Not IsEmpty(Cells(lngRow - 1, 1)) Then _ 
            Rows(lngRow).Insert Shift:=xlShiftDown 
    Next 
    Application.ScreenUpdating = True 
End Sub

Hope this will be helpful for somebody else.

greetz
 
Upvote 0
Now i want to add one empty row after every new data. The Result should look like this:
Somehow a Table.AddRows() function should do the trick. But i just dont know the parameters to put in to fit my needs.

This should help you with doing it via PowerQuery
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top