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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculated column to determine if date is between 2 dates on another table

Hi, hopefully someone can help me with this. I have a table that looks like the following

ID              CreatedDate       Actions

3334          01-Jun-2019          2

3334          29-Jun-2019          1

 

There is also another table linked using ID that looks like the below

ID                     Start                   End

3334          01-May-2019           15-Jun-2019 

 

What I want is to add a calculated column to the first table that says if the CreatedDate is between Start and End on the second table, then 1, else 0.

 

So an ideal output for ID 3334 would be 

 

ID              CreatedDate       Actions          Flag

3334          01-Jun-2019          2                  1

3334          29-Jun-2019          1                  0

 

Can someone please help with this? 

 

Appreciate any help

Thank you

 

2 ACCEPTED SOLUTIONS
TomMartens
Super User
Super User

Hey,

 

wondering if there is just one row per id in the 2nd table?

If this is the case then this DAX can be used to create a calculated column:

a new calculated column = 
var _ID = '<1st table>'[ID]
var _CreatedDate = '<1st table>'[CreatedDate]
var startdate = LOOKUPVALUE('<2nd table>'[Start] , '<2nd table>'[ID] , _ID)
var enddate = LOOKUPVALUE('<2nd table>'[End] , '<2nd table>'[ID] , _ID)
return
IF(AND ( _CreatedDate >= startdate , _CreatedDate <= enddate) , 1 , 0)

Hopefully this is what you are looking for

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

It should automatically use relationships between tables.  If it doesn't, make sure you have a relationship between them set up on linked ID#s.  If it STILL isn't working, or starts to complain about how a single value can't be determined, try using RELATED to get the specific value.

View solution in original post

4 REPLIES 4
TomMartens
Super User
Super User

Hey,

 

wondering if there is just one row per id in the 2nd table?

If this is the case then this DAX can be used to create a calculated column:

a new calculated column = 
var _ID = '<1st table>'[ID]
var _CreatedDate = '<1st table>'[CreatedDate]
var startdate = LOOKUPVALUE('<2nd table>'[Start] , '<2nd table>'[ID] , _ID)
var enddate = LOOKUPVALUE('<2nd table>'[End] , '<2nd table>'[ID] , _ID)
return
IF(AND ( _CreatedDate >= startdate , _CreatedDate <= enddate) , 1 , 0)

Hopefully this is what you are looking for

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Cmcmahan
Resident Rockstar
Resident Rockstar

Sure.  Be sure to update this with <= where appropriate.

 

Flag = IF( Table2[Start] < [CreatedDate] && [CreatedDate] < Table2[End], 1, 0)

I haven't tried this in actual PowerBI, so if you run into an issue where it's not recognizing Table2[Start]/Table2[End],  use RELATED(Table2[Start]) to fix it.

Anonymous
Not applicable

Does this take into account that each ID has different Start and End?

It should automatically use relationships between tables.  If it doesn't, make sure you have a relationship between them set up on linked ID#s.  If it STILL isn't working, or starts to complain about how a single value can't be determined, try using RELATED to get the specific value.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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