Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Pull X Number of Records Based on Total Volume

Let me try this again. I posted this once but it was deleted as spam.

 

Hello!

 

I have been racking my brain over this and have not been able to figure it out. I may be overthinking it. 

As an example of what I am looking for, I have a table of data that includes a list of orders and includes order number, vendor, and color (either Blue or Red). I would like to pull a random sampling of the data in a certain way. If the vendor's volume is between 1 - 20, I would like to pull 2 records. If the volume is 20 - 40, I would like to pull 4 records. If the volume is over 40, I would like to pull 6 records. 

 

Going further, if the number pulled is 2, I would like them to be 1 Blue record and 1 Red record. If the number pulled is 4, I would like them to be 1 Blue record and 3 Red records, If the number pulled is 6, I would like them to be 3 Blue records and 4 Red records. 

 

I am hoping to get them all in a table or two. So if the volumes are as follows:

 

VendorTotal VolumeRandom Pull TotalRandom Blue AmountRandom Red Amount
Bob36413
Jane50624
Joe28413
Sam9211

 

And I select Joe, I will get a random 4 orders, with 1 being a random blue and 3 being random reds:

 

VendorOrder #Color
Joe84Blue
Joe44Red
Joe15Red
Joe114Red

 

If I need to have two tables, one that pulls the random blues and one that pulls the random reds, this is fine. But I just cannot figure it out!

 

I tried to provide a OneDrive link to some sample data but my post was marked as spam and deleted, so to be safe, I will not post any data. Sorry.

 

I appreciate any help!


Thank you!

1 ACCEPTED SOLUTION
AlB
Community Champion
Community Champion

@Anonymous 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZWxUgMxDET/5eoUli3bUktJCeVNijCkA1Lx/5Cb4Uiyr7liT5K13pW1rosth+X5cv79Pn18n5fjYV3qFTp9XbGX8/sGtev/y9sN4juy53UtNf5L7dgELPbUv/opJ1qBRDMCqyY3inPt2Lq0YkPLTaVvSsJSr7ISjUoyKItKLKrDIZ0ChzZdJyQrkZqa2ohIAyKNDKWOamCppmq0AdXIUy2gFbWVEw03PcSr9ucN4lx6dhgNBzV8anvAwsFWnWh0UKOrrTqw6E71yFYdiHSwVVdbdbDVICIDiIxKgeqr4QrRUzUAUz1GKLOR0MksGjhN+U6YjtnkqiZMx+yAwXhMGo8JRCboEaRHmDQY6qug5yrg0Q3yVeizG2CrCMolRbIou6T9kVXYpfoqQZEkIklbMEmTDD0FZt1K0Tu0YpJsBcxlBXdhoWVYwGFWwGJWQBorQemkjRlRMrWZ0XK3BueYvsO2bffX0+cdNnbsJnkSGASmltx2/CNmkLzt+MfAdo8dfwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order #" = _t, Vendor = _t, Color = _t]),

    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "RandNum_", each Number.Random(), type number),
    #"Sorted Rows" = Table.Sort(Table.Buffer(#"Added Custom"),{{"Vendor", Order.Ascending}, {"Color", Order.Ascending}, {"RandNum_", Order.Ascending}}),
    #"Grouped Rows0" = Table.Group(#"Sorted Rows", {"Vendor"}, {{"Tot volume", each List.Count([#"Order #"])}}),
    #"Grouped Rows1" = Table.Group(Table.Buffer(#"Sorted Rows"), {"Vendor", "Color"}, {{"Temp", each _}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows1", {"Vendor"}, #"Grouped Rows0", {"Vendor"}, "Tot Volume", JoinKind.LeftOuter),
    #"Expanded Tot Volume" = Table.ExpandTableColumn(#"Merged Queries", "Tot Volume", {"Tot volume"}, {"Tot volume"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Tot Volume", "Sample", each if [Tot volume] <= 20 then Table.FirstN([Temp],1) else  
                           if [Tot volume] <= 40 then (if [Color] = "Blue" then Table.FirstN([Temp],1) else Table.FirstN([Temp],3)) else
                           Table.FirstN([Temp],3)),
    #"Expanded Sample" = Table.ExpandTableColumn(#"Added Custom1", "Sample", {"Order #"}, {"Order #"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Sample",{"Temp", "Tot volume"}),
    #"Sorted Rows1" = Table.Sort(#"Removed Columns",{{"Vendor", Order.Ascending}, {"Color", Order.Ascending}})
in
    #"Sorted Rows1"

View solution in original post

10 REPLIES 10
AlB
Community Champion
Community Champion

@Anonymous 

How many rows are we talking about?

Perhaps something like Python would be more suitable for this

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Anonymous
Not applicable

@AlB ,The table with the vendors, Order Numbers, and "Colors", etc. is, currently, at 1,979,640 rows and counting. 😕 There is also a date column though, and I really only need the orders from *this year*, which would reduce the rows currently to 349,483 if we can add some sort of date filter to that M code?

AlB
Community Champion
Community Champion

@Anonymous 

Can you share the actual data? Send it over private message if necessary. 

You have to share the URL to the file hosted elsewhere: Dropbox, Onedrive... or just upload the file to a site like tinyupload.com (no sign-up required).

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Anonymous
Not applicable

I am not able to upload or share any actual data, it is very proprietary and the amount of data makes it too hard to redact/spoof the proprietary information. Your solution did not work for me, I am sorry. There is too much data for this to work. Thank you for your help though

AlB
Community Champion
Community Champion

@Anonymous 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZWxUgMxDET/5eoUli3bUktJCeVNijCkA1Lx/5Cb4Uiyr7liT5K13pW1rosth+X5cv79Pn18n5fjYV3qFTp9XbGX8/sGtev/y9sN4juy53UtNf5L7dgELPbUv/opJ1qBRDMCqyY3inPt2Lq0YkPLTaVvSsJSr7ISjUoyKItKLKrDIZ0ChzZdJyQrkZqa2ohIAyKNDKWOamCppmq0AdXIUy2gFbWVEw03PcSr9ucN4lx6dhgNBzV8anvAwsFWnWh0UKOrrTqw6E71yFYdiHSwVVdbdbDVICIDiIxKgeqr4QrRUzUAUz1GKLOR0MksGjhN+U6YjtnkqiZMx+yAwXhMGo8JRCboEaRHmDQY6qug5yrg0Q3yVeizG2CrCMolRbIou6T9kVXYpfoqQZEkIklbMEmTDD0FZt1K0Tu0YpJsBcxlBXdhoWVYwGFWwGJWQBorQemkjRlRMrWZ0XK3BueYvsO2bffX0+cdNnbsJnkSGASmltx2/CNmkLzt+MfAdo8dfwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order #" = _t, Vendor = _t, Color = _t]),

    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "RandNum_", each Number.Random(), type number),
    #"Sorted Rows" = Table.Sort(Table.Buffer(#"Added Custom"),{{"Vendor", Order.Ascending}, {"Color", Order.Ascending}, {"RandNum_", Order.Ascending}}),
    #"Grouped Rows0" = Table.Group(#"Sorted Rows", {"Vendor"}, {{"Tot volume", each List.Count([#"Order #"])}}),
    #"Grouped Rows1" = Table.Group(Table.Buffer(#"Sorted Rows"), {"Vendor", "Color"}, {{"Temp", each _}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows1", {"Vendor"}, #"Grouped Rows0", {"Vendor"}, "Tot Volume", JoinKind.LeftOuter),
    #"Expanded Tot Volume" = Table.ExpandTableColumn(#"Merged Queries", "Tot Volume", {"Tot volume"}, {"Tot volume"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Tot Volume", "Sample", each if [Tot volume] <= 20 then Table.FirstN([Temp],1) else  
                           if [Tot volume] <= 40 then (if [Color] = "Blue" then Table.FirstN([Temp],1) else Table.FirstN([Temp],3)) else
                           Table.FirstN([Temp],3)),
    #"Expanded Sample" = Table.ExpandTableColumn(#"Added Custom1", "Sample", {"Order #"}, {"Order #"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Sample",{"Temp", "Tot volume"}),
    #"Sorted Rows1" = Table.Sort(#"Removed Columns",{{"Vendor", Order.Ascending}, {"Color", Order.Ascending}})
in
    #"Sorted Rows1"
Anonymous
Not applicable

Thanks @AlB , I have given this a try; however, it has been evaluating all day. I am actually working with a huge amount of data (the sample I gave was an example), so if it ever loads I will be able to see if it works! Thanks!

AlB
Community Champion
Community Champion

@Anonymous 

So how do we translate this: If the vendor's volume is between 1 - 20, I would like to pull 2 records. If the volume is 20 - 40, I would like to pull 4 records. If the volume is over 40, I would like to pull 6 records.

to the data you have now??  What is the volume then, the number of rows??

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Anonymous
Not applicable

Yes @AlB the number of rows where they are listed as the vendor. I generally do a distinctcount of the order number and then a visual filter the vendor though could write a measure for this with a filter as well. Thank you!

Anonymous
Not applicable

Hello @AlB !

 

I realize now that since I had to remove my sample data some may not have made sense, and I am not sure your suggestion worked with the type of data I have. The table that includes my data is a CSV like this (sorry it is a little long but I cannot attach or link to it):

 

Order #VendorColor
1JoeBlue
2JaneRed
3BobRed
4BobBlue
5JoeBlue
6JaneBlue
7JaneBlue
8JoeRed
9BobRed
10JaneBlue
11JaneBlue
12BobRed
13JaneBlue
14JoeBlue
15JoeRed
16BobRed
17BobBlue
18JoeRed
19JaneRed
20JaneBlue
21JoeBlue
22BobRed
23JaneBlue
24JaneRed
25JaneBlue
26BobBlue
27JaneRed
28JoeRed
29BobBlue
30JaneBlue
31JoeBlue
32JaneRed
33BobRed
34BobBlue
35JoeRed
36JaneRed
37JaneBlue
38JoeBlue
39BobRed
40JaneBlue
41JaneRed
42BobBlue
43JaneRed
44JoeRed
45JoeBlue
46BobBlue
47BobRed
48JoeBlue
49JaneRed
50JaneBlue
51JoeBlue
52BobRed
53JaneRed
54JaneBlue
55JaneBlue
56BobBlue
57JaneRed
58JoeRed
59BobBlue
60JaneBlue
61JoeBlue
62JaneBlue
63BobRed
64BobRed
65JoeBlue
66JoeBlue
67BobRed
68JaneRed
69JaneBlue
70JaneRed
71BobBlue
72JaneRed
73JoeRed
74BobBlue
75BobBlue
76JaneRed
77JaneBlue
78JaneRed
79BobBlue
80JaneBlue
81JoeRed
82BobRed
83JaneBlue
84JoeBlue
85JaneBlue
86BobRed
87JaneRed
88JaneBlue
89JaneBlue
90BobBlue
91JaneBlue
92JoeRed
93BobRed
94BobBlue
95JaneBlue
96JaneBlue
97JaneBlue
98BobRed
99JaneRed
100JoeBlue
101BobRed
102JaneRed
103JaneBlue
104JoeBlue
105BobBlue
106JaneRed
107JaneRed
108BobBlue
109JaneBlue
110JoeBlue
111JoeRed
112BobRed
113BobBlue
114JoeRed
115SamRed
116SamBlue
117SamBlue
118SamBlue
119SamRed
120SamRed
121SamBlue
122SamRed
123SamRed


There is no volume column. I tried to make what you sent work but it does not look like I am able to. Any suggestions now that I have copied and pasted some sample data?

 

Thank you so much!

AlB
Community Champion
Community Champion

Hi @Anonymous 

Place the following M code in a blank query to see the steps of a possible solution. Every time you refresh, the random sample will be updated

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddJNC4MwDAbg/9Kzh6W1rV497jLYjiLDYW/7AGH/f03JnOmSQ6H0IXljcBzN8LqZxpzWJa3XQ75BPsP9nczUcESw+ZzT8mf47hTD93bf9Dg/06YtRX4rGfoqkmGgTLlvpFSxtMs3r2Gfb0HtC7ijqNUCfkq3L77Mj5/Wa2LoqokZtjSx3NfXIzMNNLGskSYWYxF6DRHKOiS15V8CLdailH3IjAJOZRTYljV9AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Vendor = _t, OrderNum = _t, Volume = _t, Color = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Vendor", type text}, {"Volume", Int64.Type}, {"Color", type text}}),

    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "RandNum_", each Number.Random(), type number),
    #"Sorted Rows" = Table.Sort(Table.Buffer(#"Added Custom"),{{"Vendor", Order.Ascending}, {"Color", Order.Ascending}, {"RandNum_", Order.Ascending}}),
    #"Grouped Rows0" = Table.Group(#"Sorted Rows", {"Vendor"}, {{"Tot volume", each List.Sum([Volume]), type nullable number}}),
    #"Grouped Rows1" = Table.Group(Table.Buffer(#"Sorted Rows"), {"Vendor", "Color"}, {{"Temp", each _}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows1", {"Vendor"}, #"Grouped Rows0", {"Vendor"}, "Tot Volume", JoinKind.LeftOuter),
    #"Expanded Tot Volume" = Table.ExpandTableColumn(#"Merged Queries", "Tot Volume", {"Tot volume"}, {"Tot volume"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Tot Volume", "Sample", each if [Tot volume] <= 20 then Table.FirstN([Temp],1) else  
                           if [Tot volume] <= 40 then (if [Color] = "Blue" then Table.FirstN([Temp],1) else Table.FirstN([Temp],3)) else
                           Table.FirstN([Temp],3)),
    #"Expanded Sample" = Table.ExpandTableColumn(#"Added Custom1", "Sample", {"OrderNum", "Volume"}, {"OrderNum", "Volume"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Sample",{"Temp", "Tot volume"})
in
    #"Removed Columns"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors