Forum Discussion

hpatel24779's avatar
hpatel24779
Helper II
1 year ago

identifying continuous dates per person in new column

Hi All,

 

I want to firstly thank all members that have helped with my queries so far and hope to continue getting support which will help me improve on my Power BI skills. 

 

My latest query is quite complex so apologies in advance if i do not explain this very well. i want to be able to identify the earliest start date and latest end date if the dates are continuous or within certain time frame. Below is my table with list of clients, service and start and end dates. in separate columns, i want these required start and end dates:

 

Client IDServiceStart DateEnd DateRequired Start DateRequired End Date
1A20/06/201631/03/202220/06/2016 
1A01/04/202217/09/202320/06/2016 
1A18/09/202329/03/202420/06/2016 
1A30/03/2024 20/06/2016 
1B18/09/202330/03/202418/09/2023 
1B31/03/2024 18/09/2023 
2A14/10/202331/03/202414/10/2023 
2A01/04/2024 14/10/2023 
3C18/02/202331/03/202318/02/202316/01/2024
3C01/04/202328/11/202318/02/202316/01/2024
3C29/11/202316/01/202418/02/202316/01/2024
4A03/06/202320/08/202303/06/2023 
4A04/09/202331/03/202403/06/2023 
4A01/04/2024 03/06/2023 
5C24/01/202405/04/202424/01/202405/04/2024
5C14/06/2024 14/06/2024 

 

This will be based on the service they are getting. Client 1 is receiving Service A & B so the continuous date should only be applied to that service only.

 

For Client 4, there is a small gap in one service ending and another starting, so as a rule, i want any gaps of say 14 days between one ending and next starting, i want to treat this as a continuous service therefore show the dates in the required start and end dates.

 

For Client 5, the gap between the first service to second service is longer than 14 days so i need this to show the same dates as the service start and end date.

 

Hope this makes sense. I am not entirely sure if possible but i have come across some posts similar to this and there may be a way to do this.

 

I am doing this in Power Query.

 

kind regards

 

Hetal

16 Replies

  • p45cal's avatar
    p45cal
    Solution Supplier

    You'll have to test this one carefully.

    In the linked-to workbook below, there's a source table at cell B2 (your data). See the Power Query result table at cell I2. There's cell H1 (a named range MinGap) where you can input a gap (in days) which should consider gaps smaller than that as continuous dates (it may be a day out). Change that figure then refresh the PQ table. As I left it, the results matched your required/expected dates.:

     

     

    Link to workbook: https://app.box.com/s/cwq6j9kxwakgriwxx2uzomj6a4trdylv

     

    • hpatel24779's avatar
      hpatel24779
      Helper II

      hi p45cal,

       

      this works perfectly in excel - how can i apply the same method in Power BI query? i don't know how to get the MinGap in my model

      • p45cal's avatar
        p45cal
        Solution Supplier

        re: "how can i apply the same method in Power BI query?"

        I'm ashamed to say 'I don't know'. Power BI is something I've only just begun to explore and my attempts at this would probably be very amateurish. Maybe someone seeing this thread would know how to adapt my solution.

        I did try Yolo's query on your bigger data, primarily to compare results, and 55 minutes later it's still:

         

         

        although my machine is 9 years old.

        Does your data reach Power BI via Excel or in some other way?

         

         

         

  •  

    For Client 4, there is a small gap in one service ending and another starting, so as a rule, i want any gaps of say 14 days between one ending and next starting,

     

    The gap for Client 4 is 15 days.

     

     

     

    Define what you mean by "gap"

    • hpatel24779's avatar
      hpatel24779
      Helper II

      Sorry but miscalculation in my part as it is 15 days. By 'gap' i mean between the end of previous service and start of next service

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi hpatel24779 

    Thanks for the solutions p45cal  and lbendlin offered, and i want to offer some more information for user to refer to.

    hello hpatel24779 , basd on the data you hace offered, as lbendlin mentioned, the time difference for client4 is 15, so i change the date for client4 from 4/9/2024 to 3/9/2024, then you can refer to the following code.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZBLDoMwDESvUmUdyV/SsCw9BuL+16hpEicBsWBh82Y8k30PFGL42McImICRkg1CgGIDczhiZ9DWWtYx0BtwPQeZGMq+NtO1+ujECPo6hpf/2q7ygRsZT9fl3K4rELqcJjlfW3T5SX/rdb7JZWJc/i+YgejOWO+2NtMESD2GthhSHrz4WNPcfUZmfaijz3WWFkP9snGLcyNjL1ZiVPnxAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Client ID" = _t, Service = _t, #"Start Date" = _t, #"End Date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client ID", Int64.Type}, {"Service", type text}, {"Start Date", type text}, {"End Date", type text}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Start Date", type date}, {"End Date", type date}}, "en-GB"),
        #"Grouped Rows" = Table.Group(#"Changed Type with Locale", {"Client ID", "Service"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
        #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Start Date", "End Date", "Index"}, {"Start Date", "End Date", "Index"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Count", "Custom", each let a=[Index],
    b=[Service],
    c=[Client ID],
    d=try Table.SelectRows(#"Expanded Count", each [Index]=a+1 and [Service]=b and [Client ID]=c)[Start Date]{0} otherwise null
    in Duration.Days(d-[End Date])),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
        #"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom.1", each let _client=[Client ID],
    _service=[Service],
    _table=Table.Buffer(Table.Sort(Table.SelectRows(#"Filled Down",each [Client ID]=_client and [Service]=_service),{"Index",Order.Ascending})),
    _counttable=Table.RowCount(_table),
    _count2=Table.RowCount(Table.SelectRows(_table,each [Custom]=1 or [Custom]=14))
    in _counttable=_count2),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Required Start", each let _client=[Client ID],
    _service=[Service],
    _table=Table.Buffer(Table.Sort(Table.SelectRows(#"Filled Down",each [Client ID]=_client and [Service]=_service),{"Index",Order.Ascending}))
    in if [Custom.1]=true then List.First(_table[Start Date]) else [Start Date]),
        #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Required End", each let _client=[Client ID],
    _service=[Service],
    _table=Table.Buffer(Table.Sort(Table.SelectRows(#"Filled Down",each [Client ID]=_client and [Service]=_service),{"Index",Order.Ascending}))
    in if [Custom.1]=true then List.Last(_table[End Date]) else [End Date]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Index", "Custom", "Custom.1"})
    in
        #"Removed Columns"

     

    Output

    Best Regards!

    Yolo Zhu

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    • p45cal's avatar
      p45cal
      Solution Supplier

      Yolo, re: 'so i change the date for client4 from 4/9/2024 to 3/9/2024'

      (I think you meant 2023, not 2024.) I was comparing our results; try changing that same date to 4th Nov 2023.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi p45cal 

        Thank you for your quick reply. Yes, i change the data from 4/9/2023 to 3/9/2023.

         

        Best Regards!

        Yolo Zhu

         

         

    • hpatel24779's avatar
      hpatel24779
      Helper II

      thank you lbendlin and p45cal for you input.

       

      Anonymous this seems to be what i am looking for however, when applying this to my model it is taking too long because my data has thousands of rows and i only supplied small example. is there a workaround to speed up the calculations?

      • lbendlin's avatar
        lbendlin
        Super User

        Please provide sample data that fully covers your issue.
        Please show the expected outcome based on the sample data you provided.