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
Solved! Go to Solution.
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.
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
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.
I will try to explain it better.
I have a table with a lot of fields but in this moment we focus on the StartDate and EndDate fields.
I have something like:
I need a formula that can sum all the days in every period, something like this: SUMX(datesSheet, DATEDIFF(StartDate, EndDate))
The problem is that i need also a filter that allow me to select a "subperiod".
It means that without the filter the formula return me 2 + 33
With the filter setted on February for example the formula have to trunkate the initial periods only in the period of February and then return me the total number of days: 2.
If I select December it will return 0.
Thank you very much for helping me @MFelix
Create a calendar table and make an active relationship with Start Date and a inactive relationship with the End Date
Then add this measure:
Countdays = VAR start_date = MIN ( DimDate[Date] ) VAR end_date = MAX ( DimDate[Date] ) RETURN CALCULATE ( SUMX ( FactTable; DATEDIFF ( IF ( FactTable[StartDate] <= start_date; start_date; FactTable[StartDate] ); IF ( FactTable[EndDate] >= end_date; end_date; FactTable[EndDate] ); DAY ) ); USERELATIONSHIP ( DimDate[Date]; FactTable[EndDate] ) )
This should give the expected result.
Please tell me if it works or if there are any questions.
The question was related with the way the formula as including/ excluding the end dates remove the relationships between date tables and Date columns and add the following measure:
Countdays = VAR start_date = MIN ( DimDate[Date] ) VAR end_date = MAX ( DimDate[Date] ) VAR counting_days = CALCULATE ( SUMX ( FactTable; DATEDIFF ( IF ( FactTable[StartDate] <= start_date; start_date; FactTable[StartDate] ); IF ( FactTable[EndDate] >= end_date; end_date; FactTable[EndDate] ); DAY ) ) ) + 1 RETURN IF ( counting_days < 0; 0; counting_days )
Should work as expected.
It not work, I'm thinking for a solution
These are my testing data:
The calendar table is calculated with this formula: Calendar = CALENDAR(DATE(2000;1;1); DATE(2025;12;31))
If I select March It return me 1 that is impossible, It have to return 15.
I have an Idea,
Can I create a calculated colum in the calendar that represent the number of periods that contains the day?.
For every record it have to calcuate how many periods meet this condition: FactTable[StartDate] <= CurrentDay <= FactTable[EndDate]
and save this information or something like that.
Find out who's part of the program this season, and welcome the new Super Users.
We're excited to announce our first cross-community 'Can You Solve These?' challenge!
Learn about the exciting things that happened in July.
All Data Stories Gallery contributions are reviewed for each month. We select a contribution and feature the community member the following month.
Learn how to develop custom web applications for Power BI using .NET Core 3.1 and .NET 5.