Forum Discussion

Greg_Deckler's avatar
Greg_Deckler
Icon for Community Champion rankCommunity Champion
2 years ago
Solved

CALCULATE Challenge - Round 1

For those haters of No CALCULATE like AlexisOlson, ðŸ˜…, I challenge you to create measures with an explicit CALCULATE that for the prescribed scenarios perform anywhere close to the same speed as the ...
  • AlexisOlson's avatar
    AlexisOlson
    2 years ago

    I think this should qualify, though it isn't super clean.

     

    VAR _AllDates_ =
        SUMMARIZE (
        	ALL ( FactInternetSales ),
        	Dates[Year],
        	Dates[Date]
        )
    VAR _DateRange_ =
        WINDOW (
            1, ABS,
            0, REL,
            _AllDates_,
            ORDERBY ( Dates[Date] ),
            PARTITIONBY ( Dates[Year] )
        )
    VAR _Result =  CALCULATE ( [Internet Sales], _DateRange_ )
    RETURN
        _Result
  • AlexisOlson's avatar
    2 years ago
    thank God it's such an obvious and easy solution that even a DAX novice could understand and doesn't rely on a function introduced within that last year which pretty much means the solution was unsolvable for 7 years or so...

    WINDOW isn't required. A plain filter works fine too and has been around since the beginning.

     

    VAR _AllDates_ = SUMMARIZE ( ALL ( FactInternetSales ), Dates[Date]	)
    	
    VAR _CurrDate = MAX ( Dates[Date] )
    VAR _CurrYear = YEAR ( _CurrDate )
    
    VAR _DateRange_ =
        FILTER (
        	_ALLDates_,
        	YEAR ( Dates[Date] )  = _CurrYear &&
        	Dates[Date] <= _CurrDate
        )
    VAR _Result =  CALCULATE ( [Internet Sales], _DateRange_ )
    RETURN
        _Result

     

     

    However, there are still 15 other date intelligence measures in the file that are part of the challenge.

    I'm not really interested in doing all 15 other ones as I don't think they'll reveal much that this YTD example doesn't already. If there is one that you think is meaningfully different, let me know.

  • AlexisOlson's avatar
    AlexisOlson
    2 years ago

    Greg_Deckler, PW is an easy case where the TI solution or something like the following is just as fast.

    CALCULATE (
        [Internet Sales],
        TREATAS ( VALUES ( Dates[Prior Week Date] ), Dates[Date] )
    )