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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
lpd82
Helper I
Helper I

Merge two tables by value range

Howdy!

 

I have two tables:

Table A

NameAmount
Bob1.000
Joe2.500
Billy3.000
Frank3.500
Joy6.000

Table B

AmountRates
00
1.1251
2.0002
3.0003
4.0004
5.0005

 

Result:

NameAmountRates
Bob1.0000
Joe2.5002
Billy3.0003
Frank3.5003
Joy6.0005

 

 

How do I merge the two tables and bring the rates over to table A based on the Amount on table A that is less than or equal to the amount of table B?

 

TIA

1 ACCEPTED SOLUTION
serpiva64
Solution Sage
Solution Sage

Hi,

to obtain this

serpiva64_0-1677004296838.png

you need to add a custom column

(OuterTable)=> List.Last( Table.SelectRows( Rates, (InnerTable)=> InnerTable[Amount]<=OuterTable[Amount])[Rates])

serpiva64_1-1677004340658.png

it is better also to sort column Amount in Rates and to buffer the table to optimize the query

= Table.Buffer( #"Sorted Rows")

 

You can find a fantastic explanation of it (which i have apllied here) in 

Free M Code Class from Basic to Advanced: Power Query Excel & Power BI, Custom Functions 365 MECS 12

https://www.youtube.com/watch?v=3ZkIwKBVkVE

by Excellisfun

It is the last argument of a long video

If this post is useful to help you to solve your issue, consider giving the post a thumbs up and accepting it as a solution!

 

 

 

View solution in original post

6 REPLIES 6
serpiva64
Solution Sage
Solution Sage

Hi,

to obtain this

serpiva64_0-1677004296838.png

you need to add a custom column

(OuterTable)=> List.Last( Table.SelectRows( Rates, (InnerTable)=> InnerTable[Amount]<=OuterTable[Amount])[Rates])

serpiva64_1-1677004340658.png

it is better also to sort column Amount in Rates and to buffer the table to optimize the query

= Table.Buffer( #"Sorted Rows")

 

You can find a fantastic explanation of it (which i have apllied here) in 

Free M Code Class from Basic to Advanced: Power Query Excel & Power BI, Custom Functions 365 MECS 12

https://www.youtube.com/watch?v=3ZkIwKBVkVE

by Excellisfun

It is the last argument of a long video

If this post is useful to help you to solve your issue, consider giving the post a thumbs up and accepting it as a solution!

 

 

 

@serpiva64 , 

 

Typically, I do this way with "each" ...
Table.AddColumn(Source, "Rates", each List.Last( Table.SelectRows(TableB, (x) => [Amount]>=x[Amount])[Rates]), Int64.Type)

 

It is interesting that you used (OuterTable) => ..., I learned a new way. 🙂 

The idea is from Excelisfun but i think it is more clear

Thank you very much, I watched your video which helps me understand the logic better.

serpiva64
Solution Sage
Solution Sage

Hi, 

i think there are some typing error in your table

Probably table B first value is 1000

and

Bob result is 1125 not 1000 

or something similar, can you confirm it?

ah! sorry about that.  I fixed table A.  Thanks

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.