Forum Discussion
Grouping consecutive dates into a single row
- 5 years ago
See it all at work in the attached file.
I'm getting a bit lost. The example that you show (2504-Z004) has only one row in the data you've shared, with dates 18/01/2021 - 18/01/2021 , so the result you show is the correct one
Most of the Object No in your data have only one row. I had a look at the one with the larger number or rows (you can see them in the query Object Billing Export (TESTS) that I created to help in the debugging and they seem to work fine.
I uncommented the S2_ and S3_ ( to use the latest versions I'd suggested, you had the previous version active) and included sorting by Returned Date on this step, to get the dates completely sorted out before the crucial step:
Sorted by Object No = Table.Sort(#"Reordered Columns",{{"Object No", Order.Ascending}, {"Borrowed Date", Order.Ascending}, {"Returned Date", Order.Ascending}})plus it looks like the error you got (Error - Unable to convert type null to type logical) was caused by cases like
3433-Z062 that had a null not as the very latest date in Borrowed Date. So I temporarily replaced those nulls with a date far in the future (31/12/9999) so that the row gets placed at the end when sorting ascending (the null woud be placed at the beginning since it is treated as as 0). Then at the end convert that 31/12/9999 back to null
Please accept the solution when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
Ok, we'll have to tweak it a bit, yes.
There's something i don't understand, though. Why are you not merging the two last rows in the expected result you show above? the last two rows should be one rowwith dates:
2021-01-16 2021-01-19
shouldn't it? Otherwise it's inconsistent with what you are doing with the other rows
Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
- matthewkaess5 years ago
Helper I
That is exactly the result I'm looking for but something in the M code logic isn't quite right. The logic section in the query below. I'm just not sure how to change it to cover this scenario.
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let inputT_ = [Grouped],
colStart_ = inputT_[Borrowed Date],
colEnd_ = inputT_[Returned Date],
S1_ = Table.AddIndexColumn(inputT_,"Index",0),
S2_ = Table.AddColumn(S1_, "Date1", each if try colEnd_{[Index]-1}=Date.AddDays([Borrowed Date],-1) otherwise false then null else [Borrowed Date], type date),
S3_ = Table.AddColumn(S2_, "Date2", each if try colStart_{[Index]+1}=Date.AddDays([Returned Date], 1) otherwise false then null else [Returned Date], type date),
#"Filled Up" = Table.FillUp(S3_,{"Date2"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Up", each [Date1] <> null)