Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Row context total of previous rows

Dear all,   I would like to make a calculated column which calculates on the row context, per key the total untill that moment. See the example below, I have three columns, and a key is active fro...
  • Anonymous's avatar
    Anonymous
    7 years ago

    Solved it myself ;), and want to share it, if in any case there is an easier way to do this, im still interested.

    My calculated column formula is as follows:

    'New column' =
    CALCULATE ( SUM ( Table1[Amount] ); ALLEXCEPT ( Table1; Table1[Key] ) )
        - DATEDIFF (
            Table1[Date];
            CALCULATE ( MAX ( Table1[Date] ); ALLEXCEPT ( Table1; Table1[Key] ) );
            DAY
        )

  • Ashish_Mathur's avatar
    7 years ago

    Hi,

    This M code (Query Editor) will also work

    let
        Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Date", type datetime}, {"Amount", Int64.Type}}),
        Partition = Table.Group(#"Changed Type", {"Key"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
        #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Date", "Amount", "Index"}, {"Date", "Amount", "Index"})
    in
        #"Expanded Partition"

    Hope this helps.