Forum Discussion

muhammadbinzia's avatar
muhammadbinzia
Frequent Visitor
4 years ago
Solved

Calculation of Total s from a column based on Document Numbers

Hello guys, its very important and difficult task that I am faced with. Below is a table  where you can see that the data is in ascending order as per dates and also as per the Document number. If w...
  • Vijay_A_Verma's avatar
    4 years ago

    Solution uploaded to - https://1drv.ms/x/s!Akd5y6ruJhvhuSIghsHUEzQkwFJ-?e=8XJUft 

    M-code

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}, {"Column2", type text}, {"Column3", Int64.Type}, {"Column4", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column3"}, {{"All", each _, type table [Column1=nullable date, Column2=nullable text, Column3=nullable number, Column4=nullable number]}}),
        #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "ProcessFunction", each ProcessFunction([All])),
        #"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"Column3", "All"}),
        #"Expanded ProcessFunction" = Table.ExpandTableColumn(#"Removed Columns", "ProcessFunction", {"Column1", "Column2", "Column3", "Column4", "Result"}, {"Column1", "Column2", "Column3", "Column4", "Result"})
    in
        #"Expanded ProcessFunction"
    (TableName)=>
    let
        #"Added Index" = Table.AddIndexColumn(TableName, "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Sum", each List.Sum(#"Added Index"[Column4])),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Result", each if [Sum]>0 then
    (if [Index]=0 then [Sum] else if [Index]=2 then -1*[Column4] else 0)
    else if [Index]>0 then -1*[Column4] else 0),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Sum"})
    in
        #"Removed Columns"