How to join two queries preventing double values

BeeJay

New Member
Joined
Jul 26, 2005
Messages
11
I will try to describe it easily.

I'm trying to combine 2 queries into one so I can create a pivot table out of it.

I have made one query from my first Table1 (see below) with Claimfigures for vessels and one query from my second table below with Premiumfigures per vessel.

My problem is that per year each vessel can only have one premium listed per Underwiter, however each vessel can have per year multiple claims. If I combine the two queries I get for each vessel for each claim also the premium listed. So a vessel that has two claims, will also show two times the premiums, which should not be the case. I can't figure out how to solve this.

1588860197402.png


1588860220867.png


Would much appreciate if somebody can put me on the right track.
 

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.
Please use the XL2BB function to upload these samples so that we don't have to retype your data. Cannot manipulate data in pictures.
 
Upvote 0
Ah, I'm sorry about that.

Example.xlsx
ABCDEFGHIJKL
1Table1DataClaimsTable2Data premiums
2
3YEARVESSELNRNET CLAIMYEARUNDERWRITERSHAREVESSELNRPREMIUM
42017A1 - 2017X60%A1 2.500
52017B2 2.000 2017X60%B2 2.750
62017B2 3.000 2017X60%C3 1.750
72017C3 - 2018X60%A1 2.900
82018A1 1.750 2018X60%B2 3.150
92018B2 3.000 2018X60%C3 2.150
102018B2 4.000 2019X60%A1 3.500
112018B2 5.000 2019X60%B2 3.400
122018C3 - 2019X60%C3 4.000
132019A1 - 2017Y40%A1 2.850
142019B2 5.000 2017Y40%B2 3.100
152019C3 20.000 2017Y40%C3 2.100
162018Y40%A1 3.250
172018Y40%B2 3.500
182018Y40%C3 2.500
192019Y40%A1 3.850
202019Y40%B2 3.750
212019Y40%C3 4.350
Blad1
 
Upvote 0
If this looks like what you are trying to achieve, then the explanation follows.

Book1
ABCDE
1YEARUNDERWRITERVESSELPREMIUMTotal Claims
22017XA25006051
32018XA29006054
42019XA35006057
52017YA28506051
62018YA32506054
72017XB275012102
82018XB315012108
92019XB340012114
102017YB310012102
112017XC17506051
122018XC21506054
132019XC40006057
142017YC21006051
152018YB350012108
162018YC25006054
172019YA38506057
182019YB375012114
192019YC43506057
Sheet2


Brought both tables into Power Query. Merged the two tables. Then accumulated the claims. Advise if this presentation works for you and if so, then will supply more details.
 
Upvote 0
Dear Alan,

Thanks for your efforts, it much looks like what I'm trying to get, however the totals in the column Total Claims are incorrect. Below the correct output
YEARUNDERWRITERVESSELPREMIUMTotal Claims
2017XA 2.500 -
2017YA 2.850 -
2017XB 2.750 5.000
2017YB 3.100 5.000
2017XC 1.750 -
2017Y 2.100 -
2018XA 2.900 1.750
2018YA 3.250 1.750
2018XB 3.150 12.000
2018YB 3.500 12.000
2018XC 2.150 -
2018YC 2.500 -
2019XA 3.500 -
2019YA 3.850 -
2019XB 3.400 5.000
2019YB 3.750 5.000
2019XC 4.000 20.000
2019YC 4.350 20.000
 
Upvote 0
Book1
ABCDE
1YEARUNDERWRITERVESSELPREMIUMTotal Claims
22017XA $2,500.00 $-
32017YA $2,850.00 $-
42017XB $2,750.00 $5,000.00
52017YB $3,100.00 $5,000.00
62017XC $1,750.00 $-
72017YC $2,100.00 $-
82018XA $2,900.00 $1,750.00
92018YA $3,250.00 $1,750.00
102018XB $3,150.00 $12,000.00
112018XC $2,150.00 $-
122019XA $3,500.00 $-
132019XB $3,400.00 $5,000.00
142019XC $4,000.00 $20,000.00
152018YB $3,500.00 $12,000.00
162018YC $2,500.00 $-
172019YA $3,850.00 $-
182019YB $3,750.00 $5,000.00
192019YC $4,350.00 $20,000.00
Sheet2


Here is the Mcode for the merged tables

VBA Code:
let
    Source = Table.NestedJoin(Table2, {"YEAR", "VESSEL"}, Table1, {"YEAR", "VESSEL"}, "Table1", JoinKind.FullOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(Source, "Table1", {"NET CLAIM"}, {"Table1.NET CLAIM"}),
    #"Grouped Rows" = Table.Group(#"Expanded Table1", {"YEAR", "UNDERWRITER", "VESSEL", "PREMIUM"}, {{"Total Claims", each List.Sum([Table1.NET CLAIM]), type number}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Grouped Rows",{{"PREMIUM", Currency.Type}, {"Total Claims", Currency.Type}})
in
    #"Changed Type"
 
Upvote 0
Rich (BB code):
let
    Tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Merge = Table.NestedJoin(Tbl1,{"YEAR", "VESSEL", "NR"},Tbl2,{"YEAR", "VESSEL", "NR"},"Tbl2",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Merge, "Tbl2", {"UNDERWRITER", "SHARE", "PREMIUM"}, {"UNDERWRITER", "SHARE", "PREMIUM"}),
    TSC = Table.SelectColumns(Expand,{"YEAR", "UNDERWRITER", "VESSEL", "PREMIUM", "NET CLAIM"}),
    Group = Table.Group(TSC, {"YEAR", "UNDERWRITER", "VESSEL"}, {{"Premium", each List.Sum([PREMIUM]), type number}, {"Claim", each List.Sum([NET CLAIM]), type number}}),
    Sort = Table.Sort(Group,{{"YEAR", Order.Ascending}, {"VESSEL", Order.Ascending}, {"UNDERWRITER", Order.Ascending}})
in
    Sort

pic1.png
 
Upvote 0

Forum statistics

Threads
1,223,793
Messages
6,174,624
Members
452,575
Latest member
Fstick546

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