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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Mars3442
Helper I
Helper I

Summing Columns Based on Column Header String

Hi everyone,

I want to ask a help for solving my problem

I have a table of Raw Data like this

Mars3442_1-1700130815867.png

I need to sum the value per row when the header contains "." and "Qty" It will create a new column "TOTAL"

After that, I want to calculate the GAP = QTY PO CS - TOTAL

 

My expectation table

Mars3442_2-1700131033369.png

 

What M Code should I write to get my expectations?

 

Please help me to solve this problem

 

Thank you..

3 REPLIES 3
j_ocean
Helper V
Helper V

If the columns are fixed then just highlight the columns you want to sum and click add column > sum. Then you can click the two other columns and add column > subtract.

 

If your columns are *not* fixed then your table is not organized properly.

AlienSx
Super User
Super User

Hello, @Mars3442 

let
    Source = your_table,
    dots = List.Buffer(List.Select(Table.ColumnNames(Source), each Text.Contains(_, "."))),
    total = Table.AddColumn(
        Source, "TOTAL", 
        each List.Sum(
            Record.FieldValues(
                Record.SelectFields(_, dots)
            )
        )
    ),
    gap = Table.AddColumn(total, "GAP", each [QTY PO CS] - [TOTAL])
in
    gap

Hi thank you for your solution but I get an error

I modify your solution like this

List = List.Buffer(List.Select(Table.ColumnNames(#"Previous Step"), each Text.Contains(_, ".")and Text.Contains(_, "Qty CS"))),
Total = Table.AddColumn( List, "Total Reason QTY CS", each List.Sum(Record.FieldValues(Record.SelectFields(_, List))))

 

but I got an error

Expression.Error: We cannot convert a value of type List to type Table.
Details:
Value=[List]
Type=[Type]

 

what should I do?

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.