Forum

Sum other columns b...
 
Notifications
Clear all

Sum other columns based on distinct values in first column

4 Posts
3 Users
0 Reactions
265 Views
(@fuzzywuzzy)
Posts: 1
New Member
Topic starter
 

Hi,

 

I have the following set of data.

Material    Plant       Qty

1001         9000      542

1001         9000      200

1002         9000      599

1001         9001      202

1002         9000      212

1002         9000      300

1001         9003      454

 

Below is the end result i hope to achieve: Distinct Material, showing sum distinct of plant.

I tried Group By + Power Pivot to achieve it. But was hoping if I can do everything in Power Query.

 

Material    Plant                                     Qty

1001         27,004( 9000+9001+9003)     542+202+454

1002         9000                                     599+212+300

 

Any help is greatly appreciated. Thanks in advance!

 
Posted : 19/06/2020 5:03 am
(@bluesky63)
Posts: 162
Estimable Member
 

Hi,

You desired display not very clear (what is 27,004( 9000+9001+9003) ?)

Attached is the power query output,  please take a look,  if it is not what you want,  please attached a sample output of final table

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material", Int64.Type}, {"Plant", Int64.Type}, {"Qty", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Material", "Plant"}, {{"Total Qty", each List.Sum([Qty]), type number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Material", Order.Ascending}, {"Plant", Order.Ascending}})
in
#"Sorted Rows"

 
Posted : 19/06/2020 7:26 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi,

The total for Plant where Material is 1001 should be 36004, and where Material is 1002 it should be 27000?

You haven't said where you get this data from so I've had to enter it as a table in Excel.  You can enter this into the Advanced Editor:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material", Int64.Type}, {"Plant", Int64.Type}, {"Qty", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Material"}, {{"Plant", each List.Sum([Plant]), type number}, {"Qty", each List.Sum([Qty]), type number}})
in
#"Grouped Rows"

Regards

Phil

 
Posted : 19/06/2020 8:40 am
(@bluesky63)
Posts: 162
Estimable Member
 

Hi,

may be this is what you wanted,   for Plant 1001 9000, two records you only take unique number,  i.e. 9000,  like wise for 1002 9000,  only 9000 was taken.  the M Code as below

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material", Int64.Type}, {"Plant", Int64.Type}, {"Qty", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Number.ToText([Material])&"-"&Number.ToText([Plant])),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Material", "Custom"}, {{"Count", each List.Sum([Qty]), type number}}),
#"Extracted Text After Delimiter" = Table.TransformColumns(#"Grouped Rows", {{"Custom", each Text.AfterDelimiter(_, "-"), type text}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Extracted Text After Delimiter",{{"Custom", type number}}),
#"Grouped Rows1" = Table.Group(#"Changed Type1", {"Material"}, {{"Count", each _, type table [Material=number, Custom=number, Count=number]}}),
#"Aggregated Count" = Table.AggregateTableColumn(#"Grouped Rows1", "Count", {{"Custom", List.Sum, "Sum of Custom"}, {"Count", List.Sum, "Sum of Count"}}),
#"Renamed Columns" = Table.RenameColumns(#"Aggregated Count",{{"Sum of Custom", "Plant_Qty"}, {"Sum of Count", "Qty"}})
in
#"Renamed Columns"

 
Posted : 19/06/2020 9:47 pm
Share: