Forum Discussion
kta87
3 years agoHelper I
Create Table with all the possible group combinations
Hi Everyone, Is there any function in power query that can create a table with all the possibile group combinations? I have one table (Table1) with Customer ID, DOC ID and value, and I've man...
Jakinta
2 years agoSolution Sage
By using great code from this post by AlexisOlson , this is one solution that should be reworked, tweaked and turned into a function.
1st requirement
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLzdPYHUsamSrE60UpGCBEjAwOwkDFCyAQqZIIQ0jUFCsUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"DOC ID" = _t, #"Customer ID" = _t, Value = _t]),
Type = Table.TransformColumnTypes(Source,{{"DOC ID", Int64.Type}, {"Customer ID", type text}, {"Value", Int64.Type}}),
L=Type[DOC ID],
L2= Type[Value],
N = List.Count(L),
Subsets =
List.Transform(
{0..Number.Power(2, N)-1},
(i) => List.Transform(
{0..N-1},
(j) => if Number.Mod(Number.IntegerDivide(i, Number.Power(2, j)), 2) = 1
then L{j}
else null
)
),
Custom1 = List.Select(List.Transform(Subsets, each let l=List.RemoveNulls(_), t= List.Transform(l, Text.From) in Text.Combine(t)), each _ <> ""),
Subsets2 =
List.Transform(
{0..Number.Power(2, N)-1},
(i) => List.Transform(
{0..N-1},
(j) => if Number.Mod(Number.IntegerDivide(i, Number.Power(2, j)), 2) = 1
then L2{j}
else null
)
),
Custom2 = List.RemoveNulls ( List.Transform(Subsets2, each let l=List.RemoveNulls(_) in List.Sum(l))),
ID = List.Repeat({Type[Customer ID]{0}}, List.Count(Custom2)),
Table = Table.FromColumns ( {ID, Custom1, Custom2}, {"Customer ID", "Unique ID Combinations", "SUM Value"}),
#"Changed Type" = Table.TransformColumnTypes(Table,{{"Unique ID Combinations", Int64.Type}, {"SUM Value", type number}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Unique ID Combinations", Order.Ascending}})
in
#"Sorted Rows"
2nd requirement
I referenced the table above in the Source step
let
Source = Table,
Type = Table.TransformColumnTypes(Source,{{"Unique ID Combinations", type text}}),
RemovedSum = Table.RemoveColumns(Type,{"SUM Value"}),
InsertedDOCID = Table.AddColumn(RemovedSum, "DOC ID", each Text.ToList([Unique ID Combinations])),
ExpandedDOCID = Table.ExpandListColumn(InsertedDOCID, "DOC ID")
in
ExpandedDOCID