Better Power Query Formula to get a list of table columns name ?

thomsont

New Member
Joined
Nov 24, 2014
Messages
1
Currently, I am using the following steps in Power Query to get a list of columns for List.Sum function in a later step.
There is the Table format

Date Store Product Discount.A Discount.A% Discount.B Discount.B% Discount.C Discount.C%


Discount.A, Discount.B and Discount.C might not appear all the time (This is a sample only, the real report with much more columns)

The current working steps as below:

Source = SalesReport
ColumnsList = Table.FromList(Table.ColumnNames(Source), Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Discount Columns" = Table.ToList(Table.SelectRows(ColumnsList , each Text.StartsWith([Column1], "Discount.") and not Text.EndsWith([Column1], "%"))),
TotalDiscountColumns = Table.AddColumn(Source,"Total Discount", each List.Sum(Record.FieldValues(Record.SelectFields(_, #"Discount Columns"))),Currency.Type),

I saw the following function online using List.Difference as

List.Difference(List.Contains(Table.ColumnNames(Source),{"Discount."}), {"%"})

However, it appears List.Difference not working very well on the % symbol.

PS: Pivot and UnPivot not working very well as there are numbers of rows and columns in source file.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
i got two errors
A cyclic reference was encountered during evaluation
Token comma expected

I suggest to post a proper M-code and wrap it in CODE tags
 
Upvote 0
Source = SalesReport
ColumnsList = Table.FromList(Table.ColumnNames(Source), Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Discount Columns" = Table.ToList(Table.SelectRows(ColumnsList , each Text.StartsWith([Column1], "Discount.") and not Text.EndsWith([Column1], "%"))),
TotalDiscountColumns = Table.AddColumn(Source,"Total Discount", each List.Sum(Record.FieldValues(Record.SelectFields(_, #"Discount Columns"))),Currency.Type),
Proposition
Code:
let
    Source = SalesReport,
    ColumnsToSum = List.Buffer(List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "Discount") and not Text.EndsWith(_, "%"))),
    TotalDiscountColumns = Table.AddColumn(Source, "Total Discount", each List.Sum(Record.ToList(Record.SelectFields(_,ColumnsToSum))))
in
    TotalDiscountColumns
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,937
Members
448,534
Latest member
benefuexx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top