Forum Discussion

20 Replies

  • Ok, this is pretty weird.


    I ran the tests again, but instead of reordering the table using Table.Sort I just entered the data in a different order from the start.

    Here are the results:

    (For some reason I can't embed images even though I had no such problem yesterday. Sorry if this is harder to follow or the images eventually go down)

     

    Source

    Distinct

    Reordered Source

    Reordered Distinct

     

    Code:

     

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclLSUUpUitWJVnJEYSUpxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t]),
        Example = Table.TransformColumnTypes(Source,{{"a", type text}, {"b", type text}}),
        ExampleResult = Table.Distinct(Example, {"b"}),
        ReorderedExample = Table.Sort(Example,{{"a", Order.Ascending}}),
        ReorderedExampleResult = Table.Distinct(ReorderedExample, {"b"})
    in
        ReorderedExampleResult

     

     

     

     

     

    We can easily see that order is indeed important, but that for some reason reordering the table using Table.Sort doesn't make a difference. I found that if we need to, we can use Table.Buffer to make Table.Distinct take into account the changes made by Table.Sort. Here's my third and final test:

     

    Source

    Distinct

    Reordered Source

    Reordered Distinct

     

    Code:

     

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpUitWJVnKCs0BiSUqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [a = _t, b = _t]),
        Example = Table.TransformColumnTypes(Source,{{"a", type text}, {"b", type text}}),
        ExampleResult = Table.Distinct(Example, {"b"}),
        ReorderedExample = Table.Buffer( Table.Sort(Example,{{"a", Order.Descending}}) ),
        ReorderedExampleResult = Table.Distinct(ReorderedExample, {"b"})
    in
        ReorderedExampleResult

     

     

     

     

    • robertCharlton's avatar
      robertCharlton
      Frequent Visitor

      Thank you for doing the work of looking into this and identifying a solution to this frustrating issue.

      • Anonymous's avatar
        Anonymous
        Not applicable

        it seems as if the sort operation doesn't change the table from the point of view of the table.distinct function and uses the same result as before.(*)
        The operations that "change" the table (manual reordering, table.buffer, adding an index column, replacing any value in the table - even if the new value is the same as the old one -, changing the type of a column) do " start " the procedure of the table.distinct function all over again

         

         

         

         


        (*) if between the call to the table.distinct function and the definition of the table to which it is applied, only table.sort functions act, the table distinct function is applied to the original table "saving" the sorting operation which is generally heavy.
        Maybe ‌ 😁

      • PiR's avatar
        PiR
        New Member

        Hello,

        Just to notice that using grouping might lead to a huge computation time. Furthermore, if you want to keep only one row, using Max as the aggregation function might not work if there are multiple rows with the max value. Therefore you may think to use Table.FirstN as an aggregation function: same issue, it would burst your memory.

        Therefore I think using Table.Buffer is the best idea when it comes to sorting

        Best

    • Ehren's avatar
      Ehren
      Microsoft Employee

      I just wanted to let you know I've updated the docs page above based on this thread. Hopefully it's more relevant/helpful now.

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    I believe it keeps the first one.  You can do quick try to confirm.  You can add a sort step just before that step to keep the one you want.

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

    • pavel_severov's avatar
      pavel_severov
      Advocate I

      Hi mahoneypatI, I also "believe" in "First row", but it is good to have official confirmation from MS. Isn't it? 🙂

      • v-lid-msft's avatar
        v-lid-msft
        Community Support

        Hi pavel_severov ,

         

        Sorry for that, but we did not find any logic definition of this function in official document. But you can confirm it by the Example 2 of the function document, it will keep the first row.

         


        Best regards,

  • Anonymous's avatar
    Anonymous
    Not applicable

    You are forgetting that if you want to remove distinct letters that are different cases (A/a) then you need to add the optional parameter Comparer.OrdinalIgnoreCase

    Table.Distinct(Table, {{“b”,Comparer.OrdinalIgnoreCase}})

    This will treat A and a as duplicates. Otherwise, A/a are not the same in this function. 
    --Nate

  • Belin's avatar
    Belin
    Frequent Visitor

    I just found out the hard way.

    the algorithm goies top to bottom, so I having a set like this

     

    number updated_on

    inc0001 01/01/22

    inc0001 02/01/22

     

    would remove the second occurrence from the top.

    I wanted to keep the most updated record, so i sorted the data first, but still the algorithm remove the same  record, ignoring any sorting

    At the end I'm 99% positive that eliminate from top to bottom every reoccurrence...hoping that is not using a something faster and more difficult to predict and control.

    In the past the same function in excel worked this way, so I hope is still the same

    Cheers