Power Query to Sum a Column and Divide by the Sum of Anothr Column

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
In Power Query I want to sum one column (C) then divide it by the sum of another column (E), therefore the result column will have the same amount repeated on each row. I don't know want to use Group BY because I want to show all the remaining columns as well as the amounts in Column C and E. How can I achieve this?

Result column calculation: (Sum of Column C) 50/10 (Sum of Column E) = 5

Example

Column CColumn EResult
1055
1025
3035
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Though group by is a possibility...

Standard UI solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column C", Int64.Type}, {"Column E", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {}, {{"Details", each _, type table [Column C=nullable number, Column E=nullable number]}, {"SumC", each List.Sum([Column C]), type nullable number}, {"SumE", each List.Sum([Column E]), type nullable number}}),
    #"Inserted Division" = Table.AddColumn(#"Grouped Rows", "Division", each [SumC] / [SumE], type number),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Division",{"SumC", "SumE"}),
    #"Expanded Details" = Table.ExpandTableColumn(#"Removed Columns", "Details", {"Column C", "Column E"}, {"Column C", "Column E"})
in
    #"Expanded Details"

1625034776870.png
1625034836474.png
 
Upvote 0
That's it! But I can't get your code to work. Error shows "Token Eof expected" and when I click show error it points to the second line where it says #"Changed Type" . I created a Excel Table with the two columns (C & E) and named it "data" then selected Blank Query and pasted your code but to no avail. Please advise.
 
Upvote 0
NVM - I was able to get it to work and see what you did. Thanks!

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column C", Int64.Type}, {"Column E", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {}, {{"Details", each _, type table [Column C=nullable number, Column E=nullable number]}, {"SumC", each List.Sum([Column C]), type nullable number}, {"SumE", each List.Sum([Column E]), type nullable number}}),
    #"Inserted Division" = Table.AddColumn(#"Grouped Rows", "Division", each [SumC] / [SumE], type number),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Division",{"SumC", "SumE"}),
    #"Expanded Details" = Table.ExpandTableColumn(#"Removed Columns", "Details", {"Column C", "Column E"}, {"Column C", "Column E"})
in
    #"Expanded Details"
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = Table.AddColumn(Source,"Result",each List.Sum(Source[Column C])/List.Sum(Source[Column E]))
in
    Custom1
1625108804908.png
 
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,006
Members
452,542
Latest member
Bricklin

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