Forum Discussion

RyanL-EIS's avatar
RyanL-EIS
Frequent Visitor
9 years ago

Latest Record in Group via Query Editor

We are trying to get the latest record/row in a group via Query Editor.  After we expand the group and we filter on the latest record, it filters the entire dataset and not within the group.  Please see below.

 

 

let
Data = Source{[Name="Data"]}[Data],
dbo_PercentOfProceeds = Data{[Schema="dbo",Item="PercentOfProceeds"]}[Data],
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"settleID", "productionDate"}, {{"SettleID-Statement_Group", each _, type table}}),
#"Expanded SettleID-Statement_Group" = Table.ExpandTableColumn(#"Grouped Rows", "SettleID-Statement_Group", {"fileAndPage", "statementDate"}, {"SettleID-Statement_Group.fileAndPage", "SettleID-Statement_Group.statementDate"}),

#"Latest Records" = Table.SelectRows(#"Expanded SettleID-Statement_Group", let latest = List.Max(#"Expanded SettleID-Statement_Group"[#"SettleID-Statement_Group.statementDate"]) in each [#"SettleID-Statement_Group.statementDate"] = latest)

 

in

#"Latest Records"

 

Any suggestions?

 

Thanks for your help!

5 Replies

  • MarcelBeug's avatar
    MarcelBeug
    Community Champion

    The way to go is to adjust the generated code for #"Grouped Rows", more specifically the part each _

    Here you can specify the selection.

     

    This should work, but I can't test of course:

     

    #"Grouped Rows" = Table.Group(#"Removed Duplicates", {"settleID", "productionDate"}, {{"SettleID-Statement_Group", each Table.SelectRows(_, let latest = List.Max(_[#"SettleID-Statement_Group.statementDate"]) in each [#"SettleID-Statement_Group.statementDate"] = latest), type table}}),

     

     

    • RyanL-EIS's avatar
      RyanL-EIS
      Frequent Visitor

      Hi Marcel,

       

      Thanks for your help and we believe we are close.  The logic behind your code makes sense for what we are trying to accomplish; however, we are getting the following error trying to preview/expand the subtable of the grouped rows.

       

      Expression.Error: The field 'SettleID-Statement_Group.statementDate' of the record wasn't found.

      Any idea?

      • RyanL-EIS's avatar
        RyanL-EIS
        Frequent Visitor

        Also, should we be trying to select/filter on grouped rows at the query level (Query Editor) OR it is better to create a calculated table instead?  If so, any suggestions on the DAX for the calculated table?

    • vgeldbr's avatar
      vgeldbr
      Helper IV

      This formula solved my problem for me (thanks!). I'm using it in a dataflow. However, it performs extremely slowly. Is that expected?

       

      I'm doing a Table.Group on a linked table (based on some ingested and transformed Excel files) that is 1,996,452 rows and 19 columns. Unfortuntely the columns are all text type except for a date column and I'm using the technique described her to get the latest dated row from the grouping. I then need to expand the table to get the columns from that row.  MarcelBeug