Forum Discussion
pavel_severov
6 years agoAdvocate I
Which row will keep Table.Distinct(Table, {"Column"})
Hi colleagues, Which row will keep Table.Distinct(Table, {"Column"}) for the rows with the same values in the "Column": First row? Last row? Random row? MS documentation has no explanation abo...
tmijail
6 years agoAdvocate I
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)
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:
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
RShackelford
5 years agoFrequent Visitor
Thanks! This was a big help.