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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
jaiken
Frequent Visitor

Summing Columns Based on Column Header String

Hello, I am trying to create a couple columns here. They specifically need to be a sum total of the columns that have the header beginning with "2021" and "2022"

 

I know I can just select the columns and do a simple sum, but as more columns come as 2022 continues, I need Power Query to automatically add them. So right now, it's simply 202201, 202202, and 202203 that need to be added together to create a "Total 2022" column. Once April starts and I load a 202204 column, it will need to automatically add the column 202204 to the "Total 2022" column. 

 

Item202110202111202112202201202202202203
A111222
B222444
C333666
D444888
E555101010
1 ACCEPTED SOLUTION
artemus
Microsoft Employee
Microsoft Employee

The new column would like like:

List.Sum(List.Transform(List.Select(Record.FieldNames(_), each Text.StartsWith(_, "2021") or Text.StartsWith(_, "2022")), (name) => Record.Field(_, name)))

 

Alterativly,

1. Transpose the table

2. select rows where the first column starts with 2021 or 2022

3. transpose the table back.

View solution in original post

4 REPLIES 4
CoffeeAddictMk2
Frequent Visitor

Thanks artemus.  does the job i need too...

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Solution file uploaded to - https://1drv.ms/x/s!Akd5y6ruJhvhuSUpknlbzmi8ikqU?e=RzXqNw 

The solution is completely dynamic - Remove add new months, years or rows. 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Item"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Year Title", each Text.Start([Attribute],4)&" Total"),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Item", "Year Title"}, {{"Sum", each List.Sum([Value]), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Year Title"]), "Year Title", "Sum", List.Sum)
in
    #"Pivoted Column"

 

artemus
Microsoft Employee
Microsoft Employee

The new column would like like:

List.Sum(List.Transform(List.Select(Record.FieldNames(_), each Text.StartsWith(_, "2021") or Text.StartsWith(_, "2022")), (name) => Record.Field(_, name)))

 

Alterativly,

1. Transpose the table

2. select rows where the first column starts with 2021 or 2022

3. transpose the table back.

Very nice... I wrote it this way in order to get a 2022 Column and it works very nicely: 

 

List.Sum(List.Transform(List.Select(Record.FieldNames(_), each Text.StartsWith(_, "2022")), (name) => Record.Field(_, name)))

 

Thank you for your fast work.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors