Forum Discussion
Group rows with same dates and consecutive dates
- 1 year ago
Hi JoMont,
Hope you are doing well.Open advanced editor in power query and copy paste the below M-code.
Duplicate Entry is removed and all columns are retained.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZRNbsIwEIWvErEmyJ78LyuKaBdpK1Gpi5ZFGiwUKXFQEpC4Tc/Sk3VMgdgmfyA2yI7w+2bmPfvzcxTOHgmho/FozjgrotQI2SqJE87w01NebpIqSnFpUZMSEwgAbqhjEio2Fm4+HvAnDEOxPpw9/J/S0XJ8VBdHZhkr1ozH+9v1f39knQbGqYO3IoqrJGbNn6griVvEpNDUyTTPsi1PKsGxg4nveNSuadZgGoE+mqPRwJkEgDyV1je/fo6tTe+SY9/Hpw6XRK+v32XFqiKJSyPiK2O+5xGL8zRf71USWCaBAYmQ9L62GARXk7TdCXoHwW1VKGk5V2FfXYUDEyzCd+sq3GGu0jOd2MeBCPrL+z/d1acNNcEbfic8yVXitbgqp5RS/U74d3hDoPUNCQZMS/EKcGP1ZpOohGmacAE3FqzY4WiM52xT5DuWMV5p6fQkefSIuI3vYVLGLE0jzvJt2dDR4BSCBABiEr+J1pdBvdtut4BKGAiO1nW8+FepiyfqLEjtpnvVlgUE1eqLLZpZaOPCWRHoNEcETRw9qfuqem/SFD9oix9q0rT7P4ijSOPqckpwySF4La17cxr6OXKWfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Medical Registration Number" = _t, #"Discipline Type" = _t, #"Facility Type" = _t, #"Start Date" = _t, #"End Date" = _t, State = _t, MMM = _t, #"Discipline Group" = _t, #"FTE equivalent" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Medical Registration Number", type text}, {"Discipline Type", type text}, {"Facility Type", type text}, {"Start Date", type date}, {"End Date", type date}, {"State", type text}, {"MMM", type text}, {"Discipline Group", type text}, {"FTE equivalent", type number}}), // Sorting by Medical Registration Number and Start Date is important here. // Without applying sorting logic, sometimes gives you different result. Sort_Logic = Table.Sort(#"Changed Type",{{"Medical Registration Number", Order.Ascending}, {"Start Date", Order.Ascending}}), Duplicate_Logic = Table.Distinct(Sort_Logic, {"Medical Registration Number", "Discipline Type", "Facility Type", "Start Date", "End Date", "State", "Discipline Group", "FTE equivalent"}), Group_Logic_1 = Table.Group(Duplicate_Logic, {"Medical Registration Number", "Discipline Type","Facility Type", "State", "MMM", "Discipline Group"}, {{"AllRows", (x) => x}}, GroupKind.Local), //Assuming your data has continuous date period where a medical number is doing two different placements in the same discipline. // If Date range is not continous, sometimes gives you different result. Transform_Logic = Table.TransformColumns(Group_Logic_1, {{"AllRows", (x) => Table.TransformColumns(x, {{"Start Date", (y) => List.Min(x[Start Date])},{"End Date", (y) => List.Max(x[End Date])}})}}), Combine_Logic = Table.Combine(Transform_Logic[AllRows]), Group_Logic_2 = Table.Group(Combine_Logic, {"Medical Registration Number", "Discipline Type", "Facility Type", "Start Date", "End Date", "State", "MMM", "Discipline Group"}, {{"FTE equivalent", each List.Sum([FTE equivalent]), type nullable number}}), Output = Table.TransformColumnTypes(Group_Logic_2,{{"Medical Registration Number", type text}, {"Discipline Type", type text}, {"Start Date", type date}, {"End Date", type date}, {"FTE equivalent", type number}}) in OutputRegards,
Balakrishnan_J
Did I answer your question? If Yes,
Then mark my post as a solution and click on the Thumbs Up 👍 to give Kudos.
Remember: You can mark multiple answers as a solution...
Hi,
Thank you, but I need some additional help. I have this existing code, which creates the column "FTE Equivalent" and I'm not sure how to work in your 'let t = ' statements in the Source line, once I've created the "FTE Equivalent" column.
Here is my existing code:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Removed Columns" = Table.RemoveColumns(Source,{"Placement Number", "Column1", "Facility Name", "Address Line 1", "Address Line 2", "Street Address / Address Line 3", "Suburb/town", "Postcode#(tab)"}),
#"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Discipline Type", Text.Trim, type text}}),
#"Added Conditional Column" = Table.AddColumn(#"Trimmed Text", "Discipline Group", each if [Discipline Type] = "Acute Medicine" then "Medical" else if [Discipline Type] = "Cardiology" then "Medical" else if [Discipline Type] = "General Medicine" then "Medical" else if [Discipline Type] = "Gastroenterology" then "Medical" else if [Discipline Type] = "Geriatrics" then "Medical" else if [Discipline Type] = "Internal Medicine" then "Medical" else if [Discipline Type] = "Medical or Ward Call" then "Medical" else if [Discipline Type] = "Paediatrics" then "Medical" else if [Discipline Type] = "Palliative Care" then "Medical" else if [Discipline Type] = "Respiratory" then "Medical" else if [Discipline Type] = "Rehabilitation" then "Medical" else if [Discipline Type] = "Rural Medicine" then "Medical" else if [Discipline Type] = "Sub Acute" then "Medical" else if [Discipline Type] = "Anaesthetics" then "Anaesthetics" else if [Discipline Type] = "ENT" then "Surgical" else if [Discipline Type] = "General Surgery" then "Surgical" else if [Discipline Type] = "General surgery" then "Surgical" else if [Discipline Type] = "Neurosurgery" then "Surgical" else if [Discipline Type] = "Orthopaedics" then "Surgical" else if [Discipline Type] = "Plastic Surgery" then "Surgical" else if [Discipline Type] = "Emergency Medicine" then "Emergency" else if [Discipline Type] = "Intensive Care Unit" then "ICU" else if [Discipline Type] = "Psychiatry" then "Psychiatry" else if [Discipline Type] = "Mental Health" then "Psychiatry" else if [Discipline Type] = "General Practice" then "Community" else if [Discipline Type] = "Rural Community" then "Community" else if [Discipline Type] = "Residency Program" then "Community" else if [Discipline Type] = "PIERCE" then "Community" else if [Discipline Type] = "Primary Health" then "Community" else if [Discipline Type] = "John Flynn Prevocational Doctor Program" then "Community" else if [Discipline Type] = "Rural Junior Doctor Training Innovation Fund" then "Community" else if [Discipline Type] = "Rural Community Residency Program" then "Community" else if [Discipline Type] = "Obstetrics and Gynaecology" then "Obstetrics & Gynaecology" else if [Discipline Type] = "Radiology" then "Radiology" else if [Discipline Type] = "Clinical Service Improvement" then "Miscellaneous" else if [Discipline Type] = "Relieving" then "Miscellaneous" else if [Discipline Type] = "Research" then "Miscellaneous" else if [Discipline Type] = "Composite" then "Miscellaneous" else if [Discipline Type] = "Medical Education" then "Miscellaneous" else if [Discipline Type] = "Oncology" then "Medical" else if [Discipline Type] = "Neurology" then "Medical" else null),
#"Added Custom" = Table.AddColumn(#"Added Conditional Column", "FTE equivalent", each if [Full time Weeks] > 1 then [Full time Weeks] else ((Duration.Days ([End Date] - [Start Date])+1)/7)*[Full time Weeks]),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Full time Weeks"})
in
#Removed Columns1
Hi,
Thanks so much for the amended code, it works perfectly. However, I lose some of the columns I need, such as Discipline Group, State, MMM (which I had to leave out of my original request to avoid overrunning the character limit)
This info should just be carried across, no need to group (they should be the same for both circumstances of grouping but I think there's one or two that aren't...)
but do I just add these columns to the group statement?
For privacy reasons I can't share the spreadsheet sorry
- ronrsnfld1 year agoSuper User
This message shows you responding to yourself. For clarity, it would be helpful if you could includ the name of the person you are responding to. If you type "@" you will see some choices.
And perhaps you could add sample columns to your data sample with made up data that are representative of your actual data layout. That way things can be tested without violating confidentiality.
- JoMont1 year agoFrequent Visitor
Thanks for the tips, you can tell I haven't used these kind of forums much before 🙂
- Balakrishnan_J1 year agoHelper I
Hi JoMont,
Hope you are doing well.Open advanced editor in power query and copy paste the below M-code.
Duplicate Entry is removed and all columns are retained.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZRNbsIwEIWvErEmyJ78LyuKaBdpK1Gpi5ZFGiwUKXFQEpC4Tc/Sk3VMgdgmfyA2yI7w+2bmPfvzcxTOHgmho/FozjgrotQI2SqJE87w01NebpIqSnFpUZMSEwgAbqhjEio2Fm4+HvAnDEOxPpw9/J/S0XJ8VBdHZhkr1ozH+9v1f39knQbGqYO3IoqrJGbNn6griVvEpNDUyTTPsi1PKsGxg4nveNSuadZgGoE+mqPRwJkEgDyV1je/fo6tTe+SY9/Hpw6XRK+v32XFqiKJSyPiK2O+5xGL8zRf71USWCaBAYmQ9L62GARXk7TdCXoHwW1VKGk5V2FfXYUDEyzCd+sq3GGu0jOd2MeBCPrL+z/d1acNNcEbfic8yVXitbgqp5RS/U74d3hDoPUNCQZMS/EKcGP1ZpOohGmacAE3FqzY4WiM52xT5DuWMV5p6fQkefSIuI3vYVLGLE0jzvJt2dDR4BSCBABiEr+J1pdBvdtut4BKGAiO1nW8+FepiyfqLEjtpnvVlgUE1eqLLZpZaOPCWRHoNEcETRw9qfuqem/SFD9oix9q0rT7P4ijSOPqckpwySF4La17cxr6OXKWfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Medical Registration Number" = _t, #"Discipline Type" = _t, #"Facility Type" = _t, #"Start Date" = _t, #"End Date" = _t, State = _t, MMM = _t, #"Discipline Group" = _t, #"FTE equivalent" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Medical Registration Number", type text}, {"Discipline Type", type text}, {"Facility Type", type text}, {"Start Date", type date}, {"End Date", type date}, {"State", type text}, {"MMM", type text}, {"Discipline Group", type text}, {"FTE equivalent", type number}}), // Sorting by Medical Registration Number and Start Date is important here. // Without applying sorting logic, sometimes gives you different result. Sort_Logic = Table.Sort(#"Changed Type",{{"Medical Registration Number", Order.Ascending}, {"Start Date", Order.Ascending}}), Duplicate_Logic = Table.Distinct(Sort_Logic, {"Medical Registration Number", "Discipline Type", "Facility Type", "Start Date", "End Date", "State", "Discipline Group", "FTE equivalent"}), Group_Logic_1 = Table.Group(Duplicate_Logic, {"Medical Registration Number", "Discipline Type","Facility Type", "State", "MMM", "Discipline Group"}, {{"AllRows", (x) => x}}, GroupKind.Local), //Assuming your data has continuous date period where a medical number is doing two different placements in the same discipline. // If Date range is not continous, sometimes gives you different result. Transform_Logic = Table.TransformColumns(Group_Logic_1, {{"AllRows", (x) => Table.TransformColumns(x, {{"Start Date", (y) => List.Min(x[Start Date])},{"End Date", (y) => List.Max(x[End Date])}})}}), Combine_Logic = Table.Combine(Transform_Logic[AllRows]), Group_Logic_2 = Table.Group(Combine_Logic, {"Medical Registration Number", "Discipline Type", "Facility Type", "Start Date", "End Date", "State", "MMM", "Discipline Group"}, {{"FTE equivalent", each List.Sum([FTE equivalent]), type nullable number}}), Output = Table.TransformColumnTypes(Group_Logic_2,{{"Medical Registration Number", type text}, {"Discipline Type", type text}, {"Start Date", type date}, {"End Date", type date}, {"FTE equivalent", type number}}) in OutputRegards,
Balakrishnan_J
Did I answer your question? If Yes,
Then mark my post as a solution and click on the Thumbs Up 👍 to give Kudos.
Remember: You can mark multiple answers as a solution...