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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
HeShootsNScores
Frequent Visitor

If statement (greater than) different tables

Hi - 

 

Been pounding my head against this for weeks.. hopefully someone can help. I'm trying to calculate whether or not we've placed PO's at or before the required lead time.

 

I have 3 tables:

 

Vendor

PurchaseOrderHeader

PurchaseOrderDetail

 

2020-05-27_1420.png

 

The StandardLeadTime column is in the Vendor Table

 

2020-05-27_1426.png2020-05-27_1425.png

and the Lead Time on PO column is a calculated colum in the PurchaseOrderDetail table

 

 

Lead Time on PO = DATEDIFF(RELATED(PO_PurchaseOrderHeader[DateCreated]),PO_PurchaseOrderDetail[UDF_ON_DOCK_ETA],DAY)
 
What I want to figure out, is how to make a caculated column that says IF the number in the Lead Time on PO (in the PurchaseOrderDetails Table) >= the number in the StandardLeadTime column (in the Vendor Table), "Yes", otherwise, "No"
 
I keep getting this
 
 
1 ACCEPTED SOLUTION
v-lionel-msft
Community Support
Community Support

Hi @HeShootsNScores ,

 

Model 1

dd4.PNG

Result1

dd5.PNG

 

Model2 

dd6.PNG

Result2

dd7.PNG

RELATED() 

 

Try to create a measure like this.

Measure 2 = 
VAR x = 
CALCULATE(
    SUM(Vendor[Value]),
    FILTER( Vendor, Vendor[ItemCode] = SELECTEDVALUE(Vendor[ItemCode]))
)
RETURN
IF(
    SELECTEDVALUE(PurchaseOrderDetail[Value]) >= x,
    "Yes", "No"
)

dd8.PNG

 

 

Or Create a calculated column like this.

Column 2 = 
VAR x = 
CALCULATE(
    SUM(Vendor[Value]),
    ALLEXCEPT(PurchaseOrderDetail, PurchaseOrderDetail[ItemCode])
)
RETURN
IF(
    PurchaseOrderDetail[Value] >= x,
    "yES", "nO"
)

dd9.PNG

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
v-lionel-msft
Community Support
Community Support

Hi @HeShootsNScores ,

 

Model 1

dd4.PNG

Result1

dd5.PNG

 

Model2 

dd6.PNG

Result2

dd7.PNG

RELATED() 

 

Try to create a measure like this.

Measure 2 = 
VAR x = 
CALCULATE(
    SUM(Vendor[Value]),
    FILTER( Vendor, Vendor[ItemCode] = SELECTEDVALUE(Vendor[ItemCode]))
)
RETURN
IF(
    SELECTEDVALUE(PurchaseOrderDetail[Value]) >= x,
    "Yes", "No"
)

dd8.PNG

 

 

Or Create a calculated column like this.

Column 2 = 
VAR x = 
CALCULATE(
    SUM(Vendor[Value]),
    ALLEXCEPT(PurchaseOrderDetail, PurchaseOrderDetail[ItemCode])
)
RETURN
IF(
    PurchaseOrderDetail[Value] >= x,
    "yES", "nO"
)

dd9.PNG

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

This worked! Your model 2 calculated column worked. Thank you!

d_gosbell
Super User
Super User


@HeShootsNScores wrote:

 

The StandardLeadTime column is in the Vendor Table

 

2020-05-27_1426.png

 
 

Your relationship is going in the wrong direction to be able to use RELATED() against the vendor table. The way you have the relationships modelled is effectively saying that one purchase order line can be related to many different vendors. This does not sound correct to me.

 

If you edit this relationship and change the cardinality from many-to-one to one-to-many  (or vis-versa) this will switch the From/To columns and the RELATED() function will then be able to function as you expect.

mahoneypat
Employee
Employee

Is it a 1:many relationship between Vendor and OrderDetail tables?  Is itemcode the same data type on both sides of the relationship?   If so, Related should work. If not, you'll need to change it to that or use RELATEDTABLE() and an aggregation.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors