cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
eeew00
Helper I
Helper I

Duplicate column with offset one row inside grouped table

Hello!

 

Easier to explain in pictures, so:

table.png

 

I have grouped the data by user and what I would like to do now is to duplicate the "DateAndTime" column in each user table("Custom" column) AND also raise it by one row.. (End result is to subtract DateAndTime from the duplicated raised row to see time difference) 

 

I'm trying to follow https://www.youtube.com/watch?v=90EYX7pzVlE but since my data is grouped I'm not sure about how to proceed.. 

 

Any help would be greatly appreciated

1 ACCEPTED SOLUTION
Rocco_sprmnt21
Community Champion
Community Champion

try this way:

 

 

let    
...
...

    breakTab=(tab as table) =>
        let
         timeDiff=List.Accumulate(List.Numbers(1,Table.RowCount(tab)-1), {},(s,c)=>s&{tab[Time]{c}-tab[Time]{c-1}}) 
        in   Table.FromColumns({timeDiff},{"timeDiff"}),

    diff= Table.Group(yourTab, {"Group"}, {{"Diff", each breakTab(_), type table}}),
    te = Table.ExpandTableColumn(diff, "Diff", {"timeDiff"}, {"timeDiff"})
in
    te

 

View solution in original post

3 REPLIES 3
eeew00
Helper I
Helper I

Thank you both @Rocco_sprmnt21  @Mariusz , your solutions worked well 🙂 Have a good day and thanks!

Rocco_sprmnt21
Community Champion
Community Champion

try this way:

 

 

let    
...
...

    breakTab=(tab as table) =>
        let
         timeDiff=List.Accumulate(List.Numbers(1,Table.RowCount(tab)-1), {},(s,c)=>s&{tab[Time]{c}-tab[Time]{c-1}}) 
        in   Table.FromColumns({timeDiff},{"timeDiff"}),

    diff= Table.Group(yourTab, {"Group"}, {{"Diff", each breakTab(_), type table}}),
    te = Table.ExpandTableColumn(diff, "Diff", {"timeDiff"}, {"timeDiff"})
in
    te

 

Mariusz
Community Champion
Community Champion

Hi @eeew00 

 

You can try this.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIy1jcw0zcyMDJQMLC0MjBTitXBkDA0sDKESBih6bCA6UCTMDQE64gFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [User = _t, DateTime = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"User", Int64.Type}, {"DateTime", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"User"}, {{"rows", each Table.AddIndexColumn( _, "Index" ), type table [User=number, DateTime=datetime, Index=number]}}),
    #"Expanded rows" = Table.ExpandTableColumn(#"Grouped Rows", "rows", {"DateTime", "Index"}, {"DateTime", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded rows", "Index+1", each [Index]+1, type number ),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Index+1", "User"}, #"Added Custom", {"Index", "User"}, "Added Custom", JoinKind.LeftOuter),
    #"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"DateTime"}, {"Added Custom.DateTime"})
in
    #"Expanded Added Custom"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

Power BI Dev Camp Session 25 with aka link 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, August 25 at 11a PDT for a great session with Ted Pattison!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors