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
babajat
Frequent Visitor

LOOKUPVALUE if date difference is smaller than two years

Good morning,

I need help with a problem that has been bugging me for days.

These are my data:

IDDATE ADATE DI1I2I3
AA30/01/201701/02/2017FFF ZZZ
BB24/11/202124/11/2021 GGG 
CC22/03/201622/03/2016 HHH 
DD02/10/201802/10/2018PPPYYY 
EE06/08/201906/08/2019JJJ  
AA21/08/201821/08/2018LLL  
XX03/11/202003/11/2020 MMMXYZ
CC24/09/201924/09/2019NNN  
FF20/02/201820/02/2018 BBB 
XX25/05/202125/05/2021 VVV 
NN31/10/201931/10/2019CCCZZZ 
MM11/06/201911/06/2019 SSS 
HH09/11/201809/11/2018CCCQQQ 
MM26/11/201926/11/2019  UUU
MM26/02/202127/02/2021  RRR
UU26/07/201926/07/2019 TTT 
PP18/12/201818/12/2018YYY  
YY22/04/202222/04/2022 IIILLL
TT07/03/201807/03/2018VVVCCC 
EE01/10/202201/11/2022CCCXXX 

 

I have 2 questions:

- QUESTION 1: I need a new calculated column that returns 1 if there is an entry with the same ID with a DATE D dating back less than 2 years (730 days) from the DATE A of interest;

- QUESTION 2: I need another new calculated column that returns 1 if there is an entry that meets the criteria of QUESTION 1 and also has the values ​​"XYZ" OR "UUU" in any column between I1, I2 OR I3.

 

Here's the expected result:

IDDATE ADATE DI1I2I3QUESTION 1QUESTION 2
AA30/01/201701/02/2017FFF ZZZ  
BB24/11/202124/11/2021 GGG   
CC22/03/201622/03/2016 HHH   
DD02/10/201802/10/2018PPPYYY   
EE06/08/201906/08/2019JJJ    
AA21/08/201821/08/2018LLL  1 
XX03/11/202003/11/2020 MMMXYZ  
CC24/09/201924/09/2019NNN    
FF20/02/201820/02/2018 BBB   
XX25/05/202125/05/2021 VVV 11
NN31/10/201931/10/2019CCCZZZ   
MM11/06/201911/06/2019 SSS   
HH09/11/201809/11/2018CCCQQQ   
MM26/11/201926/11/2019  UUU1 
MM26/02/202127/02/2021  RRR11
UU26/07/201926/07/2019 TTT   
PP18/12/201818/12/2018YYY    
YY22/04/202222/04/2022 IIILLL  
TT07/03/201807/03/2018VVVCCC   
EE01/10/202201/11/2022CCCXXX   

 

I hope I was clear, thanks in advance.

1 ACCEPTED SOLUTION
v-yueyunzh-msft
Community Support
Community Support

Hi , @babajat 

According to your description, you want to create two calculated columns from your logic. Right?

Here are the steps you can refer to :
(1)This is my test data:

vyueyunzhmsft_0-1670814529613.png

(2)We can click "New Column" and enter this:

Q1 = var _cur_ID = [ID] 
var _cur_DATE_A = [DATE A] 
var _cur_DATE_D = [DATE D] 
var _pre_dateA  =MAXX( FILTER( 'Table' , 'Table'[ID] =_cur_ID && 'Table'[DATE A] < _cur_DATE_A) , [DATE A])
var _days = IF(_pre_dateA <> BLANK() , INT(_cur_DATE_D-_pre_dateA))
return
IF(_days <= 730 && _days<> BLANK(), 1, 0)
Q1 = var _cur_ID = [ID] 
var _cur_DATE_A = [DATE A] 
var _cur_DATE_D = [DATE D] 
var _pre_dateA  =MAXX( FILTER( 'Table' , 'Table'[ID] =_cur_ID && 'Table'[DATE A] < _cur_DATE_A) , [DATE A])
var _days = IF(_pre_dateA <> BLANK() , INT(_cur_DATE_D-_pre_dateA))
return
IF(_days <= 730 && _days<> BLANK(), 1, 0)

(3)Then we can meet your need , the result is as follows:

vyueyunzhmsft_1-1670814988472.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

These calculated column formulas work

Column = if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[DATE d]>=EARLIER(Data[DATE A])-730&&Data[DATE D]<=EARLIER(Data[DATE A])))>1,1,BLANK())
Column 2 = if(Data[Column]=1,CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[DATE d]>=EARLIER(Data[DATE A])-730&&Data[DATE D]<EARLIER(Data[DATE A])&&(Data[I1]="XYZ"||Data[I1]="UUU"||Data[I2]="XYZ"||Data[I2]="UUU"||Data[I3]="XYZ"||Data[I3]="UUU"))),BLANK())

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yueyunzh-msft
Community Support
Community Support

Hi , @babajat 

According to your description, you want to create two calculated columns from your logic. Right?

Here are the steps you can refer to :
(1)This is my test data:

vyueyunzhmsft_0-1670814529613.png

(2)We can click "New Column" and enter this:

Q1 = var _cur_ID = [ID] 
var _cur_DATE_A = [DATE A] 
var _cur_DATE_D = [DATE D] 
var _pre_dateA  =MAXX( FILTER( 'Table' , 'Table'[ID] =_cur_ID && 'Table'[DATE A] < _cur_DATE_A) , [DATE A])
var _days = IF(_pre_dateA <> BLANK() , INT(_cur_DATE_D-_pre_dateA))
return
IF(_days <= 730 && _days<> BLANK(), 1, 0)
Q1 = var _cur_ID = [ID] 
var _cur_DATE_A = [DATE A] 
var _cur_DATE_D = [DATE D] 
var _pre_dateA  =MAXX( FILTER( 'Table' , 'Table'[ID] =_cur_ID && 'Table'[DATE A] < _cur_DATE_A) , [DATE A])
var _days = IF(_pre_dateA <> BLANK() , INT(_cur_DATE_D-_pre_dateA))
return
IF(_days <= 730 && _days<> BLANK(), 1, 0)

(3)Then we can meet your need , the result is as follows:

vyueyunzhmsft_1-1670814988472.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Thejeswar
Resident Rockstar
Resident Rockstar

Hi @babajat,

You can use the following measures

Question 1 = VAR SELECTID = Table2[ID]
VAR MAXDATE = CALCULATE(MAX(Table2[DATE A]), FILTER(Table2, Table2[ID] = SELECTID))
VAR MINDATE = CALCULATE(MIN(Table2[DATE D]), FILTER(Table2, Table2[ID] = SELECTID))
RETURN
IF(INT(MAXDATE - MINDATE) > 730, 1, 0)
 
Question 2 = IF(Table2[Question 1] = 1 && (Table2[I1] IN {"XYZ", "UUU"} || Table2[I2] IN {"XYZ", "UUU"} || Table2[I3] IN {"XYZ", "UUU"}), 1, 0)
 
The Output looks like shown below
Thejeswar_0-1670592554794.png

 

Hi! Thanks for the answer. I'm afraid that your measure is returning 1 if the difference is grater than 730 days instead of smaller. If i swap the ">730" with "<730" it stops working correctly for some reason (it seems that it stops filtering by ID) 😵

 

Furthermore, using this measure returns "1" on both the most recent and oldest entries, is there a way to highlight only the most recent?

Hi @babajat ,

My bad!! This should be created as a Column and not a measure. But mentioned it as measure by mistake.

 

Kindly create the Columns as shown above. That is returning the right values when < 730 is mentioned in the logic. Attached below the screenshot

 

Thejeswar_0-1670835394389.png

 

Regards,

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.