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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Automatic sum of row values with dynamic number of columns

Hello,

I have the following table in Power Query and often there are new (data type: whole number) columns added or removed from this table which is stored in an Excel spreadsheet. In Power Query, i have added a step to sum all the values on each row, except for the date. However, this step needs to be manually updated every time that a new column is added/removed.

Is it possible to automate this Sum column so that this step no longer needs to be updated manually in Power Query?

This table is shown in a table visualization in Power BI. Maybe a formula to always sum all values on each row (irrespective of the number of columns) and exclude Date column? Any help is much appreciated!

 

For example, this is the original table:

DateStockReturnsDefectiveSoldExcessExpired
08/08/20223482811
01/01/202269176210
27/05/202225103612
20/10/202293163512
07/08/202256157315
12/08/202222102311
17/08/202278229319
20/08/202233134713
22/08/202263187218
29/08/202233148914
07/09/202227175217
16/09/202237223621

 

A few days later, a new column has been added, so now the sum column needs to automatically include this new column.

DateStockReturnsDefectiveSoldExcessExpiredOverdue
08/08/202234828119
01/01/2022691762105
27/05/2022251036126
20/10/2022931635126
07/08/2022561573154
12/08/2022221023112
17/08/2022782293198
20/08/2022331347133
22/08/2022631872183
29/08/2022331489143
07/09/2022271752173
16/09/2022372236217
1 ACCEPTED SOLUTION

Hi @Anonymous,

 

You could keep a copy of the original table and then merge the sum column into it, however using this idea will keep to one step in one query,

 

= Table.AddColumn(#"Changed Type", "Custom", each List.Sum(Record.FieldValues(Record.SelectFields(_, List.Difference(Table.ColumnNames(#"Changed Type"), {"Date"})))))

 

where #"Changed Type" is the previous step. This will sum up the value for every column which isn't "Date".

 

 

View solution in original post

5 REPLIES 5
LinVen
Helper I
Helper I

Hi 

This seems so close to a perfect solution for me, but i need to exclude an employee number field as well as the date

Any way to tell this to exclude 2x columns?

 

I tried:
= Table.AddColumn(#"Renamed Columns", "Custom", each List.Sum(Record.FieldValues(Record.SelectFields(_, List.Difference(Table.ColumnNames(#"Renamed Columns"), {"Date"}, {Employee ID})))))

(my prev step being a renamed columns) But no joy...

 

Hi @LinVen ,

 

Try putting both column names in the same list, so:

 

= Table.AddColumn(#"Renamed Columns", "Custom", each List.Sum(Record.FieldValues(Record.SelectFields(_, List.Difference(Table.ColumnNames(#"Renamed Columns"), {"Date", "Employee ID"} )))))

 

 

Change {"Date"} to {"Date", "Employee ID"}

AntonioM
Solution Sage
Solution Sage

Hi @Anonymous ,

 

For this I think you would want to "unpivot other columns". This will keep the date columns, unpivot and then sum up all the values (no matter what the columns are). 

This is assuming you'll never have more than one row for any date, as it will sum them all into one if you do.

 

let
    Source = (your source here)
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Stock", Int64.Type}, {"Returns", Int64.Type}, {"Defective", Int64.Type}, {"Sold", Int64.Type}, {"Excess", Int64.Type}, {"Expired", Int64.Type}, {"Overdue", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Date"}, {{"Sum", each List.Sum([Value]), type number}})
in
    #"Grouped Rows"

 

 

  • Unpivot other

AntonioM_0-1661509266096.png

  • Group by

 

AntonioM_1-1661509321673.png

AntonioM_2-1661509333538.png

 

 

Anonymous
Not applicable

@AntonioM Thanks for your reply! I tried your method and it works but is it possible to keep all the other columns and have the new Sum column added at the far right side?

 

This is the original table in Power Query:

MakeItReal_0-1661519349642.png

 

The result which is missing the other columns:

MakeItReal_1-1661519382113.png

 

Hi @Anonymous,

 

You could keep a copy of the original table and then merge the sum column into it, however using this idea will keep to one step in one query,

 

= Table.AddColumn(#"Changed Type", "Custom", each List.Sum(Record.FieldValues(Record.SelectFields(_, List.Difference(Table.ColumnNames(#"Changed Type"), {"Date"})))))

 

where #"Changed Type" is the previous step. This will sum up the value for every column which isn't "Date".

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.