March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
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 ID | Service | Start Date | End Date | Required Start Date | Required End Date |
1 | A | 20/06/2016 | 31/03/2022 | 20/06/2016 | |
1 | A | 01/04/2022 | 17/09/2023 | 20/06/2016 | |
1 | A | 18/09/2023 | 29/03/2024 | 20/06/2016 | |
1 | A | 30/03/2024 | 20/06/2016 | ||
1 | B | 18/09/2023 | 30/03/2024 | 18/09/2023 | |
1 | B | 31/03/2024 | 18/09/2023 | ||
2 | A | 14/10/2023 | 31/03/2024 | 14/10/2023 | |
2 | A | 01/04/2024 | 14/10/2023 | ||
3 | C | 18/02/2023 | 31/03/2023 | 18/02/2023 | 16/01/2024 |
3 | C | 01/04/2023 | 28/11/2023 | 18/02/2023 | 16/01/2024 |
3 | C | 29/11/2023 | 16/01/2024 | 18/02/2023 | 16/01/2024 |
4 | A | 03/06/2023 | 20/08/2023 | 03/06/2023 | |
4 | A | 04/09/2023 | 31/03/2024 | 03/06/2023 | |
4 | A | 01/04/2024 | 03/06/2023 | ||
5 | C | 24/01/2024 | 05/04/2024 | 24/01/2024 | 05/04/2024 |
5 | C | 14/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
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.
thank you @lbendlin and @p45cal for you input.
@v-xinruzhu-msft 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?
Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.
What is the meaning of the absence of End Date?
Looking at your linked worksheet:
If it means the task is ongoing, then why aren't All of the Required Start Date for 9816 = 4/1/2023.
Please clarify.
Depending on the OP's answer, I may have to revisit my offering…
I don't see thousands of rows in your sample file.
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.
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
OK. Now try changing that same date to 4th Nov 2023, refresh, does it give the results you expect? Yours and mine are different.
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
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
In the linked-to pbix file below is my offering.
Note that your columns are not the same as your original request (Client ID has become Person Id, and there is no Service column) so I've had to re-write the query a bit.
The pbix file links directly to your dropbox file, so don't change your dropbox file until you've changed the source in the pbix file.
Regarding MinGap, I've added that instead as a step in the query which you can edit easily:
Link: https://app.box.com/s/pzc1ewxh4xc5z8q7barwgtjon13cz2bm
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"
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
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.