Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

DAX measure not working

I am working in Tabular Editor 3, and trying to create a measure in DAX which will allow me to show data in a calculation group where according to the Year selected in a filter the table will show data for that year and the previous year, dynamically changing as the filter selection changes.  I have a working measure for the current year, but am unable to work out why it doesn't work when I try to use "Year - 1" within the calculation.

 

I have a measure which works to identify the Year selected in the filter:

YearOfEntrySelected = SELECTEDVALUE(LookupEntryYear[YearOfEntry])

 

The measure I am using works as long as I don't use - 1:

VAR YearSelected = [YearOfEntrySelected]

 

RETURN

IF(

    SELECTEDVALUE( DataCourseApplications[LatestYearOfEntry] ) = YearSelected,

    CALCULATE(

        COUNTROWS( DataCourseApplications )

        , DataCourseApplications[LatestRecord] <> BLANK()

    )

)

 

But does not work when I add the - 1 to the variable (or directly to the Measure and reference that in the IF statement):

VAR YearSelected = [YearOfEntrySelected] - 1

RETURN

IF(

    SELECTEDVALUE( DataCourseApplications[LatestYearOfEntry] ) = YearSelected,

    CALCULATE(

        COUNTROWS( DataCourseApplications )

        , DataCourseApplications[LatestRecord] <> BLANK()

    )

)

 

The Year values all look to be formatted as Whole Numbers, so I don't believe that there should be any issue with the minus 1 and it matching to the LatestYearOfEntry.

 

Any thoughts  and ideas welcome - I've asked a number of colleagues and we are all perplexed!

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous ,

    Please have a try.

    To avoid this problem, you can use the MAX or MIN function instead of SELECTEDVALUE, which will return the maximum or minimum value in the filter context. For example, you can use this formula:

    VAR YearSelected = MAX(LookupEntryYear[YearOfEntry]) - 1
    RETURN
    IF(
        MAX(DataCourseApplications[LatestYearOfEntry]) = YearSelected,
        CALCULATE(
            COUNTROWS(DataCourseApplications),
            DataCourseApplications[LatestRecord] <> BLANK()
        )
    )

     

     

    How to Get Your Question Answered Quickly 

     

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

     

    Best Regards
    Community Support Team _ Rongtie

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

     

2 Replies

  • What's the relevance of calculation groups here?  Isn't that something a regular measure can do?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    Please have a try.

    To avoid this problem, you can use the MAX or MIN function instead of SELECTEDVALUE, which will return the maximum or minimum value in the filter context. For example, you can use this formula:

    VAR YearSelected = MAX(LookupEntryYear[YearOfEntry]) - 1
    RETURN
    IF(
        MAX(DataCourseApplications[LatestYearOfEntry]) = YearSelected,
        CALCULATE(
            COUNTROWS(DataCourseApplications),
            DataCourseApplications[LatestRecord] <> BLANK()
        )
    )

     

     

    How to Get Your Question Answered Quickly 

     

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

     

    Best Regards
    Community Support Team _ Rongtie

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.