Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Dax calc previous & current value

Hi all, I need help with some calculations. I have the following data and the desired result: Date Task1 Task2 Task3 (task3 prev. +task1) – task2 Task4(manually entered) ...
  • Anonymous's avatar
    Anonymous
    5 years ago

     

    // Tasks
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY3BDcAwCAN34R0pxsA0UfZfo2moVH+M0J1hLYNP5CToNswBnFE33wW2x3EoDtuJm41DccmJxik4pMz/QYnS1fwO7Ac=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Task1 = _t, Task2 = _t, Task4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Task1", Int64.Type}, {"Task2", Int64.Type}, {"Task4", Int64.Type}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Date", "Task1", "Task2", "Task4"}),
        #"Invoked Custom Function" = Table.AddColumn(#"Reordered Columns", "fnGetCumulativeSum", each fnGetCumulativeSum(fnGetSummationStartIndex([Index]), [Index])),
        #"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"Index"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"fnGetCumulativeSum", "Task3"}}),
        #"Reordered Columns1" = Table.ReorderColumns(#"Renamed Columns",{"Date", "Task1", "Task2", "Task3", "Task4"})
    in
        #"Reordered Columns1"
    
    // Summation
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY3BDcAwCAN34R0pxsA0UfZfo2moVH+M0J1hLYNP5CToNswBnFE33wW2x3EoDtuJm41DccmJxik4pMz/QYnS1fwO7Ac=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Task1 = _t, Task2 = _t, Task4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Task1", Int64.Type}, {"Task2", Int64.Type}, {"Task4", Int64.Type}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Date", "Task1", "Task2", "Task4"}),
        #"Removed Columns1" = Table.RemoveColumns(#"Reordered Columns",{"Date"}),
        #"Added Custom" = Table.AddColumn(#"Removed Columns1", "Task1 - Task2", each [Task1] - [Task2]),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Helper2", each if [Task4] is null then [#"Task1 - Task2"] else [Task4]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Task1 - Task2", "Task1", "Task2"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Helper2", "Value"}})
    in
        #"Renamed Columns"
    
    // fnGetCumulativeSum
    (StartIndex as number, EndIndex as number) as number => 
    let
        CumulativeSum = List.Sum(
            Table.SelectRows(
                Summation,
                each StartIndex <= [Index] and [Index] <= EndIndex
            )[Value]
        )
    in
        CumulativeSum
    
    // fnGetSummationStartIndex
    (CurrentIndex as number) as number => 
    let
        SummationStartIndex = List.Max(
            Table.SelectRows(
                Summation,
                each 
                    [Index] <= CurrentIndex and [Task4] <> null
            )[Index]
        )
    in
        SummationStartIndex

     

    Just copy the code as it is above and paste into this pane (right-click the pane and paste):

    I have assumed that the earliest row in the Task table will have a non-null manual entry (Task 4). If not, then you'll have to modify the functions so that they return what you want.