Forum Discussion
Dihros
5 years agoFrequent Visitor
Equitable distribution by shorter distance - Geolocation
Appreciable all I require your support for the following, I need to assign clients to the closest manager by location, but these assignments should be distributed evenly, if the closest manager is a...
Anonymous
5 years agoNot applicable
clients
managers
distrClientsByLocation (via ListAccumulate)
let
distr=(loc) =>
let
manTabLoc=managers{[location=loc]}[tabLoc],
listClients=clients{[location=loc]}[clientsLoc][id],
recsCl=Table.ToRecords(clients{[location=loc]}[clientsLoc]),
m=Table.RowCount(manTabLoc),
r=Number.RoundUp(List.Count(listClients)/m),
exManTab=Table.AddIndexColumn(Table.FromRecords(List.Combine(List.Transform(Table.ToRecords(manTabLoc), each List.Repeat({_},r)))),"idx",0,1),
lacc= List.Accumulate(recsCl,[cl={},man={},manT=exManTab],
(s,c)=> s& [cl=s[cl]&{c}, man=s[man]&{closestManager(c,s[manT])},manT=Table.RemoveMatchingRows(s[manT],{closestManager(c,s[manT])})])
in Table.FromColumns({lacc[cl],lacc[man]})
in
distr
closestManager
let
closest=(client, managers) =>
let
distances=List.Transform(List.Zip({managers[lat],managers[long]}), each distance(client[lat],client[long],_{0},_{1}))
in managers{List.PositionOf(distances, List.Min(distances))}
in
closest
invoked function for location 1
let
Source = distrClientsByLocLA("l1"),
#"Expanded Column1" = Table.ExpandRecordColumn(Source, "Column1", {"id"}, {"id"}),
#"Expanded Column2" = Table.ExpandRecordColumn(#"Expanded Column1", "Column2", {"man", "location", "lat", "long"}, {"man", "location", "lat", "long"})
in
#"Expanded Column2"
results in:
PS
I dont't have enough time now to give some important (I think) comment.
I'll come back to it as soon as possible.