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

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

Reply
MCstudio
Helper I
Helper I

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 9
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."

 

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

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.

The marked solution (Full outer merge - Conditional column - Group by - Fill down), which I have seen here https://www.youtube.com/watch?v=y3bOyNpJuXc

Too long - yes, I mean the number of steps.
My first table have got about 8M rows, the second 81k, and the both are going to increase.

Anonymous
Not applicable

"Too long - yes, I mean the number of steps."

So your problem is not the execution time, but the code size !?
In this case, @wdx223_Daniel's solution (which could also be reduced by eliminating a redundant instruction and, if your db is sorted, eliminate two steps) seems to me to be right for you.

Dear @Anonymous ,

the @wdx223_Daniel 's solution seems to be the best, but how I answered to the @wdx223_Daniel 's message, there is a minor problem, which time to time results in an error.

Yesterday, at home I tried the code, it worked. Today at the office, it didn't. I was trying to find out, what is the reason. The only difference I have found, is date format.

Data format:

Home PC - dd.mm.yyyy

Office PC - dd/mm/yy

 

I guess, at office, this code change days' and months' places and results in an error.

 

If you, or @wdx223_Daniel help me, to correct the code, which will work with both formats, I will be grateful.

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1646705487021.png

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

MCstudio_0-1646820658146.png

An error appeared

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.

Helpful resources

Announcements
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 Kudoed Authors