Sum total in Power Query when pivoting a column

albertan

Board Regular
Joined
Nov 16, 2014
Messages
68
Office Version
  1. 365
Hello,
I have data in Power Query for May (5) and June (6) . I pivoted these columns now I want to add them up. When I add them I use sum formula but it generates it in M like this:

Table.AddColumn(#"Replaced Value", "Addition", each List.Sum({[5], [6]}), type number)

I want to make it dynamic so that next month when July is added the sum column is updated.

Is there any other way to sum up columns in Power Query?

Thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Given a starting lay out like
Book1
CDEFGHI
2DataJanFebMarAprMayJun
3a1,5859841,1387965,6832097,1062077,5894464,365661
4b5,6953183,2456788,2343612,4294511,7815281,615019
5c6,8826956,7031077,8307784,1004729,9024499,264326
6d6,517872,1179782,4050212,6423028,1704197,328626
7e2,5466145,2181329,269738,9370196,6181996,870404
Sheet1

One way via PQ
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    SetColTypes = Table.TransformColumnTypes(Source,{{"Data", type text}, {"Jan", type number}, {"Feb", type number}, {"Mar", type number}, {"Apr", type number}, {"May", type number}, {"Jun", type number}}),
    AddTotCol = Table.AddColumn(SetColTypes, "Sum Cols", each
  
    let
          ColNames = Table.ColumnNames(SetColTypes)
        , ListMths = {
                      "Jan", "Feb", "Mar"
                     , "Apr", "May", "Jun"
                     , "Jul", "Aug", "Sep"
                     , "Oct","Nov", "Dec"
                      }
        ,MthCols = List.Intersect({ColNames, ListMths})
        ,ColTotal = List.Sum(Record.FieldValues(Record.SelectFields(_,MthCols)))
    in
       ColTotal )
in
    AddTotCol
Results in
Book1
ABCDEFGH
1DataJanFebMarAprMayJunSum Cols
2a1,5859835011,138795635,6832090667,1062066127,5894464,36566126927,46930208
3b5,6953178623,2456783758,2343612682,4294514261,7815283541,61501890623,00135619
4c6,8826945446,7031069977,8307782964,100471949,9024492379,26432586444,68382688
5d6,5178702272,1179783112,4050207822,6423018568,1704186577,32862597829,18221581
6e2,5466140925,2181321279,2697296748,9370186276,6181989666,87040359739,46009708
Table1


Clearly, you must tweak the M according to your data.
 
Upvote 0
a bit shorter and easier
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    SetColTypes = Table.TransformColumnTypes(Source,{{"Data", type text}, {"Jan", type number}, {"Feb", type number}, {"Mar", type number}, {"Apr", type number}, {"May", type number}, {"Jun", type number}}),
    AddTotCol = Table.AddColumn(SetColTypes, "Sum Cols", each let
 Rec = Record.SelectFields( _ , {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}, MissingField.Ignore )
,ColTotal = List.Sum(Record.FieldValues(Rec))
in
 ColTotal)
in
    AddTotCol
 
Upvote 0
Hello and thank you for this reply. I use “Get data from folder” so every month incremental data is uploaded for each month. I.e. currently data is available with 2 files (May to June). Next month there will be another file. With July data and so on. I think the code you suggest will give me an error if July to Dec files are not there unless Mussingfield.ignore will take care of it? Is there a way to do it so that it dynamically gets updated with each month’s data as it comes in? Thanks
 
Upvote 0
Look at the example given. Jan-Jun data was in, and the code was able to generated the total without the months JUL-DEC being present. Indeed MissingField.Ignore takes care of it.
 
Upvote 0
Hello, because I do not have Jan - April columns it's giving an error under SetColTypes.
Is there a way to add ignore statement there or do I need to manually put only periods that are available?

Thanks
 
Upvote 0
I'm without Excel to test, however try with deleting that step.
 
Upvote 0

Forum statistics

Threads
1,223,639
Messages
6,173,496
Members
452,516
Latest member
druck21

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