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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
insightmwarrick
New Member

Select random record from one table to populate another table

I have a table of Sales Reps and a table of Customers.  The customers table has one rep per customer (currently blank).  I want to select a sales rep at random from the Sales Reps table to populate the Customer Table Sales Rep field.  There are thousands more customer than there are reps, so a single rep can be assigned to more than one company.  It would be ideal to evenly distribute the reps among the companies if possible, but that's not entirely necessary.

 

Help!

 

For example:

 

Sales Reps Table

Sales Rep
Bob Carlson
Carl Drake
David Rogan

 

Customers Table

Customer NameSales Rep
ACMECarl Drake
Dunder MifflinDavid Rogan
Stark IndustriesBob Carlson
WonkaDavid Rogan
DuffCarl Drake
Sterlin CooperDavid Rogan
Weyland CorpBob Carlson
InitechCarl Drake
1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

If you want a nice even distribution, then randomly sort your Customer table, add an index mod N, and merge with the Sales Reps table (which needs an index too).

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcuxCoAgFEDRXxHnfiKswcGpoUEcJJ8kylOeOvT3ibUe7tWar0Lt3Cyabx0dEFPB+xRw0tEsRSbR9dooQJ14Zoz2P7z/O6DxMJFzAfoqeJJFN4jKBImhwXVzY14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Name" = _t]),
    #"Added Custom" = Table.Buffer(Table.AddColumn(Source, "Random", each Number.Random(), type number)),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Random", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, Table.RowCount(SalesReps)), Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Integer-Divided Column", {"Index"}, SalesReps, {"Index"}, "SalesReps", JoinKind.LeftOuter),
    #"Expanded SalesReps" = Table.ExpandTableColumn(#"Merged Queries", "SalesReps", {"Sales Rep"}, {"Sales Rep"})
in
    #"Expanded SalesReps"

 

If you just want random and care less about an even distribution, then you can generate an index for each row separately without any sorting going on.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcuxCoAgFEDRXxHnfiKswcGpoUEcJJ8kylOeOvT3ibUe7tWar0Lt3Cyabx0dEFPB+xRw0tEsRSbR9dooQJ14Zoz2P7z/O6DxMJFzAfoqeJJFN4jKBImhwXVzY14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Name" = _t]),
    #"Added Custom" = Table.Buffer(Table.AddColumn(Source, "Index", each Number.RoundDown(Number.RandomBetween(0, Table.RowCount(SalesReps))), Int64.Type)),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Index"}, SalesReps, {"Index"}, "SalesReps", JoinKind.LeftOuter),
    #"Expanded SalesReps" = Table.ExpandTableColumn(#"Merged Queries", "SalesReps", {"Sales Rep"}, {"Sales Rep"})
in
    #"Expanded SalesReps"

 

View solution in original post

3 REPLIES 3
AlexisOlson
Super User
Super User

If you want a nice even distribution, then randomly sort your Customer table, add an index mod N, and merge with the Sales Reps table (which needs an index too).

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcuxCoAgFEDRXxHnfiKswcGpoUEcJJ8kylOeOvT3ibUe7tWar0Lt3Cyabx0dEFPB+xRw0tEsRSbR9dooQJ14Zoz2P7z/O6DxMJFzAfoqeJJFN4jKBImhwXVzY14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Name" = _t]),
    #"Added Custom" = Table.Buffer(Table.AddColumn(Source, "Random", each Number.Random(), type number)),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Random", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, Table.RowCount(SalesReps)), Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Integer-Divided Column", {"Index"}, SalesReps, {"Index"}, "SalesReps", JoinKind.LeftOuter),
    #"Expanded SalesReps" = Table.ExpandTableColumn(#"Merged Queries", "SalesReps", {"Sales Rep"}, {"Sales Rep"})
in
    #"Expanded SalesReps"

 

If you just want random and care less about an even distribution, then you can generate an index for each row separately without any sorting going on.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcuxCoAgFEDRXxHnfiKswcGpoUEcJJ8kylOeOvT3ibUe7tWar0Lt3Cyabx0dEFPB+xRw0tEsRSbR9dooQJ14Zoz2P7z/O6DxMJFzAfoqeJJFN4jKBImhwXVzY14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Name" = _t]),
    #"Added Custom" = Table.Buffer(Table.AddColumn(Source, "Index", each Number.RoundDown(Number.RandomBetween(0, Table.RowCount(SalesReps))), Int64.Type)),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Index"}, SalesReps, {"Index"}, "SalesReps", JoinKind.LeftOuter),
    #"Expanded SalesReps" = Table.ExpandTableColumn(#"Merged Queries", "SalesReps", {"Sales Rep"}, {"Sales Rep"})
in
    #"Expanded SalesReps"

 

wdx223_Daniel
Super User
Super User

you can sort CustomerTable by a random base, then split the table to the rows number of Reps.

That may be a solution for half the problem.  But how do I select reps at random to fill the customers table?  Example?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors