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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
JeroenHD
Helper I
Helper I

Lookup nearest match on multiple columns same table

Hello Power BI fanatics, 

 

i have this challenge and i can't get my head around on how to solve it. Preferably the solution i'm looking for would be Power Query, but Dax would also be much appreciated. 

The table below shows several part id's with an required quantity next to it. I would like to add another column which is the price i would be paying. The price is graduated. If the Qty = 1 than it should return the Default Price. Any other Qty > 1 should be evaluated against the QuantityBreaks and find the closest match. Then multiply the quantity agains the related Unitprice. 

 

Screenshot 2021-06-10 170555.png

 

Does anybody have an idea on how to do this? 

 

Kind regards,
Jeroen

4 ACCEPTED SOLUTIONS
DataInsights
Super User
Super User

@JeroenHD,

 

Try this calculated column (DAX):

 

Price = 
VAR vQty = Parts[Qty]
VAR vPriceToUse =
    SWITCH (
        TRUE (),
        vQty >= Parts[QuantityBreak3], Parts[UnitPrice3],
        vQty >= Parts[QuantityBreak2], Parts[UnitPrice2],
        vQty >= Parts[QuantityBreak1], Parts[UnitPrice1],
        Parts[DefaultPrice]
    )
VAR vResult = vQty * vPriceToUse
RETURN
    vResult

 

DataInsights_0-1623369884792.png

 





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

Proud to be a Super User!




View solution in original post

ryan_mayu
Super User
Super User

@JeroenHD 

you can try to use DAX to create a new column

Column = 
VAR qb1=ABS('Table'[Qty]-'Table'[QuantityBreak1])
VAR qb2=ABS('Table'[Qty]-'Table'[QuantityBreak2])
VAR qb3=ABS('Table'[Qty]-'Table'[Quantitybreak3])
VAR result=min(min(qb1,qb2),qb3)
return if('Table'[Qty]=1,'Table'[DefaultPrice],if(result=qb1,'Table'[Unitprice1],if(result=qb2,'Table'[Unitprice2],'Table'[Unitprice3])))

1.PNG

please see the attachment below.





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

Proud to be a Super User!




View solution in original post

@JeroenHD,

 

You can use the ISBLANK function to ignore blanks:

 

Price = 
VAR vQty = Parts[Qty]
VAR vPriceToUse =
    SWITCH (
        TRUE (),
        vQty >= Parts[QuantityBreak3] && NOT ISBLANK ( Parts[QuantityBreak3] ), Parts[UnitPrice3],
        vQty >= Parts[QuantityBreak2] && NOT ISBLANK ( Parts[QuantityBreak2] ), Parts[UnitPrice2],
        vQty >= Parts[QuantityBreak1] && NOT ISBLANK ( Parts[QuantityBreak1] ), Parts[UnitPrice1],
        Parts[DefaultPrice]
    )
VAR vResult = vQty * vPriceToUse
RETURN
    vResult




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

Proud to be a Super User!




View solution in original post

@JeroenHD,

 

Here's the Power Query version:

 

if [Qty] >= [QuantityBreak3] and [QuantityBreak3] <> null then
        [Qty] * [UnitPrice3]
      else if [Qty] >= [QuantityBreak2] and [QuantityBreak2] <> null then
        [Qty] * [UnitPrice2]
      else if [Qty] >= [QuantityBreak1] and [QuantityBreak1] <> null then
        [Qty] * [UnitPrice1]
      else
        [Qty] * [DefaultPrice]




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

Proud to be a Super User!




View solution in original post

8 REPLIES 8
JeroenHD
Helper I
Helper I

This works like a charm. What i forgot to mention is that Quantity breaks and Unit prices are not necessarily filled. Does anyone know how to ignore blanks in the Switch function?

@JeroenHD,

 

Here's the Power Query version:

 

if [Qty] >= [QuantityBreak3] and [QuantityBreak3] <> null then
        [Qty] * [UnitPrice3]
      else if [Qty] >= [QuantityBreak2] and [QuantityBreak2] <> null then
        [Qty] * [UnitPrice2]
      else if [Qty] >= [QuantityBreak1] and [QuantityBreak1] <> null then
        [Qty] * [UnitPrice1]
      else
        [Qty] * [DefaultPrice]




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

Proud to be a Super User!




Thanks @DataInsights this works perfectly! Didn't think of categorizing it. 

@JeroenHD,

 

You can use the ISBLANK function to ignore blanks:

 

Price = 
VAR vQty = Parts[Qty]
VAR vPriceToUse =
    SWITCH (
        TRUE (),
        vQty >= Parts[QuantityBreak3] && NOT ISBLANK ( Parts[QuantityBreak3] ), Parts[UnitPrice3],
        vQty >= Parts[QuantityBreak2] && NOT ISBLANK ( Parts[QuantityBreak2] ), Parts[UnitPrice2],
        vQty >= Parts[QuantityBreak1] && NOT ISBLANK ( Parts[QuantityBreak1] ), Parts[UnitPrice1],
        Parts[DefaultPrice]
    )
VAR vResult = vQty * vPriceToUse
RETURN
    vResult




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

Proud to be a Super User!




This is also a great solution. I can either take care of it in Dax or M now! Thanks for reaching the helping hand!

JeroenHD
Helper I
Helper I

@DataInsights & @ryan_mayu thank you so much. You are life savers! Cool to see there's more than one way to do it! Thanks again! Now i'm back into the books to see what you actually did:-)

ryan_mayu
Super User
Super User

@JeroenHD 

you can try to use DAX to create a new column

Column = 
VAR qb1=ABS('Table'[Qty]-'Table'[QuantityBreak1])
VAR qb2=ABS('Table'[Qty]-'Table'[QuantityBreak2])
VAR qb3=ABS('Table'[Qty]-'Table'[Quantitybreak3])
VAR result=min(min(qb1,qb2),qb3)
return if('Table'[Qty]=1,'Table'[DefaultPrice],if(result=qb1,'Table'[Unitprice1],if(result=qb2,'Table'[Unitprice2],'Table'[Unitprice3])))

1.PNG

please see the attachment below.





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

Proud to be a Super User!




DataInsights
Super User
Super User

@JeroenHD,

 

Try this calculated column (DAX):

 

Price = 
VAR vQty = Parts[Qty]
VAR vPriceToUse =
    SWITCH (
        TRUE (),
        vQty >= Parts[QuantityBreak3], Parts[UnitPrice3],
        vQty >= Parts[QuantityBreak2], Parts[UnitPrice2],
        vQty >= Parts[QuantityBreak1], Parts[UnitPrice1],
        Parts[DefaultPrice]
    )
VAR vResult = vQty * vPriceToUse
RETURN
    vResult

 

DataInsights_0-1623369884792.png

 





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

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.