Pick 20% random rows from list with criteria

pasjauo

New Member
Joined
May 1, 2017
Messages
49
Hey guys and girls

We are doing some quality control at work and are working on randomizing our selection of case-files the fastest way.

Right now we have a export from our system that shows the digital case file number and the owner of the case. We want to check, lets say, 20% of each persons cases for errors etc.

The excel file can look something like this but with 1000-1500 cases in every period (with more columns but these are not important):


CASE FILE NUMBEROWNER
19-2000Peter
19-2012Andrea
19-1451Peter
19-1234Mitch
19-1513Bob
19-7464Mitch
19-2362Bob
19-3561Bob
19-1515Andrea
19-9285Peter

I would like to randomly check for instance 20% of Peters cases and for that I need the Case file number. I need to check every persons cases, so it is not just Peter's

Is there any way to make this selection fast and painless? I don't mind spending a little time on a solution as long as it will work in the future (we want to do this every month). The most important thing is that my co-workers without any excelknowledge can work in the file.

So far I have played around with the RAND() but there is some degree of work every time we need to do this. A simple copy/paste a new period into a work sheet is what i'm expecting of time to be spent every period.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Here is a way using Power Query.

Book2
ABCDE
1CASE FILE NUMBEROWNEROWNERCASE FILE NUMBER
219-2000PeterPeter19-2290
319-2012AndreaPeter19-2841
419-1451PeterPeter19-5430
519-1234MitchPeter19-7862
619-1513BobAndrea19-5893
719-7464MitchAndrea19-7919
819-2362BobAndrea19-1145
919-3561BobAndrea19-1515
1019-1515AndreaAndrea19-1790
1119-9285PeterMitch19-1171
1219-6343MitchMitch19-9839
1319-6907AndreaMitch19-7926
1419-9883AndreaMitch19-1086
1519-7807BobMitch19-7684
1619-9512PeterBob19-9524
1719-5893AndreaBob19-2458
1819-2061PeterBob19-5461
1919-8448AndreaBob19-2362
2019-7633AndreaBob19-9339
2119-7684MitchBob19-6786
2219-3090Mitch
2319-6745Peter
2419-9524Bob
2519-2680Andrea
2619-1145Peter
2719-1171Mitch
2819-9872Bob
2919-9981Bob
3019-1086Mitch
3119-1498Bob
3219-2290Peter
3319-3809Andrea
3419-4622Mitch
3519-7862Peter
3619-3001Bob
3719-2841Peter
3819-1552Bob
3919-3534Bob
4019-2629Peter
4119-7156Bob
4219-3632Peter
4319-7541Peter
4419-9181Bob
4519-2272Andrea
4619-4395Mitch
4719-8849Bob
4819-6587Mitch
4919-5669Bob
5019-9482Peter
5119-6889Andrea
5219-1790Andrea
5319-8913Peter
5419-3145Mitch
5519-9339Bob
5619-7344Bob
5719-5378Peter
5819-9839Mitch
5919-5258Mitch
6019-3884Andrea
6119-3941Peter
6219-4995Mitch
6319-2458Bob
6419-7919Andrea
6519-8122Bob
6619-9410Bob
6719-6661Andrea
6819-1056Bob
6919-4723Mitch
7019-7007Peter
7119-3162Mitch
7219-5314Andrea
7319-9680Andrea
7419-5439Bob
7519-2118Andrea
7619-7279Bob
7719-9603Bob
7819-7268Mitch
7919-5182Mitch
8019-6273Andrea
8119-5531Andrea
8219-3430Andrea
8319-6747Peter
8419-1145Andrea
8519-4139Andrea
8619-7830Bob
8719-6786Bob
8819-2717Andrea
8919-7270Bob
9019-6400Bob
9119-9864Bob
9219-5461Bob
9319-2382Mitch
9419-6275Mitch
9519-4723Mitch
9619-1705Mitch
9719-1256Peter
9819-7926Mitch
9919-2021Bob
10019-5430Peter
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"OWNER"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"Data", (tbl)=> Table.AddColumn(tbl,"R",each Number.RandomBetween(1,Table.RowCount(tbl))), type table [CASE FILE NUMBER=text, OWNER=text]}}),
    Twenty = Table.TransformColumns(Group,{{"Data", (tbl)=> Table.FirstN(Table.Sort(Table.Buffer(tbl),{"R"}),Number.Round(Table.RowCount(tbl)*.2,0))}}),
    Expand = Table.ExpandTableColumn(Twenty, "Data", {"CASE FILE NUMBER"}, {"CASE FILE NUMBER"}),
    RC = Table.RemoveColumns(Expand,{"Count"})
in
    RC
 
Upvote 0
Office 365?
MrExcelPlayground2.xlsx
ABCDE
2CASE FILE NUMBEROWNER20%
319-2000PeterFALSEPeter 
419-2012AndreaTRUEAndrea19-2012
519-1451PeterFALSEMitch19-7464
619-1234MitchFALSEBob19-1513, 19-2362
719-1513BobTRUE
819-7464MitchTRUE
919-2362BobTRUE
1019-3561BobFALSE
1119-1515AndreaFALSE
1219-9285PeterFALSE
Sheet1
Cell Formulas
RangeFormula
D3:D6D3=UNIQUE(B3:B12)
E3:E6E3=TEXTJOIN(", ",TRUE,IF(($B$3:$B$12=D3)*($C$3:$C$12),$A$3:$A$12,""))
C3:C12C3=RAND()<$D$2
Dynamic array formulas.
 
Upvote 0
Here is a way using Power Query.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"OWNER"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"Data", (tbl)=> Table.AddColumn(tbl,"R",each Number.RandomBetween(1,Table.RowCount(tbl))), type table [CASE FILE NUMBER=text, OWNER=text]}}),
    Twenty = Table.TransformColumns(Group,{{"Data", (tbl)=> Table.FirstN(Table.Sort(Table.Buffer(tbl),{"R"}),Number.Round(Table.RowCount(tbl)*.2,0))}}),
    Expand = Table.ExpandTableColumn(Twenty, "Data", {"CASE FILE NUMBER"}, {"CASE FILE NUMBER"}),
    RC = Table.RemoveColumns(Expand,{"Count"})
in
    RC

Thanks!

I get a Token EOF error when running this. I'm not that strong in M, so I can't really see whats wrong with the code.
 
Upvote 0
Office 365?
MrExcelPlayground2.xlsx
ABCDE
2CASE FILE NUMBEROWNER20%
319-2000PeterFALSEPeter 
419-2012AndreaTRUEAndrea19-2012
519-1451PeterFALSEMitch19-7464
619-1234MitchFALSEBob19-1513, 19-2362
719-1513BobTRUE
819-7464MitchTRUE
919-2362BobTRUE
1019-3561BobFALSE
1119-1515AndreaFALSE
1219-9285PeterFALSE
Sheet1
Cell Formulas
RangeFormula
D3:D6D3=UNIQUE(B3:B12)
E3:E6E3=TEXTJOIN(", ",TRUE,IF(($B$3:$B$12=D3)*($C$3:$C$12),$A$3:$A$12,""))
C3:C12C3=RAND()<$D$2
Dynamic array formulas.

When doing the first C3 formula, it says TRUE in the whole column - and that makes the other ones right?

EDIT: Well, i'm stupid. I made a typing mistake, and now it works flawlessly! :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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