Not merging records in PQ

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi all,

I've been working on a PQ situation and did post in the general Excel Questions, I have progressed the solution (a little) but have reached a snag.

I have 2 tables,

Student CodeLast NameFirst NameCourse
1SmithFred2607
2JonesJack2645
3CharlesTom2607
4HarrisBert2608
5EllisBob2638
6JenkinsHarry2638

Person IDPartSUAPassFailTotal Marks001002003004
12431221Biol 60Maths 70Sci 40Eng 51
22531219Biol 60Maths 70Sci 38Eng 51
31431217Biol 60Maths 68Sci 38Eng 51
4244230Biol 60Maths 70Sci 50French 50
511172Biol 72
52312115Maths 50Sci 40Eng 25
6144221Biol 60Maths 70Sci 40Eng 51
72431240Biol 60Maths 70Sci 70Eng 40
82844302Biol 60Maths 70Sci 100Eng 72

Eventual output would be Id, last name, first name, Course, Part, SUA, Pass, Fail, Total Marks, Results (all in one cell).
I have had some assistance from Sandy666 and I have amended his solution to try and get mine to work but no matter what Bob Ellis who is ID 5 always appears on 2 rows ie I cannot merge his record successfully.
Here is Sandy's suggestion (which I played around with a little) which seemed to work with some mock data but I cannot get it to work on the data in this post.

let
Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Merged Columns" = Table.CombineColumns(Source2,{"001", "002", "003", "004"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
Trim = Table.AddColumn(#"Merged Columns", "Result", each Text.TrimEnd([Merged], ",")),
#"Grouped Rows" = Table.Group(Trim, {"Person ID"}, {{"Subjects", each List.Sum([SUA]), type number}, {"Total marks", each List.Sum([Total Marks]), type number}, {"All", each _, type table}}),
List = Table.AddColumn(#"Grouped Rows", "List", each [All][Result]),
Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
Join = Table.NestedJoin(Source1,{"Student Code"},Table2,{"Person ID"},"Table",JoinKind.LeftOuter),
Expand = Table.ExpandTableColumn(Join, "Table", {"Course", "SUA", "Pass", "Fail", "Total Marks", "001", "002", "003", "004"}, {"Course", "SUA", "Pass", "Fail", "Total Marks", "001", "002", "003", "004"})
in
Expand
They need to be linked
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I suggest you post a linknext time to mentioned thread
and use code tags for code like [CODE] your code here [/CODE]
would be fine to see expected result based on your example
 
Last edited:
Upvote 0
Rich (BB code):
let
    Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Mrg = Table.CombineColumns(Source2,{"1", "2", "3", "4"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    Trim = Table.AddColumn(Mrg, "Result", each Text.TrimEnd([Merged], ",")),
    Group = Table.Group(Trim, {"Person ID"}, {{"Part", each List.Sum([Part]), type number}, {"SUA", each List.Sum([SUA]), type number}, {"Pass", each List.Sum([Pass]), type number}, {"Fail", each List.Sum([Fail]), type number}, {"Total marks", each List.Sum([Total Marks]), type number}, {"All", each _, type table}}),
    List = Table.AddColumn(Group, "List", each [All][Result]),
    Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Join = Table.NestedJoin(Source1,{"Student Code"},Extract,{"Person ID"},"Table",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Join, "Table", {"Part", "SUA", "Pass", "Fail", "Total marks", "List"}, {"Part", "SUA", "Pass", "Fail", "Total marks", "List"})
in
    Expand
 
Upvote 0
Thanks Sandy! That seems to work fine. I can't see that you changed much with the code. Can you tell me what I did wrong on the previous query or what you did right on the latest query?
 
Last edited:
Upvote 0
at first look
Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
Join = Table.NestedJoin(Source1,{"Student Code"},Table2,{"Person ID"},"Table",JoinKind.LeftOuter),
should be Extract (previous step)

see here
Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
Join = Table.NestedJoin(Source1,{"Student Code"},Extract,{"Person ID"},"Table",JoinKind.LeftOuter),

you need transformed layout of Table2 not original
 
Upvote 0
Thanks Sandy so I guess having Table2 in the above command creates some sort of duplication which won't allow the combination of information in the separate rows. I think that makes sense, just trying to understand the theory behind this.
 
Upvote 0
structure
Rich (BB code):
   Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//this is the first table

    Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Mrg = Table.CombineColumns(Source2,{"1", "2", "3", "4"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    Trim = Table.AddColumn(Mrg, "Result", each Text.TrimEnd([Merged], ",")),
    Group = Table.Group(Trim, {"Person ID"}, {{"Part", each List.Sum([Part]), type number}, {"SUA", each List.Sum([SUA]), type number}, {"Pass", each List.Sum([Pass]), type number}, {"Fail", each List.Sum([Fail]), type number}, {"Total marks", each List.Sum([Total Marks]), type number}, {"All", each _, type table}}),
    List = Table.AddColumn(Group, "List", each [All][Result]),
    Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
//this is the second table after transforming

    Join = Table.NestedJoin(Source1,{"Student Code"},Extract,{"Person ID"},"Table",JoinKind.LeftOuter),
// here are merged two tables from above

    Expand = Table.ExpandTableColumn(Join, "Table", {"Part", "SUA", "Pass", "Fail", "Total marks", "List"}, {"Part", "SUA", "Pass", "Fail", "Total marks", "List"})
// exapanded column table after merge
You can't merge table1 and table2 with their original layouts because it doesn't make sense that is why table2 is transformed to the proper layout (see step: Extract) then you can merge Source1 (Table1) and transformed table2 (Extract)
 
Last edited:
Upvote 0
structure
Rich (BB code):
   Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//this is the first table

    Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Mrg = Table.CombineColumns(Source2,{"1", "2", "3", "4"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    Trim = Table.AddColumn(Mrg, "Result", each Text.TrimEnd([Merged], ",")),
    Group = Table.Group(Trim, {"Person ID"}, {{"Part", each List.Sum([Part]), type number}, {"SUA", each List.Sum([SUA]), type number}, {"Pass", each List.Sum([Pass]), type number}, {"Fail", each List.Sum([Fail]), type number}, {"Total marks", each List.Sum([Total Marks]), type number}, {"All", each _, type table}}),
    List = Table.AddColumn(Group, "List", each [All][Result]),
    Extract = Table.TransformColumns(List, {"List", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
//this is the second table after transforming

    Join = Table.NestedJoin(Source1,{"Student Code"},Extract,{"Person ID"},"Table",JoinKind.LeftOuter),
// here are merged two tables from above

    Expand = Table.ExpandTableColumn(Join, "Table", {"Part", "SUA", "Pass", "Fail", "Total marks", "List"}, {"Part", "SUA", "Pass", "Fail", "Total marks", "List"})
// exapanded column table after merge
You can't merge table1 and table2 with their original layouts because it doesn't make sense that is why table2 is transformed to the proper layout (see step: Extract) then you can merge Source1 (Table1) and transformed table2 (Extract)
Ok, thank you for the explanation.
 
Upvote 0

Forum statistics

Threads
1,223,789
Messages
6,174,579
Members
452,573
Latest member
Cpiet

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