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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
JuanSombrero
Frequent Visitor

Dynamically select columns to sum Get&Transform

Hi all,

 

I have a number of columns (date, item, sales rep) and multiple turnover columns. I want to dynamically create a list of all columns that contain the words "turnover" or "sales" and want to sum only these columns in an additional column. Because the turnover columns tend to change, I need to dynamically capture the columns containing these words, and pass this list as the argument into List.Sum.

 

Any ideas?

 

thx,

Juan

1 ACCEPTED SOLUTION
v-sihou-msft
Microsoft Employee
Microsoft Employee

@JuanSombrero

 

In this scenario, you can unpivot those columns first, filter columns contains "turnover" or "sales". And sum those values together.

 

I have a sample table like below:

 

45.PNG

 

1. Unpivot all columns except the group column ("Year" column).

 

46.PNG

 

2. Then filter matched rows.

 

43.PNG

 

4. Sum Values on Year level.

 

42.PNG

 

If you want to show the "Filtered Column Total" along with all original columns, you can merge it back to source table.

 

See my entire Power Query.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwNFPSUTIEYQMDCAmhgISpUqwOWIk5kGMEwmA5IwMoBSTMlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Year = _t, Sales1 = _t, Sales2 = _t, Turnover1 = _t, Turnover2 = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Sales1", Int64.Type}, {"Sales2", Int64.Type}, {"Turnover1", Int64.Type}, {"Turnover2", Int64.Type}, {"Amount", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Year"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Columns", each Text.Contains([Attribute], "Turnover") or Text.Contains([Attribute], "Sales")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Year"}, {{"Filtered Column Total", each List.Sum([Value]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Year"},#"Grouped Rows",{"Year"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", {"Filtered Column Total"}, {"NewColumn.Filtered Column Total"})
in
    #"Expanded NewColumn"

40.PNG

 

 

41.PNG

 

Regards,

View solution in original post

2 REPLIES 2
bwilkey
Frequent Visitor

Or for the column list you could simply use:

 

#"ColumnList" = List.Select(Table.ColumnNames(Source), each Text.Contains( _, "Turnover", Comparer.OrdinalIgnoreCase))

Or

#"ColumnList" = List.Select(Table.ColumnNames(Source), each Text.StartsWith( _, "Turnover", Comparer.OrdinalIgnoreCase))

 

Or Comparer.Ordinal if case matters, and then use ColumnList as the List you pass to other functions.

v-sihou-msft
Microsoft Employee
Microsoft Employee

@JuanSombrero

 

In this scenario, you can unpivot those columns first, filter columns contains "turnover" or "sales". And sum those values together.

 

I have a sample table like below:

 

45.PNG

 

1. Unpivot all columns except the group column ("Year" column).

 

46.PNG

 

2. Then filter matched rows.

 

43.PNG

 

4. Sum Values on Year level.

 

42.PNG

 

If you want to show the "Filtered Column Total" along with all original columns, you can merge it back to source table.

 

See my entire Power Query.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwNFPSUTIEYQMDCAmhgISpUqwOWIk5kGMEwmA5IwMoBSTMlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Year = _t, Sales1 = _t, Sales2 = _t, Turnover1 = _t, Turnover2 = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Sales1", Int64.Type}, {"Sales2", Int64.Type}, {"Turnover1", Int64.Type}, {"Turnover2", Int64.Type}, {"Amount", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Year"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Columns", each Text.Contains([Attribute], "Turnover") or Text.Contains([Attribute], "Sales")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Year"}, {{"Filtered Column Total", each List.Sum([Value]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Year"},#"Grouped Rows",{"Year"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", {"Filtered Column Total"}, {"NewColumn.Filtered Column Total"})
in
    #"Expanded NewColumn"

40.PNG

 

 

41.PNG

 

Regards,

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

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