powerquery - sum of column using its order - not using header

JiriH

New Member
Joined
Jun 2, 2021
Messages
2
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello, I have a silly question - I need tu sum 12 columns to make Moving Annual Total and I need to do it in power query. But headers of that columns are changing - they contains month names. So I found something, but i cannot make it work - here is syntax example I need to correct from you:

Power Query:
= Table.AddColumn(Source, "MAT", each List.Sum({ Table.ColumnNames(Source){1}, Table.ColumnNames(Source){2}}), type number)

Thank you very much in andvance to helping me with my problem :-). Jiri
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The simplest solution I have seen is this one:
  • Select all the columns you want to add up and then go Add Column > Standard > Add
  • You will get something that looks like this
    = Table.AddColumn(#"Pivoted Column", "Addition", each List.Sum({[#"28/02/2015"], [#"31/01/2015"], [#"31/12/2014"], [#"30/11/2014"], [#"31/10/2014"], [#"30/09/2014"], [#"31/08/2014"], [#"31/07/2014"], [#"30/06/2014"], [#"31/05/2014"], [#"30/04/2014"], [#"31/03/2014"]}), Int64.Type)
  • Replace everything in Red with this
    List.Sum(Record.ToList(Record.RemoveFields(_, {"Fixed", "Column", "Name"})))
  • Then replace {"Fixed","Column","Name"}, with the column names that are not going to change every time you run the report and you don't want to included in the Sum formula.
    eg. in my case I just only had 1 column to exclude which was the sales products in the column "Sales" so I the exclusion array became just {"Sales"}
If your query is including more months and you want all the months in your output but only add some of the month's it will be more complicated then this.

Reference for the above was:
Wedge > How to reference dynamic column names in Power Query (M language)
 
Upvote 0
How should the code know which fields to sum? Are they the last 12 fields, for example, or the only fields, or something else?
 
Upvote 0
There is more than 12 fields and order of them is from oldest to newest - so I can say: "sum of these 12 colums is MAT value". But headers are changing (months). And I know, that there is possibility to navigate to column by its order number (for example - Table.ColumnNames(Source){1}). But I cannot solve the right syntax fot List.Sum with it... In my example are only two columns because of lenght of it...
 
Upvote 0
Sum the last 12 columns?
工作簿1
ABCDEFGHIJKLMNO
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12Column13Column14
21234567891011121314
323456789101112131415
4345678910111213141516
54567891011121314151617
656789101112131415161718
7
8Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12Column13Column14Sum
91234567891011121314102
1023456789101112131415114
11345678910111213141516126
124567891011121314151617138
1356789101112131415161718150
Sheet1
Cell Formulas
RangeFormula
A3:N6A3=A2+1
 
Upvote 0
Code for post #5.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl"]}[Content],
    res = Table.AddColumn(Source,"Sum",each List.Sum(List.LastN(Record.ToList(_),12)))
in
    res
 
Upvote 0

Forum statistics

Threads
1,223,713
Messages
6,174,039
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