cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Elnaz91
Regular Visitor

Recursive calculations in the calculated column after filtering data based on date value

Hi all,

 

For each subgroup in a group, I want to calculate a column based on this formula:

New column(n) = max(0,new column(n-1)+ column1(n) - column2(n)

In this formula n refers to the current row, n-1 refers to previous row according to the the date column

 

20210513_073752.jpg

Actually I can do the following calculation using SUMX in CALCULATION and FILTER expression in dax:

New column(n)=new column(n-1)+ column1(n)-column2(n)

However, my main problem is in implementing the max(0, "calculation")

In fact by imposing this maximum I want to put a lower limit 0 on my calculated values.

 

Thanks for your help

 

Elnaz

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

Hi @Elnaz91 ,

 

Please try to do it in power query.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXICYiMDI0N9Q30jINPUAEgYGijF6mBIG4OZYGms8iZQKbAqLPKmYCbM/FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Subgroup = _t, Date = _t, Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Subgroup", type text}, {"Date", type date}, {"Column1", Int64.Type}, {"Column2", Int64.Type}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "column3", each [Column1]-[Column2]),
    #"Added Index" = Table.AddIndexColumn(#"Added Custom1", "index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Accumulate(List.Range(#"Added Index" [column3],0,[index]),0,(x,y)=>if x+y<0 then 0 else x+y))

in
    #"Added Custom"

 

Capture1.PNG

 

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

 

Best Regards,

Dedmon Dai

View solution in original post

2 REPLIES 2
v-deddai1-msft
Community Support
Community Support

Hi @Elnaz91 ,

 

Please try to do it in power query.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXICYiMDI0N9Q30jINPUAEgYGijF6mBIG4OZYGms8iZQKbAqLPKmYCbM/FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Subgroup = _t, Date = _t, Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Subgroup", type text}, {"Date", type date}, {"Column1", Int64.Type}, {"Column2", Int64.Type}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "column3", each [Column1]-[Column2]),
    #"Added Index" = Table.AddIndexColumn(#"Added Custom1", "index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Accumulate(List.Range(#"Added Index" [column3],0,[index]),0,(x,y)=>if x+y<0 then 0 else x+y))

in
    #"Added Custom"

 

Capture1.PNG

 

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

 

Best Regards,

Dedmon Dai

View solution in original post

Thanks a million it helped me alot.

How ever I am dealling with other problem as follows:

 

Newcolumn[n]=MAX(0,MAX(0,Newcolumn[n-1]-column1[n])+column2[n])

 

I really appreciate your time

Elnaz

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Get Ready for Power BI Dev Camp

Power BI Dev Camp - June 24th

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors