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,

ItemTimes to repeatOther columns
A3ABC
B2DEF

<tbody>
</tbody>


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,

ItemTimes to repeatOther columns
A3ABC
A3ABC
A3ABC
B2DEF
B2DEF

<tbody>
</tbody>

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

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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.

EAN
SKU
Spalte1
blablab
bla
blub
blubl
Anzahl der zu druckenden Stickers
Summe
4051234567890123456735
4051234567890123456735
4051234567890123456735
4051234567890123456735
4051234567890123456735
4051234567891123456813
4051234567891123456813
4051234567891123456813

<tbody>
</tbody>
 
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,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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