Forum Discussion

KevSPHK's avatar
KevSPHK
Frequent Visitor
3 years ago

DAX/ M-language help - Returning a result, when a value fall into a number range in the data

Hello,
my goal is to create a new column of "Order Unit Price" in power query or power bi table, which the "Order Unit Price" is depends on "Product" & "Vendor" & "Order quantity" relating to SOURCE table. 
May I ask how to write the syntex in Power BI to look up the order unit price from SOURCE TABLE? (sample structure are provided below) Many thanks

My Goal

ProductVendorOrder QuantityOrder Unit Price
ACat22??
BBat45??


Source Table

ProductVendorPrice LevelMinimum Quantity Unit Price 
ACatPx Lev 11            10.00
ACatPx Lev 221              8.00
ACatPx Lev 341              7.00
ABatPx Lev 11            10.00
ABatPx Lev 231              8.50
ABatPx Lev 346              7.10
BCatPx Lev 11              8.00
BCatPx Lev 221              6.00
BCatPx Lev 341              5.00
BBatPx Lev 11              8.00
BBatPx Lev 231              6.50
BBatPx Lev 346              5.10

 

2 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    M code

    NewStep=let a=Table.Buffer(Table.Group(SourceTable,{"Product","Vendor"},{"n",each Table.Sort(_,{"Minimum Quantity",1})})) in Table.AddColumn(YourOrderList,"OrderUnitPrice",each let b=a{[Product=[Product],Vendor=[Vendor]]}? in if b=null then null else Table.Skip(b,(x)=>x[Minimum Quantity]>[Order Quantity]){0}?[Unit Price]?)

     

    Dax code

    if there are no relationships between those tables, this code can create a calculated column in table Order

    Unit Price Column=MAXX(TOPN(1,FILTER(SourceTable,SourceTable[Product]=OrderTable[Product]&&SourceTable[Vendor]=OrderTable[Vendor]&&SourceTable[Minimum Quantity]<=OrderTable[Order Quantity]),SourceTable[Minimum Quantity]),SourceTable[Unit Price])