The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
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.
Does anybody have an idea on how to do this?
Kind regards,
Jeroen
Solved! Go to Solution.
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
Proud to be a Super User!
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])))
please see the attachment below.
Proud to be a Super User!
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
Proud to be a Super User!
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]
Proud to be a Super User!
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?
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]
Proud to be a Super User!
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
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!
@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:-)
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])))
please see the attachment below.
Proud to be a Super User!
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
Proud to be a Super User!
User | Count |
---|---|
65 | |
60 | |
55 | |
54 | |
31 |
User | Count |
---|---|
180 | |
88 | |
70 | |
46 | |
45 |