Take a list of values and space data 10 cells apart ?

mikemck

New Member
Joined
Apr 23, 2016
Messages
26
Can power query take a list of 24 values in a column and move the values 10 cells apart in the same or a seperate column?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
See if this example helps...
Using this sample data in an Excel Table named Table1:
Code:
Name     Mth     Amount
Alpha     Jan     188
Alpha     Feb     272
Alpha     Mar     151
Alpha     Apr     268
Bravo     May     294
Bravo     Jun     108
Charlie   Jul     246
Charlie   Aug     163
Charlie   Sep     260
Charlie   Oct     203
Charlie   Nov     110

This Power Query code inserts 3 blank rows between each
group of Names:
QueryName: InsBlankRowBtwnGrps
QueryCode:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

            ColHdgs         = Record.FieldNames(Source{0}),
            ColCount        = Record.FieldCount(Source{0}),
            NumOfNewRows    = 3,
            NewRowVals      = List.Repeat({null}, ColCount),
            NewRowToAdd     = Record.FromList(NewRowVals,ColHdgs),
            NewRowsStaging  = Record.AddField(NewRowToAdd,"RowSeq",{1..NumOfNewRows}),
            
    fnInsertNullRow = (TblRef as table) as table =>
        let     
            InsertionPoint  = Table.RowCount(TblRef),
            TblWithBlankRow = Table.InsertRows(TblRef, 
                                InsertionPoint, 
                                {NewRowsStaging})
            in
            TblWithBlankRow,

    SourceWithList = Table.AddColumn(Source,"RowSeq", each null),
    Grp  = Table.Group(SourceWithList, {"Name", "RowSeq"}, {{"Tbl", each _, type table}}, GroupKind.Global),

    TransformCol = Table.TransformColumns(Table.SelectColumns(Grp, {"Tbl"}), {{"Tbl", fnInsertNullRow}}),

    ExpandTblCol = Table.ExpandTableColumn(TransformCol, "Tbl", Table.ColumnNames(TransformCol{0}[Tbl])),
    ExpandRowSeqCol = Table.ExpandListColumn(ExpandTblCol, "RowSeq"),
    RemoveRowSeqCol = Table.RemoveColumns(ExpandRowSeqCol,{"RowSeq"}),
    FinalResult = RemoveRowSeqCol

in
    FinalResult

This is the end result:
Code:
Name         Mth        Amount
Alpha        Jan        188
Alpha        Feb        272
Alpha        Mar        151
Alpha        Apr        268
                
                
                
Bravo        May        294
Bravo        Jun        108
                
                
                
Charlie      Jul        246
Charlie      Aug        163
Charlie      Sep        260
Charlie      Oct        203
Charlie      Nov        110

Is that something you can work with?
 
Upvote 0
Better approach....
Billzsys pointed out to me that several steps could be saved by incorporating a list of nulls.


Here's the new code:
Code:
let

    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],



    NumOfBlankRowsToMake = 3,



    // Create a list of blanks
    ListOfBlanks = List.Repeat({null}, NumOfBlankRowsToMake),    


    // Convert the list of blanks to a table
    BlankRowsToInsert = Table.FromColumns({ListOfBlanks}, {"Name"}),



    // Group each row...Appending the blank rows to each record
    GroupRowsAndAddBlanks = Table.Group(Source, {"Name"}, {{"All", each Table.Combine({_, BlankRowsToInsert}), type table}}),


    // Combine all of the records by expanding the "All" column, omitting the duplicate "Name" column
    ExpandAndCombineRecords = Table.ExpandTableColumn(GroupRowsAndAddBlanks, "All", {"Mth", "Amount"}, {"Mth", "Amount"})
in
    ExpandAndCombineRecords
 
Upvote 0
These are great, and much appreciated. I should be able to work with these solutions and get to where I need to be to automate the whole process now in power query.

Thank you guys.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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