Forum Discussion
Counting days in a period
I have several records with a StartDate and an EndDate of a period.
I need to calculate days between this dates.
The real problem is in filtering the data.
For example If i have a StartDate = DATE(2018,1,1) and an EndDate(2018,2,2) and I would like to filter the period in February, the formula have to return two days
Hi lfrigione,
Just to clarify one thing, in the GIF beware that the dates are in US format (an error of PBI that is being solved), so the dates from 1/1/2017 - 3/1/2017 is January 1st to March 1st in this case is 10 days.
If you change the slicer to January 1st to January 3rd you get 3 days because in my formula I added +1 didn't know if you want the datediff result or the count of all days, just make the formula like this:
Counting_days = VAR START_DATE = MIN ( DimDate[Date] ) VAR END_DATE = MAX ( DimDate[Date] ) RETURN SUMX ( SUMMARIZE ( FactTable; FactTable[StartDate]; FactTable[EndDate]; "DAYS"; IF ( FactTable[StartDate] < START_DATE && FactTable[EndDate] < START_DATE || FactTable[StartDate] > END_DATE && FactTable[EndDate] > END_DATE; 0; DATEDIFF ( IF ( FactTable[StartDate] < START_DATE; START_DATE; FactTable[StartDate] ); IF ( FactTable[EndDate] > END_DATE; END_DATE; FactTable[EndDate] ); DAY ) ) ); [DAYS] )But be aware that if you get the period from 1 January to 1 January de days difference is 0 and not 1.
See the PBIX sample with the formula above working and try to check if this is the resul you want.
Regards,
MFelix
23 Replies
- MFelix
Super User
- lfrigione
Helper II
The problem with DATEDIFF is that:
in a filter i can put a record field that in this case may be the StartDate or the EndDate,
if one of this two dates don't match the filter it trunkate all the period without counting the days that match the filter
- MFelix
Super User
Hi lfrigione,
When you have a filter you need to place make some small changes to your calculations in order to have it well made.
In order to help you and picking up your example.
If you select in your filter only the date of 01/02/2018 as end date should your calculation be made as 01/02/2018 to 01/02/2018 so 0 days or should it give something else.
Can you elaborate with different examples of dates so I can pin point exactly how the measure you need can be better writen.
Regards,
MFelix