This is best Fabric, Power BI, SQL and AI community event. How do we know? The last event sold out! Save €200 with code FABCMTY200.
Register nowA new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.
Hello,
I have a problem with my reports. For example i have a table:
SA_ID START_DT END_DT
| 8702145434 | 2023-05-05 | 2023-05-08 |
| 8702145434 | 2023-05-08 | 2023-05-09 |
| 8702145434 | 2023-05-09 | 2023-05-11 |
| 8702145434 | 2023-05-12 | 2023-05-15 |
| 8702145434 | 2023-05-19 | 2023-05-22 |
| 8702145434 | 2023-05-23 | 2023-05-23 |
| 8702145434 | 2023-05-26 | 2023-05-29 |
| 1132145497 | 2023-05-05 | 2023-05-08 |
| 1132145497 | 2023-05-12 | 2023-05-15 |
| 1132145497 | 2023-05-16 | 2023-05-22 |
| 1132145497 | 2023-05-26 | 2023-05-29 |
I would like connect dates where END_DT one row is the same or one day earlier than START_DT next row grouping by SA_ID.
Below is the effect I would like to achieve.
SA_ID START_DT END_DT
| 8702145434 | 2023-05-05 | 2023-05-15 |
| 8702145434 | 2023-05-19 | 2023-05-23 |
| 8702145434 | 2023-05-26 | 2023-05-29 |
| 1132145497 | 2023-05-05 | 2023-05-08 |
| 1132145497 | 2023-05-12 | 2023-05-22 |
| 1132145497 | 2023-05-26 | 2023-05-29 |
I've tried everything, searched google and used gpt chat, however I still don't know how to do it. Please help !!!!
Solved! Go to Solution.
Hi
let
Source = YourSource,
#"End+1" = Table.AddColumn(Source, "END+1", each Date.AddDays([END_DT],1)),
Join0 = Table.NestedJoin(#"End+1", {"SA_ID", "START_DT "}, #"End+1", {"SA_ID", "END_DT"}, "Join0", JoinKind.LeftOuter),
Join1 = Table.NestedJoin(Join0, {"SA_ID", "START_DT "}, Join0, {"SA_ID", "END+1"}, "Join1", JoinKind.LeftOuter),
Test = Table.TransformColumns(Join1,{{"Join0", each Table.IsEmpty(_), type logical},{"Join1", each Table.IsEmpty(_), type logical}}),
LocalGroup = Table.Group(Test,
{"SA_ID", "Join0", "Join1"},
{{"START_DT", each List.Min([#"START_DT "]), type nullable date},
{"END_DT", each List.Max([END_DT]), type nullable date}},
GroupKind.Local,
(x,y) => if y[Join0] and y[Join1] then 1 else 0)
in
LocalGroup
Stéphane
Hi
let
Source = YourSource,
#"End+1" = Table.AddColumn(Source, "END+1", each Date.AddDays([END_DT],1)),
Join0 = Table.NestedJoin(#"End+1", {"SA_ID", "START_DT "}, #"End+1", {"SA_ID", "END_DT"}, "Join0", JoinKind.LeftOuter),
Join1 = Table.NestedJoin(Join0, {"SA_ID", "START_DT "}, Join0, {"SA_ID", "END+1"}, "Join1", JoinKind.LeftOuter),
Test = Table.TransformColumns(Join1,{{"Join0", each Table.IsEmpty(_), type logical},{"Join1", each Table.IsEmpty(_), type logical}}),
LocalGroup = Table.Group(Test,
{"SA_ID", "Join0", "Join1"},
{{"START_DT", each List.Min([#"START_DT "]), type nullable date},
{"END_DT", each List.Max([END_DT]), type nullable date}},
GroupKind.Local,
(x,y) => if y[Join0] and y[Join1] then 1 else 0)
in
LocalGroup
Stéphane
Hello,
I don't understand one line, Could you explain plesase? Why have we (x,y) but use only y[join0] and y[join1]?
(x,y) => if y[Join0] and y[Join1] then 1 else 0)
x = first row of group
y = actual row
if y[join0] and y[join1] are true then create new group else group row y with x
Stéphane
Hello,
Could you kindly explain what you do in the LocalGroup step of you query? I am trying to understand how the logic works, but having some trouble. Thanks!
Look here
https://blog.crossjoin.co.uk/2014/01/03/aggregating-by-local-groups-in-power-query/
sort by ID and START DATE before Table.Group
Stéphane
Thank you so much, you are the best ❤️
Thank you for your answer. This solution is very similar to my problem, but I also have rows which END_DT first row is the same to START_DT next row and START_DT = END_DT in one row. For example:
SA_IDSTART_DTEND_DT
| 8702145434 | 2023-05-05 | 2023-05-08 |
| 8702145434 | 2023-05-08 | 2023-05-09 |
| 8702145434 | 2023-05-09 | 2023-05-11 |
| 8702145434 | 2023-05-12 | 2023-05-15 |
| 8702145434 | 2023-05-15 | 2023-05-15 |
| 8702145434 | 2023-05-19 | 2023-05-22 |
| 8702145434 | 2023-05-23 | 2023-05-23 |
| 8702145434 | 2023-05-26 | 2023-05-29 |
| 1132145497 | 2023-05-05 | 2023-05-08 |
| 1132145497 | 2023-05-08 | 2023-05-08 |
| 1132145497 | 2023-05-12 | 2023-05-15 |
| 1132145497 | 2023-05-16 | 2023-05-22 |
| 1132145497 | 2023-05-26 | 2023-05-29 |
Hi
I answered the same question 6 hours ago
Stéphane
Sign up to receive a private message when registration opens and key events begin.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.