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).

I have grouped rows using an index column and named it 'Sub Order'. I am trying to select the first row and the last row. The first row is always 1 in 'Sub Order', however the last row will differ. I'm struggling to find a way to select first and last row, and delete everything else.

 

I have included a table of the example data, a table of the idea output, and the M code. 

 

I would be grateful for any advice.

 

Many thanks!

 

Example data

Unique IDUnique Appointment IDRaw scoreT scoreSub Order
001001-8-0017351
001001-8-00212452
001001-8-0033223
001001-8-0044254
002002-2-0019241
002002-2-00213442
002002-2-0036173
002002-2-0041204
002002-2-0058235

 

Ideal output

Unique IDUnique Appointment IDRaw scoreT scoreSub Order
001001-8-0017351
001001-8-0044254
002002-2-0019241
002002-2-0058235

 

 

Advanced Editor code:

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"})
in
#"Removed Columns"

  • 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

     

     

11 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    NewStep=Table.Combine(Table.Group(#"Sorted Rows","Unique ID",{"n",each if Table.RowCount(_)<3 then _ else Table.FirstN(_,1)&Table.LastN(_,1)})[n])

  • HotChilli's avatar
    HotChilli
    Community Champion

    One way to do this is to Group on the ID, returning aggregations on Max(sub order), Min(sub order) and 'all rows'.

    Expand the 'all rows' in the next stage and use Table.SelectRows to keep the rows where SubOrder = the min or max.

    Have a go and I'll help if you get stuck

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    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

     

     

    • bluebird9's avatar
      bluebird9
      Regular Visitor

      Thank you Vijay_A_Verma for taking the time to write out the code for me. After the smallest tweak, the code worked perfectly. Thank you very much for your help.

  • Jakinta's avatar
    Jakinta
    Solution Sage

    Or like this...

     

    let
        Source = YourPreviousStep,
        #"Grouped Rows" = Table.Group(Source, {"Unique ID"}, {{"All", each Table.FirstN(Table.Sort(_,{{"Sub Order", Order.Ascending}}) ,1) & Table.LastN(Table.Sort(_,{{"Sub Order", Order.Ascending}}) ,1), type table [Unique ID=number, Unique Appointment ID=text, Raw score=number, T score=number, Sub Order=number]}}),
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Unique ID"}),
        #"Expanded All" = Table.ExpandTableColumn(#"Removed Columns", "All", {"Unique ID", "Unique Appointment ID", "Raw score", "T score", "Sub Order"}, {"Unique ID", "Unique Appointment ID", "Raw score", "T score", "Sub Order"})
    in
        #"Expanded All"
    • wdx223_Daniel's avatar
      wdx223_Daniel
      Community Champion

      this may give a duplicated row when there is only one row for a ID 

      • bluebird9's avatar
        bluebird9
        Regular Visitor

        Thank you Jakinta and wdx223_Daniel for your combined efforts on this problem.

        wdx223_Daniel - when I added your next step to the M code provided by Jakinta, it worked perfectly! Thank you both again.

  • bluebird9's avatar
    bluebird9
    Regular Visitor

    Thank you HotChilli for taking the time to answer my question. I managed to to get as far as Table.SelectRows, but when I run the code, the data disappears, leaving only the headings. I will do my best to figure it out, but if you have any suggestions as to why this might be happening, or ways to problem solve, I would be interested in hearing them.

    This is the final line of code: = Table.SelectRows(#"Expanded All rows", each ([#"Sub Area No."] = "Min" and "Max"))

    Many thanks

    • HotChilli's avatar
      HotChilli
      Community Champion

      Respect to you for getting in there and trying it.

      The last line will be something like:

      Table.SelectRows(#"Expanded All rows", each [#"Sub Area No."] = [Min] or [#"Sub Area No."] = [Max])

      Check that the datatypes match too.

      All the best

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hello I have this problem when I connect to Oracle Data Base,  My laptop have a windows 11 Pro.

        "El proveedor que se está usando está desusado: 'System.Data.OracleClient requiere la version 8.1.7 o posterior del software cliente de Oracle.'. Visite https://go.microsoft.com/fwlink/p/?LinkID=272376 para instalar el proveedor oficial."