Forum Discussion
Consecutive streak into a date range
- 4 years ago
Hi TerriAki
I was able to double the speed by creating relationships as per below screenshot. I tried on 2.7M rows table and still takes around 50-55 sec. on my machine which is not super-fast. Still too slow and also you have to know that the time increases exponentially with the number of rows and I have no idea how many columns you have. If you have too many columns we need to select only the relevant onesStart - End = IF ( 'T&A table 2'[Status] IN 'Sick Absence Code', VAR CurrentDate = 'T&A table 2'[Date] VAR EmployeeTable = CALCULATETABLE ( 'T&A table 2', ALLEXCEPT ( 'T&A table 2', 'T&A table 2'[Employee ID] ) ) VAR OffDaysTable = CALCULATETABLE ( 'T&A table 2', ALLEXCEPT ( 'T&A table 2','T&A table 2'[Employee ID] ), 'Sick Absence Code' ) VAR BreakDaysTable = CALCULATETABLE ( 'T&A table 2', ALLEXCEPT ( 'T&A table 2','T&A table 2'[Employee ID] ), 'Break codes' ) -- Calculating last day off VAR NexBreaksTable = FILTER ( BreakDaysTable, 'T&A table 2'[Date] >= CurrentDate ) VAR NextBreakDate = MINX ( NexBreaksTable, 'T&A table 2'[Date] ) VAR NextOffDaysTable = FILTER ( OffDaysTable, 'T&A table 2'[Date] < NextBreakDate ) VAR LastDayOff = MAXX ( NextOffDaysTable, 'T&A table 2'[Date] ) RETURN IF ( CurrentDate = LastDayOff, -- Calculating first day off VAR PreviousBreaksTable = FILTER ( BreakDaysTable, 'T&A table 2'[Date] <= CurrentDate ) VAR PreviousBreakDate = MAXX ( PreviousBreaksTable, 'T&A table 2'[Date] ) VAR PreviousOffDaysTable = FILTER ( OffDaysTable, 'T&A table 2'[Date] > PreviousBreakDate ) VAR FirstDayOff = MINX ( PreviousOffDaysTable, 'T&A table 2'[Date] ) VAR Result = FirstDayOff & " - " & LastDayOff RETURN Result ) )
I think I can handle the open ended absence by getting Power Query to put in a final row for each Employee with a break code. Regarding optimisation, it currently isnt running on the full dataset due to running out of memory (I have 16 GB ram and have manually tried increasing Power BI cache settings).
Hi TerriAki
I was able to double the speed by creating relationships as per below screenshot. I tried on 2.7M rows table and still takes around 50-55 sec. on my machine which is not super-fast. Still too slow and also you have to know that the time increases exponentially with the number of rows and I have no idea how many columns you have. If you have too many columns we need to select only the relevant ones
Start - End =
IF (
'T&A table 2'[Status] IN 'Sick Absence Code',
VAR CurrentDate =
'T&A table 2'[Date]
VAR EmployeeTable =
CALCULATETABLE ( 'T&A table 2', ALLEXCEPT ( 'T&A table 2', 'T&A table 2'[Employee ID] ) )
VAR OffDaysTable =
CALCULATETABLE ( 'T&A table 2', ALLEXCEPT ( 'T&A table 2','T&A table 2'[Employee ID] ), 'Sick Absence Code' )
VAR BreakDaysTable =
CALCULATETABLE ( 'T&A table 2', ALLEXCEPT ( 'T&A table 2','T&A table 2'[Employee ID] ), 'Break codes' )
-- Calculating last day off
VAR NexBreaksTable =
FILTER ( BreakDaysTable, 'T&A table 2'[Date] >= CurrentDate )
VAR NextBreakDate =
MINX ( NexBreaksTable, 'T&A table 2'[Date] )
VAR NextOffDaysTable =
FILTER ( OffDaysTable, 'T&A table 2'[Date] < NextBreakDate )
VAR LastDayOff =
MAXX ( NextOffDaysTable, 'T&A table 2'[Date] )
RETURN
IF (
CurrentDate = LastDayOff,
-- Calculating first day off
VAR PreviousBreaksTable =
FILTER ( BreakDaysTable, 'T&A table 2'[Date] <= CurrentDate )
VAR PreviousBreakDate =
MAXX ( PreviousBreaksTable, 'T&A table 2'[Date] )
VAR PreviousOffDaysTable =
FILTER ( OffDaysTable, 'T&A table 2'[Date] > PreviousBreakDate )
VAR FirstDayOff =
MINX ( PreviousOffDaysTable, 'T&A table 2'[Date] )
VAR Result =
FirstDayOff & " - " & LastDayOff
RETURN
Result
)
)- TerriAki4 years agoFrequent Visitor
tamerj1 Speed is much better now and works with my large dataset however the result is not right. The original code was accurate. Sample attached, you can see original code in column 'T&A Table'[Start - End (v1)] and new code in 'T&A Table'[Start - End (v2)].
https://drive.google.com/file/d/1niL5GdrIXRIVKw6DYfrYDVu5qpgkuTsU/view?usp=sharing