Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
PowerBeeEye
Employee
Employee

Find row with latest date and keep one

Form a table, I am trying to pick the each ForestDNSName

 

Scan DateForestDNSNameColumn1
2020-09-15Fabrikam.com 
2020-09-05Contoso.com 
2020-09-15Contoso.com 
2020-09-15Contoso.com 

 

Using the following lines:

 

    Row_Group_by_ForestDNSName = Table.Group(Row_Filter, {"ForestDNSName"}, {{"Count", each _, type table}}),
    Extract_Latest_Data = (TT as table) =>
  Table.SelectRows(TT, each [Scan Date] = List.Max(TT[Scan Date])),
    Custom_Column_LatestData = Table.AddColumn(Row_Group_by_ForestDNSName, "LatestData", each Extract_Latest_Data([Count])),
    Column_Remove_Except_LatestData = Table.RemoveColumns(Custom_Column_LatestData,{"ForestDNSName", "Count"}),

 

The problem arises when there are multiple rows with the same (latest) Scan Date.

How can I edit the lines to pick the top row in the resulting table?

 

Scan DateForestDNSNameColumn1
2020-09-15Fabrikam.com 
2020-09-15Contoso.com 
1 ACCEPTED SOLUTION

@PowerBeeEye 

Continuing on the code you had above:


    Row_Group_by_ForestDNSName = Table.Group(Row_Filter, {"ForestDNSName"}, {{"Count", each _, type table}}),
    Extract_Latest_Data = (TT as table) =>
  Table.SelectRows(TT, each [Scan Date] = List.Max(TT[Scan Date])),
    Custom_Column_LatestData = Table.AddColumn(Row_Group_by_ForestDNSName, "LatestData", each Extract_Latest_Data([Count])),
    Column_Remove_Except_LatestData = Table.RemoveColumns(Custom_Column_LatestData,{"ForestDNSName", "Count"}),

    #"Added Custom" = Table.AddColumn(Column_Remove_Except_LatestData, "Custom", each Table.FirstN([LatestData],1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Scan Date", "ForestDNSName"}, {"Scan Date", "ForestDNSName"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"LatestData"})

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

View solution in original post

4 REPLIES 4
Jimmy801
Community Champion
Community Champion

Hello @PowerBeeEye 

 

the easiest and shortest way to do is to put all what you needed in the Group-function and expand it later

here the code snipped for the function of the group function (you can substitute with your function, and it will work). No need to create an additional column to extract etc.

        {
            {
                "PickUpLatest", 
                (tbl)=> Table.FirstN(Table.SelectRows(tbl, (row)=> row[Scan Date]= List.Max(tbl[Scan Date])),1)
            }
        }

 

here a complete code example to see how the above function is working

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtA1sNQ1NFXSUXJLTCrKzE7M1UvOzwVyFZRidRAqDEAqnPPzSvKL87EqMCRPQSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Scan Date" = _t, ForestDNSName = _t, Column1 = _t]),
    ChangeType = Table.TransformColumnTypes(Source,{{"Scan Date", type date, "en-US"}, {"ForestDNSName", type text}, {"Column1", type text}}),
    GroupAndPickUpLatest = Table.Group
    (
        ChangeType, 
        {"ForestDNSName"}, 
        {
            {
                "PickUpLatest", 
                (tbl)=> Table.FirstN(Table.SelectRows(tbl, (row)=> row[Scan Date]= List.Max(tbl[Scan Date])),1)
            }
        }
    ),
    PickUpLatest = Table.ExpandTableColumn(GroupAndPickUpLatest, "PickUpLatest", {"Scan Date", "Column1"}, {"Scan Date", "Column1"})
in
    PickUpLatest

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

AlB
Super User
Super User

Hi @PowerBeeEye 

Try Table.FirstN to keep the first row only:

Table.FirstN(TableName , 1)

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

@AlB  I'm very much a beginner when **bleep** comes to this, how can I add 

Table.FirstN(TableName , 1)

 to either of the following lines?

 

    Extract_Latest_Data = (TT as table) =>
  Table.SelectRows(TT, each [Scan Date] = List.Max(TT[Scan Date])),

 

OR

 

    Custom_Column_LatestData = Table.AddColumn(Row_Group_by_ForestDNSName, "LatestData", each Extract_Latest_Data([Count])),

 

Thanks!

@PowerBeeEye 

Continuing on the code you had above:


    Row_Group_by_ForestDNSName = Table.Group(Row_Filter, {"ForestDNSName"}, {{"Count", each _, type table}}),
    Extract_Latest_Data = (TT as table) =>
  Table.SelectRows(TT, each [Scan Date] = List.Max(TT[Scan Date])),
    Custom_Column_LatestData = Table.AddColumn(Row_Group_by_ForestDNSName, "LatestData", each Extract_Latest_Data([Count])),
    Column_Remove_Except_LatestData = Table.RemoveColumns(Custom_Column_LatestData,{"ForestDNSName", "Count"}),

    #"Added Custom" = Table.AddColumn(Column_Remove_Except_LatestData, "Custom", each Table.FirstN([LatestData],1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Scan Date", "ForestDNSName"}, {"Scan Date", "ForestDNSName"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"LatestData"})

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors