Forum Discussion

MCstudio's avatar
MCstudio
Helper I
4 years ago

Merge by condition and approximate matching in Power Query

Hi community!

I have got a question.

We have got clients, who do orders, and managers, who control orders. The final goal is to understand, which manager provide what volume of orders. But, during the time, clients' managers are changing.

We have got order table:

|Client||Order Date||Order Amount|
Cl.101/01/22120
Cl.201/01/22450
Cl.302/01/22340
Cl.410/01/22210
Cl.105/02/22119
Cl.207/02/22454
Cl.308/02/22342
Cl.411/02/22225

 

We have got managers' changing table:

|Client||Manager Change Date||Manager|
Cl.301/01/22Mng.2
Cl.408/01/22Mng.3
Cl.207/02/22Mng.2
Cl.306/02/22Mng.1
Cl.115/02/22Mng.2

 

The default manager at start is Mng.1

 

At final, we must have, in the Power Query, a table like this:

|Client||Order Date||Order Amount||Manager|
Cl.101/01/22120Mng.1
Cl.201/01/22450Mng.1
Cl.302/01/22340Mng.2
Cl.410/01/22210Mng.3
Cl.105/02/22119Mng.1
Cl.207/02/22454Mng.2
Cl.308/02/22342Mng.1
Cl.411/02/22225Mng.3

 

I have faced to solutions in the net, with Full outer merge - Conditional column - Group by - Fill down, but they aren't too good. Let's try to finde more simple and better one.

Thank you in advance!

9 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    I don't know if that's exactly what you're looking for (in the sense that being simple and "better" at the same time isn't common).
    At least the functions you mentioned and which, for some reason, did not satisfy you are not used.

     

    edit:

    This solution assumes that the Change date manager table is sorted in ascending order.
    Otherwise it can be easily adapted to the generic case.

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    let
        OrderTable = Excel.CurrentWorkbook(){[Name="OrderTable"]}[Content],
        ManagerTable = Excel.CurrentWorkbook(){[Name="ManagerTable"]}[Content],
        Custom1 = let
                     mt=Table.Group(
                                    ManagerTable,
                                    "Client",
                                    {"n",each Table.ToRows(Table.Sort(_,{"Manager Change Date",1}))}
                                   )
                  in
                     Table.AddColumn(
                                     OrderTable,
                                     "Manager",
                                     each let
                                             a=mt{[Client=[Client]]}?[n]?
                                          in
                                            if a=null then "Mng.1"
                                            else List.Skip(a,(x)=>x{1}>[Order Date]){0}?{2}? ??"Mng.1"
                                    )
    in
        Custom1
  • Anonymous's avatar
    Anonymous
    Not applicable

    Could you elaborate on the concept expressed in the following sentence?

     

    "I have faced to solutions in the net, with Full outer merge - Conditional column - Group by - Fill down, but they aren't too good. Let's try to finde more simple and better one."

     

    • MCstudio's avatar
      MCstudio
      Helper I

      I would like to find best practice to solve this problem. Marked solution seemed to me too long.

      • Anonymous's avatar
        Anonymous
        Not applicable

        "Marked solution seemed to me too long."

         

        What "marked solutions" are you referring to?

        And what is "too long"? The execution time or the proposed scripts (the number of steps required)?

         

        If you explain less sparingly what your case is (size of the tables at stake, for instance), maybe someone can help you.