Forum Discussion

VilmarSch's avatar
VilmarSch
Post Partisan
2 years ago
Solved

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?   Mes Valor1 Valor2 Medida ...
  • lbendlin's avatar
    2 years ago

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

     

     

     

    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.