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

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

Reply
Anonymous
Not applicable

Add row of Total revenue and total expenses with their calculated amount using m language

hello i have a table combining expenses and revenues with their values in the table below as a small example:

 

TypeLevel 1Level 2Amount
Revenue          Sales    Product A100     
RevenueSalesProduct B300
ExpensePayroll Expenses           Hr Cost20
ExpensePayroll ExpensesAccountant15
ExpenseBusiness ExpensesAttorney Fees         5
ExpenseBusiness ExpensesConsultant Fees5
ExpenseFinancial FeesFee A30
ExpenseFinancial FeesFee B50

 

i want to manually add total revenue and total expenses but i want the amount of total revnue to be sum(amount) if Type = Revenue and total expense amount to be sum(amount) if type="Expense",

 

so how can i do it?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I found a workaround way.

first import your data with powerquery . Then copy it as a new query. and groupby it . Then tranform data.

all done by mouse click.

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}, {"Level 1", type text}, {"Level 2", type text}, {"Amount", Int64.Type}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Type", Text.Trim, type text}}),
#"Grouped Rows" = Table.Group(#"Trimmed Text", {"Type"}, {{"Amount", each List.Sum([Amount]), type number}}),
#"Replaced Value" = Table.ReplaceValue(#"Grouped Rows","Revenue","Total Revenue",Replacer.ReplaceText,{"Type"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Expense","Total Expense",Replacer.ReplaceText,{"Type"}),
#"Transposed Table" = Table.Transpose(#"Replaced Value1"),
#"Added Custom" = Table.AddColumn(#"Transposed Table", "Custom", each try [Column1]-[Column2] otherwise "Net Profit"),
#"Transposed Table1" = Table.Transpose(#"Added Custom"),
#"Renamed Columns" = Table.RenameColumns(#"Transposed Table1",{{"Column1", "Type"}, {"Column2", "Amount"}})
in
#"Renamed Columns"

Youngli_0-1662106861073.png

second append it to orginial data.

Youngli_1-1662106962292.png

 

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

I found a workaround way.

first import your data with powerquery . Then copy it as a new query. and groupby it . Then tranform data.

all done by mouse click.

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}, {"Level 1", type text}, {"Level 2", type text}, {"Amount", Int64.Type}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Type", Text.Trim, type text}}),
#"Grouped Rows" = Table.Group(#"Trimmed Text", {"Type"}, {{"Amount", each List.Sum([Amount]), type number}}),
#"Replaced Value" = Table.ReplaceValue(#"Grouped Rows","Revenue","Total Revenue",Replacer.ReplaceText,{"Type"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Expense","Total Expense",Replacer.ReplaceText,{"Type"}),
#"Transposed Table" = Table.Transpose(#"Replaced Value1"),
#"Added Custom" = Table.AddColumn(#"Transposed Table", "Custom", each try [Column1]-[Column2] otherwise "Net Profit"),
#"Transposed Table1" = Table.Transpose(#"Added Custom"),
#"Renamed Columns" = Table.RenameColumns(#"Transposed Table1",{{"Column1", "Type"}, {"Column2", "Amount"}})
in
#"Renamed Columns"

Youngli_0-1662106861073.png

second append it to orginial data.

Youngli_1-1662106962292.png

 

Anonymous
Not applicable

I just have one more question @Anonymous  what if i have dates? in the original data and i want to filter by date? what can i do

 

Anonymous
Not applicable

Groupby you can select multiple conditions.

Youngli_0-1662109785775.png

 

Anonymous
Not applicable

Youre a life saver man thank you very much

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column.

 

Jihwan_Kim_0-1662088705185.png

 

Total CC =
SUMX ( FILTER ( Data, Data[Type] = EARLIER ( Data[Type] ) ), Data[Amount] )

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

hello i dont want it in ddax because i want my table to look like this so i need t add them mmanually with m query and i want results like this:

Type  Level 1Level 2AmountNew Column
Revenue     SalesProduct A100100
RevenueSalesProduct B300300
ExpensePayroll ExpensesHr Cost2020
ExpensePayroll ExpensesAccountant1515
ExpenseBusiness ExpensesAttorney Fees55
ExpenseBusiness Expenses    Consultant Fees  55
ExpenseFinancial FeesFee A               3030
ExpenseFinancial FeesFee B5050
Total RevenueTotal Revenuenullnull400
Total Expense      Total Expensenullnull125
Net ProfitNet Profitnullnull275
Anonymous
Not applicable

HI I am a new to M function.

Do you mean like this? 

= Table.AddColumn(#"Trimmed Text", "GroupBy",(data)=>List.Sum(Table.SelectRows(#"Trimmed Text",(b)=>b[Type]=data[Type])[Amount]))

Youngli_0-1662083182522.png

 

Anonymous
Not applicable

hello @Anonymous  yes something like that i appreciate your help but im trying to add rows too like i want the final result to be as follow:

TypeLevel 1Level 2AmountNew Column
RevenueSalesProduct A100100
RevenueSalesProduct B300300
ExpensePayroll ExpensesHr Cost2020
ExpensePayroll ExpensesAccountant1515
ExpenseBusiness ExpensesAttorney Fees55
ExpenseBusiness ExpensesConsultant Fees55
ExpenseFinancial FeesFee A3030
ExpenseFinancial FeesFee B5050
Total RevenueTotal Revenuenullnull400
Total ExpenseTotal Expensenullnull125
Net ProfitNet Profitnullnull275

 

as you see i want to add 3 new rows called Total revenue , Total Expenses and net profit and then do the calculations 

Helpful resources

Announcements
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.