Forum Discussion
Calculating maximum time between two records
- 1 year ago
Yea my bad, as _incidents is a virtual table inside the same expression, DAX expects column references without the table name.
If you want to also include the time between the last incident and today, we will have to add some more variables inside the measure. Just append this to the existing variables and replace the RETURN row:
VAR last_incident = MAXX( FILTER( Incident, Incident[TypeAccident] = "AT" ), Incident[DateAndTime] ) VAR last_vs_today = COUNTROWS( FILTER( Dates, Dates[Date] > last_incident && Dates[Date] <= TODAY() && WEEKDAY( Dates[Date], 2 ) < 6 ) ) RETURN MAX( _max, last_vs_today )
Hi Anonymous
regarding the start date issue, you could just add a filter on the _incidents VAR. I added another VAR after it, called _filtered_incidents. It filters out all records where there is no prev incident.
Here's what the updated code might look like:
Record de jours sans accidents =
VAR _incidents =
ADDCOLUMNS(
FILTER( 'Accidents du travail', 'Accidents du travail'[Type d'accident du travail] = "AT avec arrêt" ),
"prev_incident",
CALCULATE(
MAX( 'Accidents du travail'[Date et heure.1]),
FILTER(
ALL( 'Accidents du travail' ),
'Accidents du travail'[Date et heure.1] < EARLIER( 'Accidents du travail'[Date et heure.1] ) &&
'Accidents du travail'[Type d'accident du travail] = "AT avec arrêt"
)
)
)
VAR _filtered_incidents =
FILTER(
_incidents,
NOT( ISBLANK( [prev_incident] ) )
)
VAR _max =
MAXX(
_filtered_incidents,
COUNTROWS(
FILTER(
'Calendar',
'Calendar'[Date] > [prev_incident] &&
'Calendar'[Date] <= [Date et heure.1] &&
WEEKDAY( 'Calendar'[Date], 2 ) < 6
)
)
)
VAR last_incident = MAXX( FILTER( 'Accidents du travail', 'Accidents du travail'[Type d'accident du travail] = "AT avec arrêt" ), 'Accidents du travail'[Date et heure.1] )
VAR last_vs_today =
COUNTROWS(
FILTER(
'Calendar',
'Calendar'[Date] > last_incident &&
'Calendar'[Date] <= TODAY() &&
WEEKDAY( 'Calendar'[Date], 2 ) < 6
)
)-1
RETURN MAX( _max, last_vs_today )
You also said, that something isn't working in your productive file? It's hard to tell.. Maybe you could elaborate a little what exactly the issue is? I will try my best to get this running...
Hello timalbers thanks again for your help.
I added your new variable and it work fine in the test file.
For the production file, I think my problem comes from the fact that I have incident from 4 different factory.
When i use the factory filter in my page, the incident record is not exact. But I think it's because theres no filter on the factory in the measure.
The column containing the name of the factory in my Accident table is named "Site".
Thanks for your help.
Br,
Philippe.