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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kivancc94
Helper I
Helper I

Running Subtract by Group

Hi, 
I would be happy if you could help me.

I need to create a formula for Cumulative subtraction in which "Stock" should be decreased by day and by HE.Code

kivancc94_0-1679993310323.png


For example below,

The stock is 17.250. It should be decreased by "Starting Date" and  "HE.Code". Maybe i can use ALLEXCEPT(Table,HE.Code) but i could not create the formula. 

kivancc94_1-1679993394543.png

 

Regards

1 ACCEPTED SOLUTION
v-xinruzhu-msft
Community Support
Community Support

Hi @kivancc94 

You can refer to the following example.

Create two blank query,then put the following two queries to the Advanced Editor in power query Editor.

1.Create a custom function.(Query1)

 

(Sum) =>
let
    #"Added Index" = Table.AddIndexColumn(Sum, "Index", 1, 1, Int64.Type),
    #"Added Conditional Column" = Table.AddColumn(#"Added Index", "Custom", each if [Index] = 1 then [Stock] else 0-[Stock]),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column", "Custom.1", each List.Sum(List.FirstN(#"Added Conditional Column"[Custom],[Index]))),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Custom.1", "Sum"}})
in
    #"Renamed Columns"

 

2.Put the function to the table (Query 2)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI31jcyV9JRMjQ3MjUA0kbGZpZKsTpUkDQ2BEmamBqB5UwM8cvFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Starting Date" = _t, Stock = _t, HE.Code = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Starting Date", type date}, {"Stock", Int64.Type}, {"HE.Code", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"HE.Code"}, {{"Count", each _, type table [Starting Date=nullable date, Stock=nullable number, HE.Code=nullable number]}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "Query1", each Query1([Count])),
    #"Expanded Query1" = Table.ExpandTableColumn(#"Invoked Custom Function", "Query1", {"Starting Date", "Stock", "Sum"}, {"Starting Date", "Stock", "Sum"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Query1",{"Count"})
in
    #"Removed Columns"

 

Output

vxinruzhumsft_0-1680144832184.png

 

Best Regards!

Yolo Zhu

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

View solution in original post

2 REPLIES 2
v-xinruzhu-msft
Community Support
Community Support

Hi @kivancc94 

You can refer to the following example.

Create two blank query,then put the following two queries to the Advanced Editor in power query Editor.

1.Create a custom function.(Query1)

 

(Sum) =>
let
    #"Added Index" = Table.AddIndexColumn(Sum, "Index", 1, 1, Int64.Type),
    #"Added Conditional Column" = Table.AddColumn(#"Added Index", "Custom", each if [Index] = 1 then [Stock] else 0-[Stock]),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column", "Custom.1", each List.Sum(List.FirstN(#"Added Conditional Column"[Custom],[Index]))),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Custom.1", "Sum"}})
in
    #"Renamed Columns"

 

2.Put the function to the table (Query 2)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI31jcyV9JRMjQ3MjUA0kbGZpZKsTpUkDQ2BEmamBqB5UwM8cvFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Starting Date" = _t, Stock = _t, HE.Code = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Starting Date", type date}, {"Stock", Int64.Type}, {"HE.Code", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"HE.Code"}, {{"Count", each _, type table [Starting Date=nullable date, Stock=nullable number, HE.Code=nullable number]}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "Query1", each Query1([Count])),
    #"Expanded Query1" = Table.ExpandTableColumn(#"Invoked Custom Function", "Query1", {"Starting Date", "Stock", "Sum"}, {"Starting Date", "Stock", "Sum"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Query1",{"Count"})
in
    #"Removed Columns"

 

Output

vxinruzhumsft_0-1680144832184.png

 

Best Regards!

Yolo Zhu

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

Thanks a lot !

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.