Forum Discussion

osama_ayoub's avatar
osama_ayoub
Helper III
3 years ago
Solved

customize column to choose from duplicated rows in power Query

Hi, I have a table like below column A [Planning Code] have some of family codes and this family codes are sometimes shared between different codes in column[Material Code] now I want to package onl...
  • jbwtp's avatar
    jbwtp
    3 years ago

    Hi osama_ayoub,

     

    As you merged the tables PBI allocated a set of records for each matching line in the source table (this is why it is important/more convenient to merge Requests with Avail rather then vise versa):

    if you click on the cell (not on the Table itself) in the Available column you will see how the available quantities are assigned to the requested ones.

     

    Then we add a column that is actually a set of the available quantities with how many of them we can allocate to fullfill the request. The majic happens in this fuction:

     f = (t as table, req as number)=> 
            let 
                avail = Number.From(t{0}[Available in Material Code]),
                res = if avail > req or Table.RowCount(t) = 1 then {Record.AddField(t{0}, "take", List.Min({req, avail}))} else {Record.AddField(t{0}, "take", avail)} & @f(Table.Skip(t), req - avail)
            in res

    It gets two parameters:

    • t - a table of quanties available for this product,
    • req - required quantity

    In the funciton:

    1. avail takes the first row of the table of available materials passed to the funciton
    2. then we check if the available quantity on this row is sufficient to fulfill the required quantity passed to the function:
      1. If this is sufficient, it adds a field (which later converted to ta column in the output table) and sets its value = req
      2. If there is only one row left in the column, there is no point going any further and we just allocate the lesser of req and avail , i.e. trying to fullfill the request as much as possible
      3. Otherwise, we take as much as we can on this "supply" row and call the function again, this time passing all but the first line in the "supply" table (cause this is already been "taken") and the portion of "required" amount (reduced by the volumes fullfuilled by this "supply" row.

     

    This may be a bit unclear, but it hopefully it can make sense if you "walk the steps" in the function, bearing in mind that it recursively calls itself with demonishing number of lines and required quantities until either "supply" rows will be depleted or the entire required amount supplied. 

     

    Kind regards,

    John