Forum Discussion

Flona's avatar
Flona
Regular Visitor
7 years ago
Solved

Extract distinct data from one table (Source) into an new table

Hi I have some problem extracting distinct data from on table into another. I have one large source table with 20k+ rows, but only around 3000 distinct IDs (se example under). I would like to extrac...
  • Zubair_Muhammad's avatar
    7 years ago

    Flona 

     

    You can use Table.Max function in Power Query as follows

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdJBC4MgFAfwryKdPeQz27qPYINOBTtEB7eEdmngEta3X3nIqekuuwiPpz/fX2zb5HwiCV5X1Ij3hNaiUeLV8xmjCx8VlzMiGEFKiqVVyofulOImrVaHPQr+R9GlqNVoSZB6VMXlfXAc0A58pRuUdGYyUvCSEOblY95Um8Usi2qLGusq+tHRYMP8qWlcq/mkpHWABVPmcap62tsPZio3/jEswX7EIhKR2K+faS7T3H5EyH/9ie4D", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Description = _t, #"Start date" = _t, #"End Date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Description", type text}, {"Start date", type date}, {"End Date", type date}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"All", each Table.Max(_,"Start date"), type record}}),
        #"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"ID", "Description", "Start date", "End Date"}, {"ID.1", "Description", "Start date", "End Date"})
    in
        #"Expanded All"

     

  • AlB's avatar
    7 years ago

    Hi Flona 

    If you want it in DAX you can create a new calculated table as follows:

     

     

    NewTable =
    FILTER (
        Table1,
        Table1[ID]
            = CALCULATE ( MAX ( Table1[Start date] ), ALLEXCEPT ( Table1, Table1[ID] ) )
    )

     

  • AlB's avatar
    AlB
    7 years ago

    Flona 

     

    You're quite right. There was an error in the code. Try this slightly modified version:

     

    NewTable = 
    FILTER (
        Table1;
        Table1[Start date]
            = CALCULATE ( MAX ( Table1[Start date] ); ALLEXCEPT ( Table1; Table1[ID] ) )
    )