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
lmgibs04
Regular Visitor

Cartesian Product of String Lists in M

 
Hello Everyone, 

 

I'm looking to create a cartesian product of two lists of strings through a function in M. Imagine I have lists {"a","b","c"} and {"1","2","3"} and I want to create a list of lists such that the cartesian product is as follows {{"a1","b1","c1"},{"a2","b2","c2"},{"a3","b3","c3"}}.

 

How could I approach this? 

 

I've considered using a List.Generate but can't seem to get the right arguments that don't return an error. I was also looking at using List.Transform but again couldn't get it to work. 

 

Any help is appreciated. 

 

Thanks!

2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

You may use this

let
list1 = {"a", "b", "c", "d"},
list2 = {1, 2, 3}
in
List.Split(List.TransformMany(list2, (x)=>list1, (x,y)=> y & Text.From(x)), List.Count(list1))

View solution in original post

v-junyant-msft
Community Support
Community Support

Hi @lmgibs04 ,

@Vijay_A_Verma Good Answer!
And you can also try this:

 

let
    List1 = {"a", "b", "c"},
    List2 = {"1", "2", "3"},
    Product = List.Transform(List2, each let currentItem = _ in List.Transform(List1, each _ & currentItem)),
    Result = List.Zip(Product),
    #"Converted to Table" = Table.FromList(Result, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Extracted Values" = Table.TransformColumns(#"Converted to Table", {"Column1", each Text.Combine(List.Transform(_, Text.From), "#(tab)"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Column1", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}})
in
    #"Changed Type"

 

Just put all of this M function into Advanced Editor, and the final output is as below:

vjunyantmsft_0-1708932097193.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept both of our answers as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-junyant-msft
Community Support
Community Support

Hi @lmgibs04 ,

@Vijay_A_Verma Good Answer!
And you can also try this:

 

let
    List1 = {"a", "b", "c"},
    List2 = {"1", "2", "3"},
    Product = List.Transform(List2, each let currentItem = _ in List.Transform(List1, each _ & currentItem)),
    Result = List.Zip(Product),
    #"Converted to Table" = Table.FromList(Result, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Extracted Values" = Table.TransformColumns(#"Converted to Table", {"Column1", each Text.Combine(List.Transform(_, Text.From), "#(tab)"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Column1", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}})
in
    #"Changed Type"

 

Just put all of this M function into Advanced Editor, and the final output is as below:

vjunyantmsft_0-1708932097193.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept both of our answers as the solution to help the other members find it more quickly.

Vijay_A_Verma
Super User
Super User

You may use this

let
list1 = {"a", "b", "c", "d"},
list2 = {1, 2, 3}
in
List.Split(List.TransformMany(list2, (x)=>list1, (x,y)=> y & Text.From(x)), List.Count(list1))

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
Top Kudoed Authors