Randomize PQ Results Based on Certain Criteria

seaottr

Board Regular
Joined
Feb 10, 2010
Messages
60
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Power Query Gurus out there!

I don't have extensive knowledge of Power Query, but use it for simpler tasks. I'm currently using it to query data from a SharePoint list (not relevant to my question), and I'm looking to have PQ look at the data (sample data below), and randomly select 3 IDs for each Employee/Manager.

Based on the data set below, that should produce 12 random IDs for "John Smith" (specifically 3 for each of John's employees), 12 random IDs for "Mary Brown" (specifically 3 for each of Mary's employees), and 12 random IDs for "Lisa Simpson" (specifically 3 for each of Lisa's employees).

I know I could also do this with VBA, but I'm looking for the PQ query to do everything all with one refresh.

Any help would be greatly appreciated for this Power Query idiot :)

ManagerEmployeeID
John SmithEmployee 1
2429928​
John SmithEmployee 2
337073​
John SmithEmployee 4
33930317​
John SmithEmployee 4
25263840​
John SmithEmployee 1
701579​
John SmithEmployee 3
967994​
John SmithEmployee 2
198319429​
John SmithEmployee 3
5197473​
John SmithEmployee 4
539053​
John SmithEmployee 3
611529​
John SmithEmployee 4
3091981​
John SmithEmployee 1
99859​
John SmithEmployee 1
21671976​
John SmithEmployee 2
150127189​
John SmithEmployee 4
613581​
John SmithEmployee 2
25306513​
John SmithEmployee 1
15565530​
John SmithEmployee 2
649545​
John SmithEmployee 3
426138​
John SmithEmployee 3
183305801​
Mary BrownEmployee 4
1831019​
Mary BrownEmployee 2
484174​
Mary BrownEmployee 3
442733​
Mary BrownEmployee 4
2595118​
Mary BrownEmployee 4
396377​
Mary BrownEmployee 4
53598054​
Mary BrownEmployee 1
154059591​
Mary BrownEmployee 1
133444​
Mary BrownEmployee 4
28395377​
Mary BrownEmployee 3
22363651​
Mary BrownEmployee 2
661402​
Mary BrownEmployee 1
870077​
Mary BrownEmployee 3
117759350​
Mary BrownEmployee 2
1327183​
Mary BrownEmployee 3
888435​
Mary BrownEmployee 1
903​
Mary BrownEmployee 3
1899601​
Mary BrownEmployee 2
437726​
Mary BrownEmployee 2
19303604​
Mary BrownEmployee 1
12196487​
Lisa SimpsonEmployee 2
707090​
Lisa SimpsonEmployee 1
15316272​
Lisa SimpsonEmployee 2
25814310​
Lisa SimpsonEmployee 3
470464​
Lisa SimpsonEmployee 1
515186​
Lisa SimpsonEmployee 3
35011322​
Lisa SimpsonEmployee 4
1920190​
Lisa SimpsonEmployee 2
195898​
Lisa SimpsonEmployee 3
217373​
Lisa SimpsonEmployee 1
1362011​
Lisa SimpsonEmployee 4
115747​
Lisa SimpsonEmployee 2
7000976​
Lisa SimpsonEmployee 3
143910400​
Lisa SimpsonEmployee 1
762190​
Lisa SimpsonEmployee 4
15538251​
Lisa SimpsonEmployee 2
26335404​
Lisa SimpsonEmployee 1
729790​
Lisa SimpsonEmployee 3
346976​
Lisa SimpsonEmployee 4
62082228​
Lisa SimpsonEmployee 4
5923133​
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.Group(Source, {"Manager", "Employee"}, {{"All", each Table.Buffer(Table.AddColumn(Table.SelectColumns(_, {"ID"}), "Random", (x)=> Number.Random() ))}}),
    tbl2 = Table.TransformColumns(tbl, {"All", each Table.FirstN(Table.Sort(_, {"Random"}),3)}),
    Result = Table.ExpandTableColumn(tbl2, "All", {"ID"})
in
    Result
 
Upvote 1
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.Group(Source, {"Manager", "Employee"}, {{"All", each Table.Buffer(Table.AddColumn(Table.SelectColumns(_, {"ID"}), "Random", (x)=> Number.Random() ))}}),
    tbl2 = Table.TransformColumns(tbl, {"All", each Table.FirstN(Table.Sort(_, {"Random"}),3)}),
    Result = Table.ExpandTableColumn(tbl2, "All", {"ID"})
in
    Result
Thank you so much for the reply JGordon! I haven't yet had a chance to try this out as I was done work for the weekend when you posted, and it's shaping up to be a crazy week!

I'll definitely try this out though and let you know how it goes!

Thanks again!
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.Group(Source, {"Manager", "Employee"}, {{"All", each Table.Buffer(Table.AddColumn(Table.SelectColumns(_, {"ID"}), "Random", (x)=> Number.Random() ))}}),
    tbl2 = Table.TransformColumns(tbl, {"All", each Table.FirstN(Table.Sort(_, {"Random"}),3)}),
    Result = Table.ExpandTableColumn(tbl2, "All", {"ID"})
in
    Result
This works PERFECTLY JGordon! Thank you again for your help! Quick question, how would I modify this to still include other columns in the original data source?
 
Upvote 0
This works PERFECTLY JGordon! Thank you again for your help! Quick question, how would I modify this to still include other columns in the original data source?
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tcn = List.RemoveItems(Table.ColumnNames(Source), {"Manager", "Employee"}),
    tbl = Table.Group(Source, {"Manager", "Employee"}, {{"All", each Table.Buffer(Table.AddColumn(Table.SelectColumns(_, tcn), "Random", (x)=> Number.Random() ))}}),
    tbl2 = Table.TransformColumns(tbl, {"All", each Table.FirstN(Table.Sort(_, {"Random"}),3)}),
    Result = Table.ExpandTableColumn(tbl2, "All", tcn)
in
    Result
 
Upvote 1
Solution

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top