Forum

Sum with Dynamic Co...
 
Notifications
Clear all

Sum with Dynamic Column List

6 Posts
3 Users
0 Reactions
952 Views
(@tanyamc)
Posts: 11
Active Member
Topic starter
 

I need to add columns that sum based on column name. The columns present may vary but will be 4 digit codes (GL account numbers).

I have a solution that requires unpivot and merge for each combination, but I have six combinations and 60K rows so I am looking for a way to do it with lists instead in hopes of improved performance.

I need to get a list for the List.Sum function as a list rather than as text.

Example: Sum values by row for all columns that start with 47 and end in 1 (47x1 column)

Not dynamic: = Table.AddColumn(Source, "Addition", each List.Sum({[4701], [4721], [4751]}), type number)

I can get a dynamic list in a step to call, but it brings in text values "4701", "4751", "4781" rather than [4701], [4751], [4781] format for the List.Sum to accept it.

I can't find a way to convert a list from text into just a string of values.

I attached a sample data with hard coded examples. Any suggestions for how to do this with lists/efficiently, would be appreciated. Thanks.

 
Posted : 20/03/2022 3:29 pm
Riny van Eekelen
(@riny)
Posts: 1185
Member Moderator
 

You didn't attach a file but I believe it was clear what you needed.

Google a bit and come to this site:

https://www.bookkempt.com/2019/03/sum-all-except-certain-columns-in-power.html

Used the method described to construct the attached file and learned a great deal from it myself. Perhaps you can use it in your own.

 
Posted : 21/03/2022 5:29 am
(@tanyamc)
Posts: 11
Active Member
Topic starter
 

Thank you! I thought I had attached a file! I appreciate your reply and am headed to check it out!

 
Posted : 21/03/2022 3:46 pm
(@debaser)
Posts: 836
Member Moderator
 

With a list of matching column names, you can get the sum using something like:

List.Sum(Record.ToList(Record.SelectFields(_, MatchingColumnNames)))

 
Posted : 22/03/2022 5:30 pm
(@tanyamc)
Posts: 11
Active Member
Topic starter
 

Thank you Velouria. I can't mark two answers 🙁

 
Posted : 23/03/2022 10:47 am
(@debaser)
Posts: 836
Member Moderator
 

No problem - just wanted to mention it as an alternative. 🙂

 
Posted : 23/03/2022 11:59 am
Share: