Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

permutation in power query (multiple rows and same columns)

Hello,

 

I am trying to create a permutation in Power Query with the following data:

NameCountryType
Jason DEA
MarryFRB
PaulITC
 US 

 

I am trying to get an output like this:

JasonDEA
JasonDEB
JasonDEC
JasonFRA
JasonFRB
JasonFRC
JasonITA
JasonITB
JasonITC
JasonUSA
JasonUSB
JasonUSC
MarryDEA
MarryDEB
MarryDEC
MarryFRA
MarryFRB
MarryFRC
MarryITA
MarryITB
MarryITC
MarryUSA
MarryUSB
MarryUSC
PaulDEA
PaulDEB
PaulDEC
PaulFRA
PaulFRB
PaulFRC
PaulITA
PaulITB
PaulITC
PaulUSA
PaulUSB
PaulUSC

 

So in a way keep the columns separated but make a match for each column 1 with column 2 and column 3 combined. Is this possible with Power Query? Thanks for the help in advance. 

2 Replies

  • ERD's avatar
    ERD
    Icon for Community Champion rankCommunity Champion

    Anonymous ,

    Here is one of the ways to get Cartesian product of columns within same query (it gets rid of blank values, but doesn't imply duplicates existence. If you have duplicated values, the code will be a bit different):

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8koszs9TUNJRcnEFEo5KsTrRSr6JRUWVQJ5bEJBwAgsFJJbmADmeIUDCGSwC0hMaDCQUlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Country = _t, Type = _t]),
        RemovedColumns = Table.RemoveColumns(Source,{"Country", "Type"}),
        #"Filtered Rows" = Table.SelectRows(RemovedColumns, each ([Name] <> " ")),
        #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Country", each Table.SelectRows(
            Table.FromList(Source[Country], Splitter.SplitByNothing(), null, null, ExtraValues.Error), 
            each ([Column1] <> " "))),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Type", each Table.SelectRows(
            Table.FromList(Source[Type], Splitter.SplitByNothing(), null, null, ExtraValues.Error), 
            each ([Column1] <> " "))),
        #"Expanded Country" = Table.ExpandTableColumn(#"Added Custom1", "Country", {"Column1"}, {"Country"}),
        #"Expanded Type" = Table.ExpandTableColumn(#"Expanded Country", "Type", {"Column1"}, {"Type"})
    in
        #"Expanded Type"

     

    If this post helps, then please consider Accept it as the solution ✔️to help the other members find it more quickly.