Unstacking a column with variable intervals of data

althorian

New Member
Joined
Oct 20, 2016
Messages
5
Hi,

I have a power query problem I can't solve. I have data sourced a text file in a single column. In the data there are interface records that may or may not have related data, if there is related data it follows in the next lines until there is another interface record. I need to get each of the 'Word' records on their own columns in rows next to their corresponding Interface record.

I have an example below of what the source data structure looks like and further down how it should look.

Interface.Name1
Word1.Variable
Word2.Variable
Interface.Name2
Interface.Name3
Word1.Variable
Word3.Variable
Interface.Name4
Word2.Variable
Interface.Name5
Word1.Variable
Word2.Variable
Word3.Variable

<tbody>
</tbody>


Interface.Name1Word1.VariableWord2.Variable
Interface.Name2
Interface.Name3Word1.VariableWord3.Variable
Interface.Name4Word2.Variable
Interface.Name5Word1.VariableWord2.VariableWord3.Variable

<tbody>
</tbody>

I am having a hard time establishing a relationship between each 'set' of records. I have tried adding new columns for each type of record but when indexing but it doesn't work with the variable length between each record 'set'

Thanks
Ryan
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi althorian,
Try this code below
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="RawDataTbl"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Interface", each if Text.StartsWith([Data], "Interface") then [Data] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Interface"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.StartsWith([Data], "Interface")),
    #"Duplicated Column" = Table.DuplicateColumn(#"Filtered Rows", "Data", "Data — kopia"),
    #"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[#"Data — kopia"]), "Data — kopia", "Data")
in
    #"Pivoted Column"

where "RawDataTbl" is a one column table to transform with header "Data".

Regards
 
Upvote 0
Hello,

I've made a video lesson about this. See here (also you may download example file)

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
   //1) Using add conditional column feature to define left side headers (row headers)
    #"Added Conditional Column" = Table.AddColumn(Source, "Custom", each if Text.StartsWith([Column1], "Interface.Name") then [Column1] else null ),
   //2) Fill down will help us to groupping
    #"Filled Down" = Table.FillDown(#"Added Conditional Column",{"Custom"}),
   //3) Group by "Custom" column and remove "Custom" column
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {{"Gr", each Table.RemoveColumns(_,"Custom"), type table}}),
   //4) Adding another column to configure the column headers. And these names will be used in Table.Transpose() function
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Cols", each List.ReplaceRange([Gr][Column1],0,1,{"Interface.Name"})),
   //5) Using Table.Transpose() function 
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Table.Transpose([Gr],[Cols])),
   //6) Removing extra columns
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom.1"}),
   //7) Drill down the table and using Table.Combine() function
    Finish = Table.Combine(#"Removed Other Columns"[Custom.1])
in
    Finish
 
Upvote 0
Thanks for the video! that's a great way to separate out the data. I will definitely use this in the future.

Ryan
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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