Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
thampton
Helper III
Helper III

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 actions that show up on separate rows. These are the rows below in red text. Is this possible? 

 

UserActionDate TimeSequenceDesired IndexDuration
JohnA1/1/19 1:00000
JohnB1/1/19 2:001160
John1/1/19 3:00
2

260
JohnC 1/1/19 3:05325
JohnD1/1/19 4:004355

 

2. Is there a way to create a duration column to show time between the steps. All of these will have the same ID field, so i would like to see duration between each step from start to end. 

 

Thanks!

2 ACCEPTED SOLUTIONS
v-cherch-msft
Microsoft Employee
Microsoft Employee

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

1.png

Regards,

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Zubair_Muhammad
Community Champion
Community Champion

@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"

View solution in original post

2 REPLIES 2
Zubair_Muhammad
Community Champion
Community Champion

@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"
v-cherch-msft
Microsoft Employee
Microsoft Employee

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

1.png

Regards,

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.