Forum Discussion
Average duration between two dates is not calculated
- 4 years ago
actually when I saw your reply I had a 2nd look at the solution and the results.
In your report you are slicing by 'vw_powerBI'[stamnummer] which means that the filter context contains the table of each student and there is no need to CALCULATETABLE - ALLEXCEPT. Therefore, the VALUES ( 'vw_powerBI'[Graduated?] ) should contain all the "Yes" and "No" values for each "stamnummer". The limitation I set is: IF at least one "Yes" is available for the current student then do the calculation otherwise return blank. I see no reason why this shouldn't work.However this condition need to be applied inside the SUMX as well in order to limit the total average (In the column total cell) to only the students who have "Yes" in their Graduate status.
Study duration = IF ( "Yes" IN VALUES ( 'vw_powerBI'[Graduated?] ), AVERAGEX ( VALUES ( 'vw_powerBI'[stamnummer] ), IF ( "Yes" IN CALCULATETABLE ( VALUES ( 'vw_powerBI'[Graduated?] ) ), DATEDIFF ( CALCULATE ( MIN ( 'vw_powerBI'[registrationdate] ) ), CALCULATE ( MAX ( 'vw_powerBI'[deregistrationdate] ) ), DAY ) ) ) )
Thanks. This one gives a better result.
The dataset looks like this:
Studentnumber RegistrationDate DeregistrationDate Graduated?
00001 15/09/2019 30/06/2020 No
00001 15/09/2020 30/06/2021 No
00001 15/09/2021 30/06/2022 Yes
00002 15/09/2020 30/06/2021 No
00002 15/09/2021 30/06/2022 No
00003 ...
So the formula above may only work when there is one row with a yes for that student. Students who tried but did not graduated (only no's) cant be included in the calculation.
Thanks,
Jo
JoSwinnen
Please try
Study duration =
VAR Graduated =
CALCULATETABLE (
VALUES ( 'vw_powerBI'[Graduated?] ),
ALLEXCEPT ( 'vw_powerBI', 'vw_powerBI'[Studentnumber] )
)
RETURN
AVERAGEX (
VALUES ( 'vw_powerBI'[studentnumber] ),
CALCULATE (
IF (
"Yes" IN Graduated,
DATEDIFF (
MIN ( 'vw_powerBI'[registrationdate] ),
MAX ( 'vw_powerBI'[deregistrationdate] ),
DAY
)
)
)
)
Or
Study duration =
VAR CurrentStudentTable =
CALCULATETABLE (
'vw_powerBI',
ALLEXCEPT ( 'vw_powerBI', 'vw_powerBI'[Studentnumber] )
)
VAR FiteredTable =
FILTER ( CurrentStudentTable, 'vw_powerBI'[Graduated?] = "Yes" )
RETURN
AVERAGEX (
FiteredTable,
DATEDIFF (
MIN ( 'vw_powerBI'[registrationdate] ),
MAX ( 'vw_powerBI'[deregistrationdate] ),
DAY
)
)
- JoSwinnen4 years ago
Helper I
Hi,
I've tried both ways, but it all gives the same result. The study duration for each student graduated or not...
Thanks,
Jo
- tamerj14 years ago
Community Champion
Please try
Study duration = VAR Graduated = CALCULATETABLE ( VALUES ( 'vw_powerBI'[Graduated?] ), ALLEXCEPT ( 'vw_powerBI', 'vw_powerBI'[Studentnumber] ) ) RETURN IF ( "Yes" IN Graduated, AVERAGEX ( VALUES ( 'vw_powerBI'[studentnumber] ), CALCULATE ( DATEDIFF ( MIN ( 'vw_powerBI'[registrationdate] ), MAX ( 'vw_powerBI'[deregistrationdate] ), DAY ) ) ) )- JoSwinnen4 years ago
Helper I
Thank you for trying, but it still gives the same result.