Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
danextian
Super User
Super User

Select items from a list based on items from another list

Hi All,

 

One of my columns contains a list of values while another one has the positions. What I'm trying to achieve is select items from the values list based on the positions.

 

I've attempted to follow the structure of this formula

 

= List.Transform({0,3,4,6}, each {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}{_})

which returns {"A","D","E","G"} but I would get an error but I would get this error: Expression.Error: We cannot apply field access to the type Number.

 

Here's my M including my attempt:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUQpILCqp9EvMTVVRVnFJzANRwfk5KSH5QIahkbGJqRmQ4ZxfmldSVAlkheZllqSmKASXJJakFoPUghhA2i+1XCEyvyhbKVYHbLQR0Gi4OWbmFpYGKMY4J+YlpiQi6fdNBBqcn5SoFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Transaction Number" = _t, String = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction Number", type text}, {"String", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Delimiter Occurence", each ( (Text.Length([String]) - Text.Length( Text.Replace([String], "$#$", "") ) )/Text.Length("$#$") ) + 1, Int64.Type),
    MaxOccurence = List.Max(#"Added Custom"[Delimiter Occurence]),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "String", Splitter.SplitTextByDelimiter("$#$"),
MaxOccurence),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"String.1", type text}, {"String.2", type text}, {"String.3", type text}, {"String.4", type text}, {"String.5", type text}, {"String.6", type text}, {"String.7", type text}, {"String.8", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Delimiter Occurence"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns", "ListWithValues", each List.Skip(Record.FieldValues(_))),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "ListWithPositions", each List.Select(List.Positions([ListWithValues]), each Number.IsEven(_))),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "SelectedList", each List.Transform([ListWithPositions], each [ListWithValues]{_})),
    SelectedList = #"Added Custom3"{0}[SelectedList],
    SelectedList1 = SelectedList{0}
in
    SelectedList1




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @danextian,

 

Please try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUQpILCqp9EvMTVVRVnFJzANRwfk5KSH5QIahkbGJqRmQ4ZxfmldSVAlkheZllqSmKASXJJakFoPUghhA2i+1XCEyvyhbKVYHbLQR0Gi4OWbmFpYGKMY4J+YlpiQi6fdNBBqcn5SoFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Transaction Number" = _t, String = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction Number", type text}, {"String", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Delimiter Occurence", each ( (Text.Length([String]) - Text.Length( Text.Replace([String], "$#$", "") ) )/Text.Length("$#$") ) + 1, Int64.Type),
    MaxOccurence = List.Max(#"Added Custom"[Delimiter Occurence]),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "String", Splitter.SplitTextByDelimiter("$#$"),
MaxOccurence),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"String.1", type text}, {"String.2", type text}, {"String.3", type text}, {"String.4", type text}, {"String.5", type text}, {"String.6", type text}, {"String.7", type text}, {"String.8", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Delimiter Occurence"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns", "ListWithValues", each List.Skip(Record.FieldValues(_))),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "ListWithPositions", each List.Select(List.Positions([ListWithValues]), each Number.IsEven(_))),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "SelectedList", each Function.Invoke((list1 as list, list2 as list )=>List.Transform(list1, each list2{_}),{[ListWithPositions],[ListWithValues]}))
in
    #"Added Custom3"

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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

2 REPLIES 2
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @danextian,

 

Please try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUQpILCqp9EvMTVVRVnFJzANRwfk5KSH5QIahkbGJqRmQ4ZxfmldSVAlkheZllqSmKASXJJakFoPUghhA2i+1XCEyvyhbKVYHbLQR0Gi4OWbmFpYGKMY4J+YlpiQi6fdNBBqcn5SoFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Transaction Number" = _t, String = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction Number", type text}, {"String", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Delimiter Occurence", each ( (Text.Length([String]) - Text.Length( Text.Replace([String], "$#$", "") ) )/Text.Length("$#$") ) + 1, Int64.Type),
    MaxOccurence = List.Max(#"Added Custom"[Delimiter Occurence]),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "String", Splitter.SplitTextByDelimiter("$#$"),
MaxOccurence),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"String.1", type text}, {"String.2", type text}, {"String.3", type text}, {"String.4", type text}, {"String.5", type text}, {"String.6", type text}, {"String.7", type text}, {"String.8", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Delimiter Occurence"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns", "ListWithValues", each List.Skip(Record.FieldValues(_))),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "ListWithPositions", each List.Select(List.Positions([ListWithValues]), each Number.IsEven(_))),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "SelectedList", each Function.Invoke((list1 as list, list2 as list )=>List.Transform(list1, each list2{_}),{[ListWithPositions],[ListWithValues]}))
in
    #"Added Custom3"

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks! This works like a charm.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.