Forum Discussion

thampton's avatar
thampton
Helper III
7 years ago
Solved

Nested Index Column & Referencing Previous Row

Hello, i have a two part inquiry:    I have a table similar to what is below. It has a sequence column that acts as an index, but i would like to create a new index column to catch duplicate action...
  • v-cherch-msft's avatar
    7 years ago

    Hi thampton 

    You may create two calculated columns as below:

    Desired Index =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[Action] ),
        FILTER ( 'Table', 'Table'[Sequence] <= EARLIER ( 'Table'[Sequence] ) )
    ) - 1
    
    Duration =
    DATEDIFF (
        CALCULATE (
            MAX ( 'Table'[Date Time] ),
            FILTER ( 'Table', 'Table'[Sequence] = EARLIER ( 'Table'[Sequence] ) - 1 )
        ),
        'Table'[Date Time],
        MINUTE
    ) + 0
    

    Regards,

  • Zubair_Muhammad's avatar
    7 years ago

    thampton 

     

    Check the M / Power Query solution as well

     

    File attached as well

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUXIEYgUTYx0TMzM9AxNDMxAwVwAKGijF6sBVOSGrsjCGAJAqQ2RVzocWIKkzNDI1AAOQOiN86ixMzI2AAKTOGFmdC7IqMzOE20yUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [User = _t, Action = _t, #"Date Time" = _t, Sequence = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date Time", type number}, {"Sequence", Int64.Type}}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Date Time", type datetime}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"Action"}, {{"All", each _, type table}}),
        #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 0, 1),
        #"Expanded All" = Table.ExpandTableColumn(#"Added Index", "All", {"User", "Date Time", "Sequence"}, {"User", "Date Time", "Sequence"}),
        #"Added Custom" = Table.AddColumn(#"Expanded All", "Custom", each let myuser=[User],myseq=[Sequence] in Duration.TotalMinutes(
    [Date Time]-  (try Table.SelectRows(#"Expanded All",each [User]=myuser and [Sequence]=myseq-1)[Date Time]{0} otherwise [Date Time])))
    in
        #"Added Custom"