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.
Hi,
On of the column in my table consists of lists. I am looking for a solution to convert the lists in a way that repeating values that follow one after another are replaced by single value.
Ex.
[A,A,A,B,B,C,C,B,B,B,C,C,C,A] => [A,B,C,B,C,A]
[C,C,B,B,A,A,C,C,B,A,A,B] = > [C,B,A,C,B,A,B]
Could you please share your suggestions?
Solved! Go to Solution.
Hi @pnapiera
Your example looks more like records :). If they are actually lists.
You can use this:
let
Source = {"A","A","A","B","B", "C","C","B","B","C","C","C","A"},
ToTable = Table.FromColumns({Source}),
Distill = Table.Group(ToTable, {"Column1"}, {}, GroupKind.Local)[Column1]
in
Distill
or this:
let
Source = {"A","A","A","B","B", "C","C","B","B","C","C","C","A"},
Distill = List.Accumulate(Source, {}, (s,a) => if a = List.Last(s) then s else s & {a})
in
Distill
Kind regards,
JB
Hi @pnapiera
jborro's solution is great if you have one column in table.
If you have example data as below, you could modify jborro's method as below to make it work.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [index = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"index", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "list", each {"A","A","A","B","B", "C","C","B","B","C","C","C","A"}),
Custom1 = Table.ReplaceValue(#"Added Custom",each [list],each Function.Invoke((_list as list)=> List.Accumulate(_list, {}, (s,a) => if a = List.Last(s) then s else s & {a}),{[list]}),Replacer.ReplaceValue,{"list"}),
#"Expanded list" = Table.ExpandListColumn(Custom1, "list")
in
#"Expanded list"
Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @pnapiera
jborro's solution is great if you have one column in table.
If you have example data as below, you could modify jborro's method as below to make it work.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [index = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"index", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "list", each {"A","A","A","B","B", "C","C","B","B","C","C","C","A"}),
Custom1 = Table.ReplaceValue(#"Added Custom",each [list],each Function.Invoke((_list as list)=> List.Accumulate(_list, {}, (s,a) => if a = List.Last(s) then s else s & {a}),{[list]}),Replacer.ReplaceValue,{"list"}),
#"Expanded list" = Table.ExpandListColumn(Custom1, "list")
in
#"Expanded list"
Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thank you very much @Anonymous and @v-juanli-msft .
Both answers are helpful and this is exactly what I needed. Moreover I learned about the power of List.Accumulate.
Hi @pnapiera
Your example looks more like records :). If they are actually lists.
You can use this:
let
Source = {"A","A","A","B","B", "C","C","B","B","C","C","C","A"},
ToTable = Table.FromColumns({Source}),
Distill = Table.Group(ToTable, {"Column1"}, {}, GroupKind.Local)[Column1]
in
Distill
or this:
let
Source = {"A","A","A","B","B", "C","C","B","B","C","C","C","A"},
Distill = List.Accumulate(Source, {}, (s,a) => if a = List.Last(s) then s else s & {a})
in
Distill
Kind regards,
JB
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
102 | |
53 | |
21 | |
12 | |
12 |