Transpose and separate data

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Book4
ABCDE
1TITLENAMESubjectGradeScore
2BusinessTomABC1010D72
3BusinessTomABC1020D75
4BusinessTomABC1110C63
5BusinessTomABC1690D71
6BusinessTomABC1500N45
7BusinessTomABC1700P51
8EngineeringJackABC4160C62
9EngineeringJackABC4390D73
10EngineeringJackABC4490D72
11EngineeringJackABC4140C62
12ArtsPeterABC4160C60
13ArtsPeterABC4390P52
14ArtsPeterABC4140N38
15ArtsPeterABC4270N47
16ArtsFredABC6010N4
17ArtsFredABC6020N3
18ArtsFredABC6090N3
19ArtsFredABC6120N3
20BusinessBertABC2020D73
21BusinessBertABC2040N42
22BusinessBertABC2050D73
23BusinessBertABC2080N44
Sheet1


Would like to have the records showing (with each record component in a separate cell):
Business Tom ABC1010 D 72 ABC1020 D 75 ABC1110 C 63 etc,
Engineering Jack ABC4160 C 62 etc,
 
Your code contain different name(s) than source you show in the first post so it doesn't work to me.
eg. Historical Results
but I think you can do that yourself now

I've to go so happy querying :biggrin:
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Here is a single query which will do that
(I added this code to the shared file from post#9)
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"TITLE", "NAME"}, {{"Count", each _, type table}}),
    Subject = Table.AddColumn(Group, "Subject", each [Count][Subject]),
    ExtractSubject = Table.TransformColumns(Subject, {"Subject", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Grade = Table.AddColumn(ExtractSubject, "Grade", each [Count][Grade]),
    ExtractGrade = Table.TransformColumns(Grade, {"Grade", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Score = Table.AddColumn(ExtractGrade, "Score", each [Count][Score]),
    ExtractScore = Table.TransformColumns(Score, {"Score", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    SplitSubject = Table.SplitColumn(ExtractScore, "Subject", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Subject.1", "Subject.2", "Subject.3", "Subject.4", "Subject.5", "Subject.6"}),
    SplitGrade = Table.SplitColumn(SplitSubject, "Grade", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Grade.1", "Grade.2", "Grade.3", "Grade.4", "Grade.5", "Grade.6"}),
    SplitScore = Table.SplitColumn(SplitGrade, "Score", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Score.1", "Score.2", "Score.3", "Score.4", "Score.5", "Score.6"}),
    Reorder = Table.ReorderColumns(SplitScore,{"TITLE", "NAME", "Count", "Subject.1", "Grade.1", "Score.1", "Subject.2", "Grade.2", "Score.2", "Subject.3", "Grade.3", "Score.3", "Subject.4", "Grade.4", "Score.4", "Subject.5", "Grade.5", "Score.5", "Subject.6", "Grade.6", "Score.6"}),
    Type = Table.TransformColumnTypes(Reorder,{{"TITLE", type text}, {"NAME", type text}, {"Count", type any}, {"Subject.1", type text}, {"Grade.1", type text}, {"Score.1", Int64.Type}, {"Subject.2", type text}, {"Grade.2", type text}, {"Score.2", Int64.Type}, {"Subject.3", type text}, {"Grade.3", type text}, {"Score.3", Int64.Type}, {"Subject.4", type text}, {"Grade.4", type text}, {"Score.4", Int64.Type}, {"Subject.5", type text}, {"Grade.5", type text}, {"Score.5", Int64.Type}, {"Subject.6", type text}, {"Grade.6", type text}, {"Score.6", Int64.Type}})
in
    Type

TITLENAMECountSubject.1Grade.1Score.1Subject.2Grade.2Score.2Subject.3Grade.3Score.3Subject.4Grade.4Score.4Subject.5Grade.5Score.5Subject.6Grade.6Score.6
BusinessTom ABC1010D72ABC1020D75ABC1110C63ABC1690D71ABC1500N45ABC1700P51
EngineeringJack ABC4160C62ABC4390D73ABC4490D72ABC4140C62
ArtsPeter ABC4160C60ABC4390P52ABC4140N38ABC4270N47
ArtsFred ABC6010N4ABC6020N3ABC6090N3ABC6120N3
BusinessBert ABC2020D73ABC2040N42ABC2050D73ABC2080N44
 
Last edited:
Upvote 0
Hi Sandy & Tigerexcel
Here is my attempt to solve the problem (based on Sandy's file from post #9)
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"TITLE", type text}, {"NAME", type text}, {"Subject", type text}, {"Grade", type text}, {"Score", Int64.Type}}),
    GroupedRows = Table.Group(Type, {"TITLE", "NAME"}, {{"lst", each List.Combine(Table.ToRows(Table.RemoveColumns(_, {"TITLE", "NAME"}))), type list }}),
    HowMany = Table.AddColumn(GroupedRows, "hm", each List.Count([lst])+2),
    SortedRows = Table.Sort(HowMany,{{"hm", Order.Descending}}),
    AddedTableColumn = Table.AddColumn(SortedRows, "Tbl", each Table.FromRows({{[TITLE], [NAME]} & [lst]})),
    Combine = Table.Combine(AddedTableColumn[Tbl]),
    Max = (List.Max(HowMany[hm])-2)/3,
    OldColNames = List.Buffer(Table.ColumnNames(Combine)),
    ColToRepl = List.Buffer(List.LastN(Table.ColumnNames(Source),3)),
    NewColNames = {"TITLE", "NAME"} & List.Combine(List.Transform({1..Max}, (x) => List.Transform(ColToRepl, (y) => Text.Combine({y, Text.From(x)},".")))),    
    Result = Table.RenameColumns(Combine, List.Zip({OldColNames, NewColNames}))
in
    Result
 
Last edited:
Upvote 0
Hi Billszysz,

Thanks for your input, that is very clever. I have not seen some of those PQ steps before particularly the How Many added column and the List Buffer steps.
 
Upvote 0
Hi, I'm new to Power Query. I tried to solve this problem and it seems worked well, post for your reference.
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Index =Table.AddIndexColumn(Table.UnpivotOtherColumns(Source, {"TITLE", "NAME"}, "Attr", "Value"),"Index",1),
    fx=(x)=>
        Text.From(
            List.Count(
                List.PositionOf(Table.SelectRows(Index,each [Index]<=x{4} and [TITLE]=x{0} and [NAME]=x{1})[Attr],x{2},2))),
    DeleteCol = Table.RemoveColumns(Table.AddColumn(Index,"Helper",each _[Attr]&"."&fx(Record.ToList(_))),{"Attr", "Index"}),
    Pivot = Table.Pivot(DeleteCol, List.Distinct(DeleteCol[Helper]), "Helper", "Value")
in
    Pivot
1595826995896.png

Transpose and separate data.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1TITLENAMESubjectGradeScoreTITLENAMESubject.1Grade.1Score.1Subject.2Grade.2Score.2Subject.3Grade.3Score.3Subject.4Grade.4Score.4Subject.5Grade.5Score.5Subject.6Grade.6Score.6
2BusinessTomABC1010D72ArtsFredABC6010N4ABC6020N3ABC6090N3ABC6120N3
3BusinessTomABC1020D75ArtsPeterABC4160C60ABC4390P52ABC4140N38ABC4270N47
4BusinessTomABC1110C63BusinessBertABC2020D73ABC2040N42ABC2050D73ABC2080N44
5BusinessTomABC1690D71BusinessTomABC1010D72ABC1020D75ABC1110C63ABC1690D71ABC1500N45ABC1700P51
6BusinessTomABC1500N45EngineeringJackABC4160C62ABC4390D73ABC4490D72ABC4140C62
7BusinessTomABC1700P51
8EngineeringJackABC4160C62
9EngineeringJackABC4390D73
10EngineeringJackABC4490D72
11EngineeringJackABC4140C62
12ArtsPeterABC4160C60
13ArtsPeterABC4390P52
14ArtsPeterABC4140N38
15ArtsPeterABC4270N47
16ArtsFredABC6010N4
17ArtsFredABC6020N3
18ArtsFredABC6090N3
19ArtsFredABC6120N3
20BusinessBertABC2020D73
21BusinessBertABC2040N42
22BusinessBertABC2050D73
23BusinessBertABC2080N44
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,866
Members
449,129
Latest member
krishnamadison

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