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
VilmarSch
Post Patron
Post Patron

Do Not Consider If Accumulated Value Is Negative Part2

I tried to do the same calculation that my friend @DataNinja777  helped with in this topic , but it didn't work.

 

How to do this calculation in Power BI?

Captura de tela 2024-04-26 203352.png

 

MesValor1Valor2Medida
1277,25476,260,00
2892,78997,810,00
3322,32313,259,07
4557,25734,940,00
5782,88992,080,00
6476,11942,300,00
71.034,96921,74113,22
81.641,06859,57894,72
9544,831.142,17297,38
101.064,50825,65536,23
11710,08626,05620,26
12849,20675,70793,77
1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

It is impossible to do variable aggregations in Power BI. You can only do these in Power Query, with List.Accumulate.

 

lbendlin_0-1714177150647.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TZDLEQMxCEN78VmT4WcDteyk/zYCOIe9CfMsBM+zeGGJO2SXMD+QU4JAtL54llQRKfAokekIfre1ChWBNqes1yZBPm2rYu+/uash7f17XkMQ11xA8W6ffyLukWk1hd5tr4I/1K5NpjC87bljyCAxyDEGNRI7sX02MvhFsiOaIXRYrjHciNSuGoMw3UnHsFuGbJzOvrXOpZfpjM50VzhyQHsE9UGHmFNaQtrj+Ia38FR4Xev7Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Mes = _t, Valor1 = _t, Valor2 = _t, Medida = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Mes", Int64.Type},{"Valor1", Currency.Type}, {"Valor2", Currency.Type}, {"Medida", Currency.Type}},"es"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Medida2", each List.Accumulate({1..[Mes]},List.Max({0,#"Changed Type"{0}[Valor1]-#"Changed Type"{0}[Valor2]}),(state,current)=> List.Max({0,#"Changed Type"{current-1}[Valor1]-#"Changed Type"{current-1}[Valor2]+state})), Currency.Type)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

It is impossible to do variable aggregations in Power BI. You can only do these in Power Query, with List.Accumulate.

 

lbendlin_0-1714177150647.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TZDLEQMxCEN78VmT4WcDteyk/zYCOIe9CfMsBM+zeGGJO2SXMD+QU4JAtL54llQRKfAokekIfre1ChWBNqes1yZBPm2rYu+/uash7f17XkMQ11xA8W6ffyLukWk1hd5tr4I/1K5NpjC87bljyCAxyDEGNRI7sX02MvhFsiOaIXRYrjHciNSuGoMw3UnHsFuGbJzOvrXOpZfpjM50VzhyQHsE9UGHmFNaQtrj+Ia38FR4Xev7Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Mes = _t, Valor1 = _t, Valor2 = _t, Medida = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Mes", Int64.Type},{"Valor1", Currency.Type}, {"Valor2", Currency.Type}, {"Medida", Currency.Type}},"es"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Medida2", each List.Accumulate({1..[Mes]},List.Max({0,#"Changed Type"{0}[Valor1]-#"Changed Type"{0}[Valor2]}),(state,current)=> List.Max({0,#"Changed Type"{current-1}[Valor1]-#"Changed Type"{current-1}[Valor2]+state})), Currency.Type)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

@lbendlin thanks for the answers. I believe Microsoft could see this.

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.