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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Remaining values calculation

I want to add an additional row for "Remaining Commercial" that subtracts Commercial 1 and Commercial 2 from Total Commercial to avoid double counting (i.e. Remaining Commercial would read "$24,265,334" and I could take out Total Commercial from the table). 

 

Is there a formula or data modeling tool I can use to accomplish this?

 

 Capture.PNG

 

Thanks! 

1 REPLY 1
dax
Community Support
Community Support

Hi juliebenedict,

It seems that you want to add a row in data  model, right? If so, you could try to use below M code to see whether it work or not.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcoopNTAwMvNVMFTSUTIyhxCxOkgSRkAxE1MQYYkqYQwUswBJmJuAJZzzc3NTi5IzE3PAhhmbgAhLdCmQcaYgraamYKmQ/BKgKEIBUMbQ0AhMWijFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Account = _t, Cur = _t, YAGO = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}, {"Cur", Int64.Type}, {"YAGO", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "group name", each Text.Combine(List.RemoveItems(Text.ToList([Account]),{"0".."9"}))),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"group name"}, {{"sum cur", each List.Sum([Cur]), type number}, {"sum yago", each List.Sum([YAGO]), type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([group name] <> "B&M ")),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 1, 1),
    #"Added Custom1" = Table.AddColumn(#"Added Index", "Custom", each try #"Added Index" [sum cur]{[Index]}- #"Added Index" [sum cur]{[Index]-1} otherwise 0),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.1", each try #"Added Index" [sum yago]{[Index]}- #"Added Index" [sum yago]{[Index]-1} otherwise 0),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom.2", each "remaining"),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"group name", "sum cur", "sum yago", "Index"}),
    #"Filtered Rows1" = Table.SelectRows(#"Removed Columns", each ([Custom] <>0)),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows1",{{"Custom", "Cur"}, {"Custom.1", "YAGO"}, {"Custom.2", "Account"}}),
    #"Appended Query" = Table.Combine({#"Changed Type", #"Renamed Columns"})
in
    #"Appended Query"

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

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 MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.