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
Phuonghm22
Frequent Visitor

Create a calculated column like running total but have limitations

Hi guys,

I have a problem. I want to create a calculated column call ExpectedOutcome .

The value of column ExpectedOutcome for 1st row where No.Session = 1 is calculated by Score + 90.
From the second row, the value of ExpectedOutcome = previous ExpectedOutcome + Score.

The value of ExpectedOutcome is always in the range of 0 - 100. If it < 0, then we put 0 as the value. If it > 100, then we put 100 as the value.

It seems like calculate a running total, but it is not. And I do not know how to solve this problem.

Plz help me. Thank you.Screenshot_10.png

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

Hi  @Phuonghm22 ,

There is no recursive function in computed columns, you can use Power Query:

Here are the steps you can follow:

1. Power Query – Home  -- Advanced Editor.

vyangliumsft_0-1667536095508.png

2. Put the following code in it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc/RDcAgCEXRXfjWxIfV2lmM+69REAv9MYeGeO2cBErEtNKU81N1Xa7m6q7bNUS5bT7+EUVYjYgFRAhRQqTQfstRQ+QwgtrLvdgvaDHXM+wmn4H3YNaqXc9aPRtalSev9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No.Session = _t, Score = _t]),
    test = Table.TransformColumnTypes(Source,{{"No.Session", Int64.Type}, {"Score", Int64.Type}}),
    #"Added Custom" = Table.TransformColumns(Table.AddColumn(test, "Custom", (x)=>  List.Accumulate(List.Range(test[Score],0,x[No.Session]),0,(x,y)=>if x+y >=10 then 10 else if x+y<=-90 then -90 else x+y)             ),{"Custom",(x)=>x+90})
in
    #"Added Custom"

3. Result:

vyangliumsft_1-1667536095511.png

 

Best Regards,

Liu Yang

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-yangliu-msft
Community Support
Community Support

Hi  @Phuonghm22 ,

There is no recursive function in computed columns, you can use Power Query:

Here are the steps you can follow:

1. Power Query – Home  -- Advanced Editor.

vyangliumsft_0-1667536095508.png

2. Put the following code in it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc/RDcAgCEXRXfjWxIfV2lmM+69REAv9MYeGeO2cBErEtNKU81N1Xa7m6q7bNUS5bT7+EUVYjYgFRAhRQqTQfstRQ+QwgtrLvdgvaDHXM+wmn4H3YNaqXc9aPRtalSev9QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [No.Session = _t, Score = _t]),
    test = Table.TransformColumnTypes(Source,{{"No.Session", Int64.Type}, {"Score", Int64.Type}}),
    #"Added Custom" = Table.TransformColumns(Table.AddColumn(test, "Custom", (x)=>  List.Accumulate(List.Range(test[Score],0,x[No.Session]),0,(x,y)=>if x+y >=10 then 10 else if x+y<=-90 then -90 else x+y)             ),{"Custom",(x)=>x+90})
in
    #"Added Custom"

3. Result:

vyangliumsft_1-1667536095511.png

 

Best Regards,

Liu Yang

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

Thank you for reply. But I have another problem that make thing more complicated. The same data sample as I already provided before, but now it have a column that have multiple ID, and I have to use that column ID to group by while calculating. How can I do to handle this problem?

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.

Top Solution Authors