Forum Discussion

bluebird9's avatar
bluebird9
Regular Visitor
4 years ago
Solved

Keep first and last from grouped rows

Dear Power BI Community   Using an individual's Unique ID, I am trying to pair data using their first and last scores. Ideally I would like to keep all data in that row (e.g., raw score and T score...
  • Vijay_A_Verma's avatar
    4 years ago

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test.

     

    let
        Source = Table.Combine({Full1, MDD1, GAD1, PD1, OCD1, SAD1, SOC1}),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"EVENT_TIME", type time}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Service ID", Order.Ascending}, {"Patient ID", Order.Ascending}, {"Date of referral", Order.Ascending}, {"EVENT_DATE", Order.Ascending}, {"EVENT_TIME", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
        #"Grouped Rows" = Table.Group(#"Added Index", {"Unique ID"}, {{"Count", each _, type table [Service name=nullable text, Service ID=nullable number, Patient ID=nullable number, Date of referral=nullable date, Unique ID=nullable text, EVENT_DATE=nullable date, EVENT_TIME=nullable time, Unique Appointment ID=nullable text, GENDER=nullable text, Gender code=nullable number, Age=nullable number, Grade=nullable number, SAD raw=nullable number, GAD raw=nullable number, PD raw=nullable number, SOC raw=nullable number, OCD raw=nullable number, MDD raw=nullable number, Total Anxiety raw=nullable number, Total RCADS raw=nullable number, SAD T=nullable number, GAD T=nullable number, PD T=nullable number, SOC T=nullable number, OCD T=nullable number, MDD T=nullable number, Total Anxiety T=nullable number, Total RCADS T=nullable number, Index=number]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Sub Order", 1, 1)),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Service name", "Service ID", "Patient ID", "Date of referral", "EVENT_DATE", "EVENT_TIME", "Unique Appointment ID", "GENDER", "Gender code", "Age", "Grade", "SAD raw", "GAD raw", "PD raw", "SOC raw", "OCD raw", "MDD raw", "Total Anxiety raw", "Total RCADS raw", "SAD T", "GAD T", "PD T", "SOC T", "OCD T", "MDD T", "Total Anxiety T", "Total RCADS T", "Index", "Sub Order"}, {"Service name", "Service ID", "Patient ID", "Date of referral", "EVENT_DATE", "EVENT_TIME", "Unique Appointment ID", "GENDER", "Gender code", "Age", "Grade", "SAD raw", "GAD raw", "PD raw", "SOC raw", "OCD raw", "MDD raw", "Total Anxiety raw", "Total RCADS raw", "SAD T", "GAD T", "PD T", "SOC T", "OCD T", "MDD T", "Total Anxiety T", "Total RCADS T", "Index", "Sub Order"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Count"}),
        GroupedRows = Table.Group(#"Changed Type", {"Unique ID"}, {{"Min", each List.Min([Sub Order]), type nullable number}, {"Max", each List.Max([Sub Order]), type nullable number}}),
        AddedCustom = Table.AddColumn(GroupedRows, "Custom", each {[Min]}&{[Max]}),
        ExpandedCustom = Table.ExpandListColumn(AddedCustom, "Custom"),
        RemovedColumns = Table.RemoveColumns(ExpandedCustom,{"Min", "Max"}),
        MergedQueries = Table.NestedJoin(#"Changed Type", {"Unique ID", "Sub Order"}, RemovedColumns, {"Unique ID", "Custom"}, "Removed Columns", JoinKind.LeftOuter),
        ExpandedRemovedColumns = Table.ExpandTableColumn(MergedQueries, "Removed Columns", {"Unique ID"}, {"Unique ID.1"}),
        FilteredRows = Table.SelectRows(ExpandedRemovedColumns, each ([Unique ID.1] <> null)),
        RemovedColumns1 = Table.RemoveColumns(FilteredRows,{"Unique ID.1"})
    in
        RemovedColumns1