Forum Discussion
Combine all possible values from a single column- Power query
- 4 years ago
Anonymous Using my code I referenced previously, I turned it into a function fn_Subsets that transforms a list into a list of subsets (a list of lists).
(L as list) as list => let 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 ) ), RemoveNulls = List.Transform(Subsets, each List.RemoveNulls(_)) in RemoveNullsWe can apply this function in a Group By set to each set of companies associated with each ID.
Here's a complete sample query (including the function definition) you can paste into the Advanced Editor of a new blank query.
let /*Define a list function. This is usually done in a separate query.*/ fn_Subsets = (L as list) as list => let 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 ) ), RemoveNulls = List.Transform(Subsets, each List.RemoveNulls(_)) in RemoveNulls, /*Define sample dataset. Replace with your own data.*/ Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Rcy7DcAgFEPRXVxTBPIvSQjkMwJ6+68RIyG5uJJP41oRPRwi8zDXebAgnmwVE9vEi+1ibleDXJr7d+C+2Sg+bBJfNosfW2D2Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Company = _t, Price = _t]), SampleData = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Company", type text}, {"Price", Int64.Type}}), /*Logic applying the subsets function and aggregating the results.*/ #"Grouped Rows" = Table.Group(SampleData, {"ID"}, {{"SubsetList", each fn_Subsets([Company]), type list}}), #"Expanded Count" = Table.ExpandListColumn(#"Grouped Rows", "SubsetList"), #"Added Custom" = Table.AddColumn(#"Expanded Count", "Company", each Text.Combine([SubsetList], ","), type text), #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Company] <> "")), #"Expanded SubsetList" = Table.ExpandListColumn(#"Filtered Rows", "SubsetList"), #"Merged Queries" = Table.NestedJoin(#"Expanded SubsetList", {"ID", "SubsetList"}, SampleData, {"ID", "Company"}, "Expanded SubsetList", JoinKind.LeftOuter), #"Expanded Expanded SubsetList" = Table.ExpandTableColumn(#"Merged Queries", "Expanded SubsetList", {"Price"}, {"Price"}), #"Aggregate Rows" = Table.Group(#"Expanded Expanded SubsetList", {"ID", "Company"}, {{"Sum_Price", each List.Sum([Price]), type nullable number}, {"No of Supplier", each Table.RowCount(_), Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Aggregate Rows",{{"ID", Order.Ascending}, {"No of Supplier", Order.Ascending}, {"Company", Order.Ascending}}) in #"Sorted Rows"
Hi Anonymous ,
Please see code below (highlighted code show is the code to get the combination):
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjRU0lFyBGJjY6VYHSjfCYiNjBB8ZyA2gcobwdSbIPhg9UjyIPWGQP2xAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Company = _t, Price = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Company", type text}, {"Price", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Count", each Table.SelectRows(#"Changed Type", (x)=>x[Company]=[Company])[ID]),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Count", each Text.Combine(List.Transform(_, Text.From), ";"), type text})
in
#"Extracted Values"
Regards
KT
Thank you for your comment. But I would like to know how many possible combination and its sum of price for 1to 7 suppliers within the same ID
e.g
- AlexisOlson4 years agoSuper User
You might be interested in my comment here. It has all the basic math needed.
Note that there are 2^7 = 128 combinations for 7 companies. The size can get out of hand really quickly if you try to precompute all possibilities. I'm not sure what your ultimate goal is, but it might be better not to try to precompute everything but rather calculate combinations with DAX measures.
- AlexisOlson4 years agoSuper User
Anonymous Using my code I referenced previously, I turned it into a function fn_Subsets that transforms a list into a list of subsets (a list of lists).
(L as list) as list => let 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 ) ), RemoveNulls = List.Transform(Subsets, each List.RemoveNulls(_)) in RemoveNullsWe can apply this function in a Group By set to each set of companies associated with each ID.
Here's a complete sample query (including the function definition) you can paste into the Advanced Editor of a new blank query.
let /*Define a list function. This is usually done in a separate query.*/ fn_Subsets = (L as list) as list => let 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 ) ), RemoveNulls = List.Transform(Subsets, each List.RemoveNulls(_)) in RemoveNulls, /*Define sample dataset. Replace with your own data.*/ Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Rcy7DcAgFEPRXVxTBPIvSQjkMwJ6+68RIyG5uJJP41oRPRwi8zDXebAgnmwVE9vEi+1ibleDXJr7d+C+2Sg+bBJfNosfW2D2Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Company = _t, Price = _t]), SampleData = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Company", type text}, {"Price", Int64.Type}}), /*Logic applying the subsets function and aggregating the results.*/ #"Grouped Rows" = Table.Group(SampleData, {"ID"}, {{"SubsetList", each fn_Subsets([Company]), type list}}), #"Expanded Count" = Table.ExpandListColumn(#"Grouped Rows", "SubsetList"), #"Added Custom" = Table.AddColumn(#"Expanded Count", "Company", each Text.Combine([SubsetList], ","), type text), #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Company] <> "")), #"Expanded SubsetList" = Table.ExpandListColumn(#"Filtered Rows", "SubsetList"), #"Merged Queries" = Table.NestedJoin(#"Expanded SubsetList", {"ID", "SubsetList"}, SampleData, {"ID", "Company"}, "Expanded SubsetList", JoinKind.LeftOuter), #"Expanded Expanded SubsetList" = Table.ExpandTableColumn(#"Merged Queries", "Expanded SubsetList", {"Price"}, {"Price"}), #"Aggregate Rows" = Table.Group(#"Expanded Expanded SubsetList", {"ID", "Company"}, {{"Sum_Price", each List.Sum([Price]), type nullable number}, {"No of Supplier", each Table.RowCount(_), Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Aggregate Rows",{{"ID", Order.Ascending}, {"No of Supplier", Order.Ascending}, {"Company", Order.Ascending}}) in #"Sorted Rows"- Anonymous4 years agoNot applicable
Raw Data
Result Example
How to get the 7! (factorial) number of all possible combination on company col for the unique ID?
I would like to have 1 to 7 of suppliers and see the all the possible combinations.
- KT_Bsmart2gethe4 years agoImpactful Individual
Hi Anonymous ,
I'll have a think through about what can be done. It is easy to find out how many combinations but to list them out in a way like A; A B; A C ....... It is quite a challenge, but I will have a go.
Regards
KT