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
aktripathi2506
Helper IV
Helper IV

Calculating number of weeks between two dates from different year

Hi,

 

 

 

Situation is like this: Suppose i Have a drug data with its expiry date:

 

Drug NameExp
Alp 32002-12-2018
B02-10-2017
C12-04-2016
D22-04-2010
E03-07-2020
F23-05-2017
G24-05-2016
H25-05-2016
I26-05-2018
J27-05-2016
K27-05-2019
L29-05-2011
M30-05-2012
N03-01-2010
O03-06-2016
P02-06-2017

 

 

Exp. date is of different year.

 

Now I have another column with date, based on which I calculated the week number.

 

Current DateWeek number
14-08-201634
15-08-201634
16-08-201634
17-08-201634
18-08-201634
19-08-201634
20-08-201634
21-08-201635
22-08-201635
23-08-201635
24-08-201635
25-08-201635
26-08-201635
27-08-201635
28-08-201636
29-08-201636
30-08-201636
31-08-201636
01-09-201636
02-09-201636
03-09-201636
04-09-201637

 

Now I have 2 slicer from this: Year and Weeknum.

When I select year as 2016 and week as 34 then I should get the name of drugs with their week to expiry data. (how many weeks remaining to expire the medicine)

 

 

I tried:  weeknum(exp) - weeknum(current date)

for same year (exp year = 2016) it is working fine for other years it is not working fine.

 

I am trying for a dynamic report where i can go to past or futer and figure out at tht time how many weeks were remaining for the drug to expire.

 

Meaning based on week slicer I should be able to see the data according to the week selected.

 

Thanks.. Please advise.

1 ACCEPTED SOLUTION

Hi @aktripathi2506,

To calculate number of weeks between two dates, you can firstly calculate days between dates using formula likes below:

DaysNo = IF(Table6[Exp]<Table6[Current Date],DATEDIFF(Table6[Exp],Table6[Current Date],DAY),1*DATEDIFF(Table6[Current Date],Table6[Exp],DAY))

Then you are able to calculate weeks between dates by using the following formula.

Weeknum = ROUNDDOWN((Table6[DaysNo] / 7), 0)

There is an example for your reference, and you can review the example in this attached PBIX file.
1.PNG


Thanks,
Lydia Zhang

Community Support Team _ Lydia 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

7 REPLIES 7
Greg_Deckler
Super User
Super User

In general, I would combine your year with this, so something like:

 

YearAndWeek = YEAR([Exp]) * 100 + WEEKNUM([Exp])

Then you could calculate today as:

 

Today = YEAR(TODAY()) * 100 + WEEKNUM(TODAY())

Then you could go backwards and forwards and account for the year.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you @Greg_Deckler

But its not working

 

Suppose the exp date is 1-1-2017 then according to the suggested formula it will be

 

201700 + 1 = 201701

 

if I select week as 53 for the year 2016 than

 

201701 - 201653 = 48

Which is not correct.

Just a thought @Greg_Deckler

 

Is there any thing that we can fix the reference year say 2000 or 1900 and then we calculate the week number for the years and subtract them.

 

Suppose then for date 2 july 2017 the week number comes as 750 and for the date 2 sept 2016 date comes as 700 and then we take the difference = 50 weeks to go.

Hi @aktripathi2506,

To calculate number of weeks between two dates, you can firstly calculate days between dates using formula likes below:

DaysNo = IF(Table6[Exp]<Table6[Current Date],DATEDIFF(Table6[Exp],Table6[Current Date],DAY),1*DATEDIFF(Table6[Current Date],Table6[Exp],DAY))

Then you are able to calculate weeks between dates by using the following formula.

Weeknum = ROUNDDOWN((Table6[DaysNo] / 7), 0)

There is an example for your reference, and you can review the example in this attached PBIX file.
1.PNG


Thanks,
Lydia Zhang

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hey Hi,

This one best fits my need. 

But Can i ask for one more thing. There are cases where my no.of.days are even lesser than 7days. 

 

Which techincally means that the Week numbers to be 0. But is there a way i can set a condition for which the days no < 7 returns me 1. 

 

Thanks,

Govind

 

Anonymous
Not applicable

In addition you can directly calculate the difference in WEEK by setting WEEK at the end of DATEDIFF function !

Thank you @v-yuezhe-msft

It worked perfect, thats what I was looking for, Thank you.

Cheers!!

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.