Forum Discussion

credencial's avatar
credencial
Frequent Visitor
5 years ago
Solved

Transforming Data with M

Hi folks, I have a table data:     I need create two more columns like this:     and fill in the START and END columns following the formula:   START = END OF PREVIOUS MONTH (in ...
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi credencial ,

     

    Here is the whole M syntax:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKk4t0DcyUNJRgiBDAwOlWJ1opfzkEoiwKYgwAysAiefll0HELUCEoRFcIiU1GSJhhKQhFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Entrance = _t, Exit = _t, End = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}, {"Entrance", Int64.Type}, {"Exit", Int64.Type}, {"End", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try #"Added Index" [End] {[Index] - 1} otherwise null),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"End"}),
        #"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom.1", each if [Index]=0 then [End] else [End]+List.Sum(List.Range(#"Filled Down"[Entrance],1,[Index]))-List.Sum(List.Range(#"Filled Down"[Exit],1,[Index]))),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom"}),
        #"Added Custom2" = Table.AddColumn(#"Removed Columns", "Custom", each try #"Added Custom1" [Custom.1]{[Index] - 1} otherwise null),
        #"Removed Columns1" = Table.RemoveColumns(#"Added Custom2",{"Index", "End"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Custom.1", "End"}, {"Custom", "Start"}})
    in
        #"Renamed Columns"

    The final result is this.

     

    Best Regards,

    Stephen Tao

     

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