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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
heiligbd
Helper I
Helper I

Match Closest Length to Existing Available Length Between Two Tables

So I have two tables. One with new sales orders and one with existing inventory. I want to match the existing inventory to the sales orders by both material # and the closest length that is longer than the requested length. I can't match a sales order to shorter existing inventory but anything that is equal to or longer is acceptable.  Below would be sample examples of tables and what I would like to match in yellow between the batches and the lengths. In the example, you can see that some orders would not have matching inventory available, but others would potentially have multiple available lengths that would work, but I want the closest to (equal or longer) to match to the ordered length by best fit. Therefore, if you had an order at 3 length, and on hand inventory at 3.5 and 4, I would want 3.5 to match to the order not 4.

heiligbd_0-1676577467467.png

 

 

 

Table 1  
Sales Order #Material #Length Ordered
1A3
2A3.5
3B9
4C4
5B6.5
6A4.25
7C2

Thanks in advance for your help! 

Table 2  
Material #BatchOn Hand Length
A114.35
A222.95
A334.15
B448.5
B556.75
C664.25
C772.25

 

 

Matched Table    
Sales Order #MaterialLength OrderedMatched BatchOn Hand Length
1A3No MatchNo Match
2A3.5334.15
3B9No MatchNo Match
4C4664.25
5B6.5556.75
6A4.25114.35
7C2772.25
2 ACCEPTED SOLUTIONS
DataInsights
Super User
Super User

@heiligbd,

 

Try these calculated columns in Table1:

 

Matched Batch = 
VAR vMaterial = Table1[Material #]
VAR vLengthOrdered = Table1[Length Ordered]
VAR vInventory =
    FILTER (
        Table2,
        Table2[Material #] = vMaterial
            && Table2[On Hand Length] >= vLengthOrdered
    )
VAR vMinOnHandLength =
    MINX ( vInventory, Table2[On Hand Length] )
VAR vMinBatch =
    MINX (
        FILTER ( vInventory, Table2[On Hand Length] = vMinOnHandLength ),
        Table2[Batch]
    )
VAR vResult =
    IF ( ISBLANK ( vMinBatch ), "No Match", CONVERT ( vMinBatch, STRING ) )
RETURN
    vResult
On Hand Length = 
VAR vMaterial = Table1[Material #]
VAR vLengthOrdered = Table1[Length Ordered]
VAR vInventory =
    FILTER (
        Table2,
        Table2[Material #] = vMaterial
            && Table2[On Hand Length] >= vLengthOrdered
    )
VAR vMinOnHandLength =
    MINX ( vInventory, Table2[On Hand Length] )
VAR vResult =
    IF ( ISBLANK ( vMinOnHandLength ), "No Match", CONVERT ( vMinOnHandLength, STRING ) )
RETURN
    vResult

 

DataInsights_0-1676643842348.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

heiligbd
Helper I
Helper I

Awesome! Worked like a Champ! Thanks so much!

View solution in original post

2 REPLIES 2
heiligbd
Helper I
Helper I

Awesome! Worked like a Champ! Thanks so much!

DataInsights
Super User
Super User

@heiligbd,

 

Try these calculated columns in Table1:

 

Matched Batch = 
VAR vMaterial = Table1[Material #]
VAR vLengthOrdered = Table1[Length Ordered]
VAR vInventory =
    FILTER (
        Table2,
        Table2[Material #] = vMaterial
            && Table2[On Hand Length] >= vLengthOrdered
    )
VAR vMinOnHandLength =
    MINX ( vInventory, Table2[On Hand Length] )
VAR vMinBatch =
    MINX (
        FILTER ( vInventory, Table2[On Hand Length] = vMinOnHandLength ),
        Table2[Batch]
    )
VAR vResult =
    IF ( ISBLANK ( vMinBatch ), "No Match", CONVERT ( vMinBatch, STRING ) )
RETURN
    vResult
On Hand Length = 
VAR vMaterial = Table1[Material #]
VAR vLengthOrdered = Table1[Length Ordered]
VAR vInventory =
    FILTER (
        Table2,
        Table2[Material #] = vMaterial
            && Table2[On Hand Length] >= vLengthOrdered
    )
VAR vMinOnHandLength =
    MINX ( vInventory, Table2[On Hand Length] )
VAR vResult =
    IF ( ISBLANK ( vMinOnHandLength ), "No Match", CONVERT ( vMinOnHandLength, STRING ) )
RETURN
    vResult

 

DataInsights_0-1676643842348.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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 Solution Authors
Top Kudoed Authors