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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply

If Column header start with text "" then add values under the column header for each row in M Query

I created a pivot table in Query Editor using Pivot Column. I have financial accounts as columns and I have a project label for each row. What I want to do is create two new columns: one for Revenue and another for Cost of Revenue for each project. I know that financial accounts starting with "4" are Revenue Accounts and financial accounts starting with "5" are Costs accounts. Is there an if statement in M Query that computes values based on these conditions? 

2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

See the code below

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRS0lEyNDAAkmbmYCaIsICzQIQRlBWrE63k7AIWAImYmoGETeHqTVE0gdTHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Name" = _t, #"40567-service income" = _t, #"40785 - product income" = _t, #"40037 - additional income" = _t, #"50678 - materials cost" = _t, #"54678 - service cost" = _t, #"56890 - driver fee" = _t, #"56768 - gasoline fee" = _t, #"56780 - additional fee" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Name", type text}, {"40567-service income", Int64.Type}, {"40785 - product income", Int64.Type}, {"40037 - additional income", Int64.Type}, {"50678 - materials cost", Int64.Type}, {"54678 - service cost", Int64.Type}, {"56890 - driver fee", Int64.Type}, {"56768 - gasoline fee", Int64.Type}, {"56780 - additional fee", Int64.Type}}),
    ListOfColumns = List.Buffer(Table.ColumnNames(Source)),
    ColumnCount = Table.ColumnCount(Source) - 1,
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Revenue", each List.Sum(List.Transform({0..ColumnCount}, (x)=> if Text.StartsWith(ListOfColumns{x},"4") then Record.ToList(_){x} else null))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Cost of Revenue", each List.Sum(List.Transform({0..ColumnCount}, (x)=> if Text.StartsWith(ListOfColumns{x},"5") then List.RemoveLastN(Record.ToList(_),1){x} else null)))
in
    #"Added Custom1"

View solution in original post

dufoq3
Super User
Super User

Hi @smallfires0628, similar approach here.

 

Result (added 2 columns to sample table)

dufoq3_0-1709492481376.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRS0lEyNDAAkmbmYCaIsICzQIQRlBWrE63k7AIWAImYmoGETeHqTVE0gdTHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Name" = _t, #"40567-service income" = _t, #"40785 - product income" = _t, #"40037 - additional income" = _t, #"50678 - materials cost" = _t, #"54678 - service cost" = _t, #"56890 - driver fee" = _t, #"56768 - gasoline fee" = _t, #"56780 - additional fee" = _t]),
    // You can delete this step if you have columns with correct types.
    ChangedToNumbers = Table.TransformColumns(Source, {}, each try Number.From(_) otherwise Text.From(_)),
    ColumnNames = List.Buffer(List.Transform(Table.ColumnNames(ChangedToNumbers), Text.Trim)),
    StepBack = ChangedToNumbers,
    Ad_Revenue = Table.AddColumn(StepBack, "Revenue", each List.Sum(Record.ToList(Record.SelectFields(_, List.Select(ColumnNames, (x)=> Text.StartsWith(x, "4"))))), type number),
    Ad_CostOfRevenue = Table.AddColumn(Ad_Revenue, "Cost of Revenue", each List.Sum(Record.ToList(Record.SelectFields(_, List.Select(ColumnNames, (x)=> Text.StartsWith(x, "5"))))), type number)
in
    Ad_CostOfRevenue

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

4 REPLIES 4
dufoq3
Super User
Super User

Hi @smallfires0628, similar approach here.

 

Result (added 2 columns to sample table)

dufoq3_0-1709492481376.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRS0lEyNDAAkmbmYCaIsICzQIQRlBWrE63k7AIWAImYmoGETeHqTVE0gdTHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Name" = _t, #"40567-service income" = _t, #"40785 - product income" = _t, #"40037 - additional income" = _t, #"50678 - materials cost" = _t, #"54678 - service cost" = _t, #"56890 - driver fee" = _t, #"56768 - gasoline fee" = _t, #"56780 - additional fee" = _t]),
    // You can delete this step if you have columns with correct types.
    ChangedToNumbers = Table.TransformColumns(Source, {}, each try Number.From(_) otherwise Text.From(_)),
    ColumnNames = List.Buffer(List.Transform(Table.ColumnNames(ChangedToNumbers), Text.Trim)),
    StepBack = ChangedToNumbers,
    Ad_Revenue = Table.AddColumn(StepBack, "Revenue", each List.Sum(Record.ToList(Record.SelectFields(_, List.Select(ColumnNames, (x)=> Text.StartsWith(x, "4"))))), type number),
    Ad_CostOfRevenue = Table.AddColumn(Ad_Revenue, "Cost of Revenue", each List.Sum(Record.ToList(Record.SelectFields(_, List.Select(ColumnNames, (x)=> Text.StartsWith(x, "5"))))), type number)
in
    Ad_CostOfRevenue

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Vijay_A_Verma
Super User
Super User

See the code below

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRS0lEyNDAAkmbmYCaIsICzQIQRlBWrE63k7AIWAImYmoGETeHqTVE0gdTHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Name" = _t, #"40567-service income" = _t, #"40785 - product income" = _t, #"40037 - additional income" = _t, #"50678 - materials cost" = _t, #"54678 - service cost" = _t, #"56890 - driver fee" = _t, #"56768 - gasoline fee" = _t, #"56780 - additional fee" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Name", type text}, {"40567-service income", Int64.Type}, {"40785 - product income", Int64.Type}, {"40037 - additional income", Int64.Type}, {"50678 - materials cost", Int64.Type}, {"54678 - service cost", Int64.Type}, {"56890 - driver fee", Int64.Type}, {"56768 - gasoline fee", Int64.Type}, {"56780 - additional fee", Int64.Type}}),
    ListOfColumns = List.Buffer(Table.ColumnNames(Source)),
    ColumnCount = Table.ColumnCount(Source) - 1,
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Revenue", each List.Sum(List.Transform({0..ColumnCount}, (x)=> if Text.StartsWith(ListOfColumns{x},"4") then Record.ToList(_){x} else null))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Cost of Revenue", each List.Sum(List.Transform({0..ColumnCount}, (x)=> if Text.StartsWith(ListOfColumns{x},"5") then List.RemoveLastN(Record.ToList(_),1){x} else null)))
in
    #"Added Custom1"
some_bih
Super User
Super User

Hi @smallfires0628 

possible solution for new column below 

= if Text.StartsWith([Account], "4") then [YourAmountColumn]
else if Text.StartsWith([Account], "5") then [YourAmountColumn]
else null

 

Link for this function





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!






My table looks something like this: 

 

Project Name40567-service income40785 - product income40037 - additional income50678 - materials cost54678 - service cost56890 - driver fee56768 - gasoline fee56780 - additional feeNew column - RevenueNew Column - Cost of Revenue
AB10067101810102010  
CD20056151815101010  

If I understand correctly, the text.startswith function will look at a specific column (let's say the service income column) and add amounts based on if the values underneath it match a condition. What I want is something like this: if Column header starts with "4" then add values.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors