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
pnapiera
Frequent Visitor

Replacing repeating (following) values of a list by a single value

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?

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

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

View solution in original post

v-juanli-msft
Community Support
Community Support

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"

Capture9.JPGCapture10.JPG

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.

View solution in original post

3 REPLIES 3
v-juanli-msft
Community Support
Community Support

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"

Capture9.JPGCapture10.JPG

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.

Anonymous
Not applicable

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

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