Forum Discussion

markus_zhang's avatar
markus_zhang
Icon for Advocate III rankAdvocate III
7 years ago
Solved

How can I further optimize this DAX formula?

Hi experts, I have a table with 12 million rows and two columns:   email    |    logins    Basically it's a table of login records for each account (email) between 2018-10-17 and 2019-01-16.   ...
  • AlB's avatar
    AlB
    7 years ago

    Hi markus_zhang

     

     Unless I'm missing something, what your code does in a slightly convoluted manner is count how many users (emails) have logged in on the date selected in the slicer or any of the previous [ActiveThreshold] days. If so, you can simplify the code a whole lot:

     

    Measure =
    IF (
        ISFILTERED ( WeekMatrix[Date] ),
        VAR _SelectedDate = SELECTEDVALUE ( WeekMatrix[Date] )
        RETURN
            CALCULATE (
                DISTINCTCOUNT ( NTKLogin[email] ),
                NTKLogin[logins] <= _SelectedDate, NTKLogin[logins] >= _SelectedDate - [ActiveThreshold]
            ),
        ERROR ( "This measure can only be evaluated under the filter context of WeekMatrix[Date]" )
    )