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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Anonymous
Not applicable

Summing Columns in a Table

I have a Table with a Number of Columns  Col1, Col2, Col 3 and I want to add a Column that is the Sum so I am Using List.Sum({[Col1],[Col2],[Col3])

However I want to make this Generic, so that If a Col4 is added it automatically Includes it in the sum Without having to ManualFix the Query

I have a Iist of the cols I want to add {"Col1", "Col2", "Col3"} but I cannot get this to work without Resorting to Unpvoting and Grouping which is a HUGE perfromance Hit

 

Any Suggestion as to How I Could achive this

2 ACCEPTED SOLUTIONS

Hi @Anonymous ,

 

Scenario:

If you want to keep any columns other than "EBIT",

1. Reorder Column to move all "EBIT" column to the end:

Code: Table.ReorderColumns(#"Changed Type",List.Combine({List.Select(Table.ColumnNames(PreviousStep), each not Text.Contains(_,"EBIT",Comparer.OrdinalIgnoreCase)), List.Select(Table.ColumnNames(PreviousStep), each Text.Contains(_,"EBIT",Comparer.OrdinalIgnoreCase))}))

 

then,

2. Add a custom column and add sum lastN columns:

Code: List.Sum(List.LastN(Record.ToList(_),List.Count(List.Select(Table.ColumnNames(PreviousStep), each Text.Contains(_,"EBIT",Comparer.OrdinalIgnoreCase)))))

 

 

 

View solution in original post

Anonymous
Not applicable

WOW that is Awesome. Not Intuative - I would never have figured this out

Two Fantastic Snippits of Code  that I am Going to Use Over and Over Again

 

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

WOW that is Awesome. Not Intuative - I would never have figured this out

Two Fantastic Snippits of Code  that I am Going to Use Over and Over Again

 

 

KT_Bsmart2gethe
Impactful Individual
Impactful Individual

Hi @Anonymous ,

 

Please see code below:

Replace {"Col1", "Col2" .....} with List.Select(Table.ColumnNames(PreviousStep), each Text.Contains(_, "Keyword"))

 

Regard

KT

Anonymous
Not applicable

Thanks for the Reply - You Code Does Select the Column names (actually Betterr than My Methd) but I does Rurtn the Vlaues that I Need to Pass to List.SUM - Which is what I an Strugling With

 

i.e the tableis Table.png

 

List is {"EBIT1","EBIT2,"EBIT3")

 

I need it to be {2,3,0} tso I can Pass it to List.Sum()

Hi @Anonymous ,

 

Scenario:

If you want to keep any columns other than "EBIT",

1. Reorder Column to move all "EBIT" column to the end:

Code: Table.ReorderColumns(#"Changed Type",List.Combine({List.Select(Table.ColumnNames(PreviousStep), each not Text.Contains(_,"EBIT",Comparer.OrdinalIgnoreCase)), List.Select(Table.ColumnNames(PreviousStep), each Text.Contains(_,"EBIT",Comparer.OrdinalIgnoreCase))}))

 

then,

2. Add a custom column and add sum lastN columns:

Code: List.Sum(List.LastN(Record.ToList(_),List.Count(List.Select(Table.ColumnNames(PreviousStep), each Text.Contains(_,"EBIT",Comparer.OrdinalIgnoreCase)))))

 

 

 

Helpful resources

Announcements
March Fabric Community Update

Fabric Community Update - March 2024

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

Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors