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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

Number of days between two dates only if at least one record exists in a related table

Hi,

 

I am new to Power BI and to DAX, and I am trying to create a measure.

 

For this question, we need to consider 2 fact tables and a few fields for each:

  • FactOpportunity
    • Id
    • Name
    • PRC_signoff_complete
    • Flipped_to_Proposal
  • FactOpportunityRequest
    • Id
    • OpportunityId (foreign key to FactOpportunity[Id])
    • Name

My goal is to calculate the number of days for each row, between PRC_signoff_complete and Flipped_to_Proposal, only when there is at least one record for that Opportunity in the FactOpportunityRequest table.

 

I was able to get the number of days by using the following syntax:

 

SWITCH (
        TRUE (),
        FactOpportunity[PRC_signoff_complete] > FactOpportunity[Flipped_to_Proposal], DATEDIFF ( FactOpportunity[Flipped_to_Proposal], FactOpportunity[PRC_signoff_complete], DAY ),
        FactOpportunity[PRC_signoff_complete] < FactOpportunity[Flipped_to_Proposal], DATEDIFF ( FactOpportunity[PRC_signoff_complete], FactOpportunity[Flipped_to_Proposal], DAY ) * -1
    )

 

 

Now I am stuck on the filtering, to get the number of days only when there is at least one record for that Opportunity in the FactOpportunityRequest table. If there is no record, it should be blank.

 

Any idea how to do that?

1 ACCEPTED SOLUTION
tamerj1
Community Champion
Community Champion

Hi @Anonymous 

Please try

=
IF (
    NOT ISEMPTY ( RELATEDTABLE ( FactOpportunityRequest ) ),
    VAR SignDate = FactOpportunity[PRC_signoff_complete]
    VAR FlipDate = FactOpportunity[Flipped_to_Proposal]
    VAR MinDate =
        MIN ( SignDate, FlipDate )
    VAR MaxDate =
        MAX ( SignDate, FlipDate )
    VAR NumOfDays =
        DATEDIFF ( MinDate, MaxDate, DAY )
    RETURN
        IF ( SignDate > FlipDate, NumOfDays, - NumOfDays )
)

View solution in original post

2 REPLIES 2
tamerj1
Community Champion
Community Champion

Hi @Anonymous 

Please try

=
IF (
    NOT ISEMPTY ( RELATEDTABLE ( FactOpportunityRequest ) ),
    VAR SignDate = FactOpportunity[PRC_signoff_complete]
    VAR FlipDate = FactOpportunity[Flipped_to_Proposal]
    VAR MinDate =
        MIN ( SignDate, FlipDate )
    VAR MaxDate =
        MAX ( SignDate, FlipDate )
    VAR NumOfDays =
        DATEDIFF ( MinDate, MaxDate, DAY )
    RETURN
        IF ( SignDate > FlipDate, NumOfDays, - NumOfDays )
)
Anonymous
Not applicable

Thank you @tamerj1 that helps a lot!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

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.