Forum Discussion

AGo's avatar
AGo
Post Patron
6 years ago
Solved

Conditional efficient rolling sum function

Hi! I combined a lot of csvs now I have to calculate (in M language because I'll have to pivot a column next) a new column which increases by 1 after every time [Stop] value is 1. In Calc1 there's t...
  • ImkeF's avatar
    ImkeF
    6 years ago

    Hi AGo 

    please check out this solution (paste the code into the advanced editor and follow the steps):

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1SGXNKRAL9XNiQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Stop = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Stop", Int64.Type}}),
        Custom1 = List.Buffer( #"Changed Type"[Stop] ),
        Calc1 = List.Generate( ()=>
            [Level = 0, Counter = 0],
            each [Counter] < List.Count(Custom1) ,
            each [
                Level = if Custom1{[Counter]} = 1 then [Level] + 1 else [Level],
                Counter = [Counter] + 1
            ],
            each [Level] ),
        AutomaticConversionWithFullTable = Table.FromColumns( Table.ToColumns(#"Changed Type") & {#"Calc1"}, Table.ColumnNames(#"Changed Type") & {"Calc1"} )
    in
        AutomaticConversionWithFullTable