Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
AlexJones
New Member

Sum of month columns by year

Hello,

 

I'm new with power query and I try to do the sum of each month column by year.

 

For example, I have columns : jan. 2025, feb. 2025, ..., dec. 2025 and I want to sum all by row inside a 2025 column.

Same for 2026, 2027...

 

I tried this :

 

= List.Accumulate({Number.From(#"ParamYear")+1..2040}, #"Source", (state, current) => Table.AddColumn(state, Number.ToText(current), each List.Sum(List.Transform({"jan.", "feb.", "mar.", "apr.", "may", "june", "july", "aug.", "sept.", "oct.", "nov.", "dec."}, each Table.Column(state, _ & " " & Number.ToText(current))))))

 

 

I think I am close to the solution, I tried to implement this solution

 

Thanks you in advance,

 

Alex

1 ACCEPTED SOLUTION

I find a solution before I see your comment :

List.Accumulate({2022..2031}, #"Source", (state, current) => Table.AddColumn(state, Number.ToText(current), (record) => List.Sum(List.Transform({"jan.", "feb.", "mar.", "apr.", "may", "june", "july", "aug.", "sept.", "oct.", "nov.", "dec."}, (month) => Record.FieldOrDefault(record, month & " " & Number.ToText(current), 0)))))

It was easier by not using each _ but only lambda with specified arg name.

 

Thanks all !

 

View solution in original post

6 REPLIES 6
AlexJones
New Member

Example of the dataset :

AlexJones_0-1704804198407.png

If possible, I want also to be able to do the sum by year if a user delete a column like "mar. 2024", the sum need to be done on other existing columns

 

Wouldn't it make more sense to transform the table so that you had 4 Columns? ID, Month, Year and Value. It is easy to transform it this way and you can easily do data manipulations in Dax etc....  

Data come from an export of another software, I can't change the format. And there is a lot of other columns that are processed to generate the result.

Hello, @AlexJones first unpivot all columns but ID. Then

 - to get totals by mon. year: group by that column

 - to get totals by year: split "mon. year" column by space and group by new "year" column. 

I find a solution before I see your comment :

List.Accumulate({2022..2031}, #"Source", (state, current) => Table.AddColumn(state, Number.ToText(current), (record) => List.Sum(List.Transform({"jan.", "feb.", "mar.", "apr.", "may", "june", "july", "aug.", "sept.", "oct.", "nov.", "dec."}, (month) => Record.FieldOrDefault(record, month & " " & Number.ToText(current), 0)))))

It was easier by not using each _ but only lambda with specified arg name.

 

Thanks all !

 

zenisekd
Super User
Super User

Could you share a sample / printscreen of your dataset in PQ?

Helpful resources

Announcements
ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors
Top Kudoed Authors