Forum Discussion

TimProvelu's avatar
TimProvelu
Frequent Visitor
5 years ago
Solved

Bad performance in calculating datediff without weekends/holidays

I am trying to perform a datediff in DAX excluding certain days and hours. This datediff should give me the results in hours. I have checked the internet and found a lot of helpful solutions, but cou...
  • Anonymous's avatar
    Anonymous
    5 years ago

    The rules are these:

    1. You should avoid as much as possible calculated columns in DAX, especially in fact tables. 

    2. When you have to put in a calc column, you should refrain as much as passible from using CALCULATE.

    3. Do the calculation in the source or in Power Query.

     

    There are too many reasons behind those statements to be able to expand on them here. If you want to know their source, please read The Definitive Guide to DAX by The Italians.

     

    If you really want to do what you should not, you can try this:

    DateDiffWithoutWeekends =
    // First of all, you should calculate
    // things once only, never repeat
    // the same calculation
    VAR MinDate = MIN( Facttable[Date1] )
    var MinTime = MIN( Facttable[Time1] )
    VAR MinDateTime1 = MinDate + MinTime
    VAR MinDateTime2 = MIN( Facttable[DateTime2] )
    RETURN
    	COUNTROWS(
    		FILTER(
    			HulpDatumTijd,
    			true()
    			&& NOT( HulpDatumTijd[is Weekend] )
    			&& HulpDatumTijd[Datum] >= MinDate
    			&& HulpDatumTijd[Datum] <= MinDateTime2
    			&& HulpDatumTijd[DatumTijd] > MinDateTime1
    			&& HulpDatumTijd[DatumTijd] < MinDateTime2
    		)
    	)