Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
Hi,
I am trying to get the average days between inspection dates. Below is a breakdown of current dates by project (with blue and green dots being 2 different projects).
Below is my attempted DAX for getting the days between inspections, with an average calculation for averages later on:
DaysBetweenInspectionsALL = VAR CurrentProject = SELECTEDVALUE(Project[ProjectName]) VAR CurrentDate = SELECTEDVALUE('Date of Inspection'[date]) VAR LastInspectionDate = CALCULATE( MAX('Date'[Date]), FILTER( ALL(Project), Project[ProjectName] = CurrentProject && SELECTEDVALUE('Date of Inspection'[date]) < CurrentDate)) RETURN IF(LastInspectionDate = BLANK(), BLANK(), CurrentDate - LastInspectionDate)
However, this has returned all row values as '27/12/1774', even with whole number formatting. I belive this might be from using an incorrect field in the LastInspectionDate variable. Any help would be appreciated, thank you.
Solved! Go to Solution.
@BotBot1
Please try
AverageDaysBetweenInspections =
AVERAGEX (
SUMMARIZE ( 'HSE Inspection', Project[ProjectName], 'Date of Inspection'[date] ),
VAR CurrentDate = 'Date of Inspection'[date]
VAR CurrentProjectTable =
CALCULATETABLE (
SUMMARIZE ( 'HSE Inspection', Project[ProjectName], 'Date of Inspection'[date] ),
ALL ( 'Date of Inspection'[date] )
)
VAR TableBefore =
FILTER ( CurrentProjectTable, 'Date of Inspection'[date] < CurrentDate )
VAR PreviousDate =
MAXX ( TableBefore, 'Date of Inspection'[date] )
RETURN
IF ( NOT ISEMPTY ( TableBefore ), INT ( CurrentDate - PreviousDate ) )
)
Try removing the SELECTEDVALUE from the filter and casting the result as an int
DaysBetweenInspectionsALL =
VAR CurrentProject =
SELECTEDVALUE ( Project[ProjectName] )
VAR CurrentDate =
SELECTEDVALUE ( 'Date of Inspection'[date] )
VAR LastInspectionDate =
CALCULATE (
MAX ( 'Date'[Date] ),
FILTER (
ALL ( Project ),
Project[ProjectName] = CurrentProject
&& 'Date of Inspection'[date] < CurrentDate
)
)
RETURN
IF (
LastInspectionDate = BLANK (),
BLANK (),
INT ( CurrentDate - LastInspectionDate )
)
Hi @johnt75,
Thank you for this. It is currently saying "cannot find name [date]" for the LastInspectionDate variable! Can this be fixed?
How about
DaysBetweenInspectionsALL =
VAR CurrentProject =
SELECTEDVALUE ( Project[ProjectName] )
VAR CurrentDate =
SELECTEDVALUE ( 'Date of Inspection'[date] )
VAR LastInspectionDate =
CALCULATE (
MAX ( 'Date'[Date] ),
REMOVEFILTERS ( Project ),
Project[ProjectName] = CurrentProject,
'Date of Inspection'[date] < CurrentDate
)
RETURN
IF (
LastInspectionDate = BLANK (),
BLANK (),
INT ( CurrentDate - LastInspectionDate )
)
Seems like 'Date of Inspection' is at the many side od the relationship.
In this case either to CROSSFILTER-BOTH the relationship or MAXX ( RELATEDTABLE ) like
DaysBetweenInspectionsALL =
VAR CurrentProject =
SELECTEDVALUE ( Project[ProjectName] )
VAR CurrentDate =
SELECTEDVALUE ( 'Date of Inspection'[date] )
VAR LastInspectionDate =
CALCULATE (
MAX ( 'Date'[Date] ),
FILTER (
ALL ( Project ),
Project[ProjectName] = CurrentProject
&& MAXX ( RELATEDTABLE ( 'Date of Inspection' ), 'Date of Inspection'[date] ) < CurrentDate
)
)
RETURN
IF (
LastInspectionDate = BLANK (),
BLANK (),
INT ( CurrentDate - LastInspectionDate )
)
When a formula refers to multiple columns from multiple tables then I need to see the relationships of the model and the filter context of the visual.
Hi @tamerj1,
I hope this helps - screenshoot shows most/all relevant tables.
Notes on most relevant connections:
Filters on visual:
BusinessUnit is not (blank) or Fitout.
ProjectName does not contain 'Test '.
ProjectStatusName is Live. (Or ProjectStatusID is 2).
InspectionTypeName is Formal Inspection (not (blank) or Site Inspection).
Sorry @BotBot1 I just noticed your reply.
I don't see the 'Date' table in the screenshot. However, please try
DaysBetweenInspectionsALL =
VAR CurrentDate =
MAX ( 'Date'[date] )
RETURN
MAXX (
FILTER (
CALCULATETABLE (
SUMMARIZE ( 'HSE Inspection', Project[ProjectName], 'Date of Inspection'[date] ),
ALLEXCEPT ( 'HSE Inspection', Project[ProjectName] )
),
'Date of Inspection'[date] <= CurrentDate
),
'Date of Inspection'[date]
)
Hi @tamerj1,
Not a problem at all, thanks for your continued help.
My 'Date' table has no connections.
All rows are showing as '22/02/2023'
@tamerj1 ,
Just for the new measure. This is before:
If I add 'Date of Inspection'[Date], the [date] column has dates going up each row by a day from 01/01/2000 to 31/12/2029. If I remove the [date] column, DaysBetweenInspectionsALL becomes the latest inspection dates!
Let me get that straight. You're not trying to get the latest inspection date of the project but only want to filter the project related rows based on the selection of the disconnected date table by keeping the rows with related inspection date before the selected 'Date'[Date]
I apologise for not being clear/causing any confusion. The 'Date' table is not of great importance and I do not have a slicer for this either.
I am simply trying to get the difference in days between each inspection date for all projects. I eventually want to get the average days between inspections based on the project, which I believe will be categorised later on in a different DAX by getting the averages of days between inspections by project.
But ideally if possible, I would just like a DAX for the average days between inspections by project in one go!
@BotBot1
Please try
AverageDaysBetweenInspections =
AVERAGEX (
SUMMARIZE ( 'HSE Inspection', Project[ProjectName], 'Date of Inspection'[date] ),
VAR CurrentDate = 'Date of Inspection'[date]
VAR CurrentProjectTable =
CALCULATETABLE (
SUMMARIZE ( 'HSE Inspection', Project[ProjectName], 'Date of Inspection'[date] ),
ALL ( 'Date of Inspection'[date] )
)
VAR TableBefore =
FILTER ( CurrentProjectTable, 'Date of Inspection'[date] < CurrentDate )
VAR PreviousDate =
MAXX ( TableBefore, 'Date of Inspection'[date] )
RETURN
IF ( NOT ISEMPTY ( TableBefore ), INT ( CurrentDate - PreviousDate ) )
)
@tamerj1 ,
I don't suppose you could help me with the DAX for the difference between the last inspection date and today for each project in this case? Thank you
You may try
AverageDaysBtweenInspectionAndToday =
AVERAGEX (
VALUES ( Project[ProjectName] ),
INT (
TODAY ()
- CALCULATE (
MAX ( 'Date of Inspection'[date] ),
CROSSFILTER ( 'HSE Inspection'[DateHSEInspectionCreatedOnInteger], 'Date of Inspection'[DateAsInteger], BOTH )
)
)
)
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
18 | |
12 | |
10 | |
8 | |
7 |
User | Count |
---|---|
26 | |
16 | |
12 | |
12 | |
10 |