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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
lucasneedhelp
Helper I
Helper I

Calculated Column between start time from current row and end time from previous row

Hi All,

I'm have a set of data already formated and sorted by space and start time. I'm trying to add a column to say if space is the same as previous row, calculate start time from current row - end time from previous row. otherwise leave blank. Can someone please help with the fomular? Thanks heaps!! Below is an example of the data set :

 

SPACESTART TIMEEND TIME
ABC21/06/2025 06:00:0022/06/2025 23:59:00
ABC25/06/2025 00:01:0026/06/2025 23:59:00
DEF30/11/2023 00:01:0001/12/2023 06:00:00
2 ACCEPTED SOLUTIONS
BA_Pete
Super User
Super User

Hi @lucasneedhelp ,

 

One way to do it is to create two offset Index columns and merge the table on itself using [SPACE] & [Index0] = [SPACE] & [Index1], like this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRMjLUNzDTNzIwMlUwMLMyMAAikKgRXNTI2MrUEiQaqwPXY4rQA9RgCNVjhl2Pi6sbUNbYQN/QECRrjKzHwFDf0AgqCrM9NhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SPACE = _t, #"START TIME" = _t, #"END TIME" = _t]),

// Relevant steps from here ====>
    addIndex1 = Table.AddIndexColumn(Source, "Index1", 1, 1, Int64.Type),
    addIndex0 = Table.AddIndexColumn(addIndex1, "Index0", 0, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex0, {"SPACE", "Index0"}, addIndex0, {"SPACE", "Index1"}, "addIndex0", JoinKind.LeftOuter),
    expandEndTime = Table.ExpandTableColumn(mergeOnSelf, "addIndex0", {"END TIME"}, {"END TIME PREV ROW"}),
// <==== Relevant steps end here

    sortIndex0 = Table.Sort(expandEndTime,{{"Index0", Order.Ascending}}),
    remOthCols = Table.SelectColumns(sortIndex0,{"SPACE", "START TIME", "END TIME", "END TIME PREV ROW"})
in
    remOthCols

 

To get this output:

BA_Pete_0-1701844724244.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

AlienSx
Super User
Super User

hi, @lucasneedhelp funny recursion

    f = (i, lst, space, etime) =>
        if rows{i}? = null 
            then lst 
            else 
                @f(
                    i + 1, 
                    lst & 
                        {rows{i} & 
                            [diff = 
                                if (rows{i}[SPACE] <> space or space = null) 
                                    then null 
                                    else rows{i}[START TIME] - etime
                            ]
                        },
                    rows{i}[SPACE],
                    rows{i}[END TIME]
                ),
    rows = List.Buffer(Table.ToRecords(your_table)),
    z = Table.FromRecords(f(0, {}, null, null))

 

rcrs.jpg 

View solution in original post

5 REPLIES 5
AlienSx
Super User
Super User

hi, @lucasneedhelp funny recursion

    f = (i, lst, space, etime) =>
        if rows{i}? = null 
            then lst 
            else 
                @f(
                    i + 1, 
                    lst & 
                        {rows{i} & 
                            [diff = 
                                if (rows{i}[SPACE] <> space or space = null) 
                                    then null 
                                    else rows{i}[START TIME] - etime
                            ]
                        },
                    rows{i}[SPACE],
                    rows{i}[END TIME]
                ),
    rows = List.Buffer(Table.ToRecords(your_table)),
    z = Table.FromRecords(f(0, {}, null, null))

 

rcrs.jpg 

Hi Alien, 

thanks for your solution, i'll give it a go. we haev multiple spaces for hire int he venue, and i'm just trying to figure out the turnaround time for a space between last hire and next hire.

BA_Pete
Super User
Super User

Hi @lucasneedhelp ,

 

One way to do it is to create two offset Index columns and merge the table on itself using [SPACE] & [Index0] = [SPACE] & [Index1], like this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRMjLUNzDTNzIwMlUwMLMyMAAikKgRXNTI2MrUEiQaqwPXY4rQA9RgCNVjhl2Pi6sbUNbYQN/QECRrjKzHwFDf0AgqCrM9NhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SPACE = _t, #"START TIME" = _t, #"END TIME" = _t]),

// Relevant steps from here ====>
    addIndex1 = Table.AddIndexColumn(Source, "Index1", 1, 1, Int64.Type),
    addIndex0 = Table.AddIndexColumn(addIndex1, "Index0", 0, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex0, {"SPACE", "Index0"}, addIndex0, {"SPACE", "Index1"}, "addIndex0", JoinKind.LeftOuter),
    expandEndTime = Table.ExpandTableColumn(mergeOnSelf, "addIndex0", {"END TIME"}, {"END TIME PREV ROW"}),
// <==== Relevant steps end here

    sortIndex0 = Table.Sort(expandEndTime,{{"Index0", Order.Ascending}}),
    remOthCols = Table.SelectColumns(sortIndex0,{"SPACE", "START TIME", "END TIME", "END TIME PREV ROW"})
in
    remOthCols

 

To get this output:

BA_Pete_0-1701844724244.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hey Pete, this works perfectly for me!

Brilliant!!!!!

Thanks Pete. the script seems a bit too advanced for me. but i'll give it a go.

Helpful resources

Announcements
Fabcon_Europe_Social_Bogo

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

Top Solution Authors
Top Kudoed Authors