Powerquery - converting rows to columns based on condition

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have the following scenario:
How can I split the col. B depending on the number of values? For example, below I need to have another col C to place the second value.

A B
1679523555273.png
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Bringing this into Power Query
Book1
AB
1Table1
2ItemValue
3Heineken Lgr 330mL BT4x6 BP1201976
4Heineken Lgr 330mL BT4x6 BP1257179
5Heineken Lgr 500mL CN4x6 CP1254837
6Ice Beer 375mL CN30 MP1200517
Sheet1

Unfortunately, Split by Text to Number isn't as flexible as a simple Split by Delimiter, so had to do it in a couple of steps
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    SplitByLetterToNumber = Table.SplitColumn(Source, "Item", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Item.1", "Item.2", "Item.3", "Item.4"}),
    SplitBySpace = Table.SplitColumn(SplitByLetterToNumber, "Item.2", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Item.2.1", "Item.2.2"}),
    MergedML = Table.CombineColumns(SplitBySpace,{"Item.1", "Item.2.1"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    MergedDetail = Table.CombineColumns(MergedML,{"Item.2.2", "Item.3", "Item.4"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged.1"),
    RenamedColumns = Table.RenameColumns(MergedDetail,{{"Merged", "Item"}, {"Merged.1", "Detail"}}),
    ChangedType = Table.TransformColumnTypes(RenamedColumns,{{"Value", Int64.Type}})
in
    ChangedType
Resulting in this table
Book1
DEF
1Table1Split
2ItemDetailValue
3Heineken Lgr 330mLBT4x6 BP1201976
4Heineken Lgr 330mLBT4x6 BP1257179
5Heineken Lgr 500mLCN4x6 CP1254837
6Ice Beer 375mLCN30 MP1200517
Sheet1

Hope that's what you were thinking of!
 
Upvote 0
Maybe this?

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    GroupRows = Table.Group(Source, {"Item"}, {{"All", each _}},0),
    MaxCols = List.Accumulate(GroupRows[All], 0, (s,c)=> let trc = Table.RowCount(c) in if trc > s then trc else s),
    AddListColumn = Table.AddColumn(GroupRows, "Lists", each [All][Value] & List.Repeat({null}, MaxCols - Table.RowCount([All]))),
    ColNames = {"Item"} & List.Accumulate({1..MaxCols}, {}, (s,c)=> s & {"Value." & Text.From(c)}),
    DeleteAllColumn = Table.RemoveColumns(AddListColumn, "All"),
    TableRows = List.Transform(Table.ToRows(DeleteAllColumn), each {_{0}} & _{1}),
    Result = Table.FromRows(TableRows, ColNames)
in
    Result

Book2
ABCDEFGH
1Table1Query Output
2ItemValueItemValue.1Value.2Value.3
3A5A536
4A3B14
5A6C2
6B1
7B4
8C2
9
Sheet1
 
Upvote 1
Solution

Forum statistics

Threads
1,223,362
Messages
6,171,642
Members
452,415
Latest member
mansoorali

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