Forum Discussion

ElChambre20's avatar
ElChambre20
Frequent Visitor
5 years ago
Solved

Calculate Average Using Dates from a Measure

Hi All,   Im relatively new to Power BI (only been using for 5 months).   Im dealing with data relating to site surveys, and each survey is assigned a score based on its quality.   I have been ...
  • ElChambre20's avatar
    5 years ago

    Hi AlB ,

     

    After tweaking your measure vary slightly i got it to work!

     

    AT SURVEY ACC_3 = 
    CALCULATE (
        AVERAGEX (
            FILTER (
                CalcTableSiteVisitsWithRatings,
                VAR currentYear_ =
                    CALCULATE(
                        MAX ( 'Date'[Date] ),
                        ALLSELECTED('Date')
                    )
                VAR latestInYear_ =
                    CALCULATE (
                        MAX ( CalcTableSiteVisitsWithRatings[EventDate] ),
                        CalcTableSiteVisitsWithRatings[EventDate]  <= currentYear_,
                        ALLEXCEPT (
                            CalcTableSiteVisitsWithRatings,
                            CalcTableSiteVisitsWithRatings[LocationID]
                        )
                    )
                RETURN
                    IF (
                        CalcTableSiteVisitsWithRatings[EventDate] = latestInYear_,
                        TRUE (),
                        FALSE ()
                    )
            ),
            CalcTableSiteVisitsWithRatings[AtSurveyRiskRatingReverseRank]
        ),
        FILTER ( ALL ( 'Date' ), 'Date'[Date] <= MAX ( 'Date'[Date] ) )
    )

    I changed currentYear_ to work out the last date in the year from the Date table instead of using the eventdate, then changed latestInYear to MAX the dates up to and including currentYear_

     

    Im now getting the desired result. Thankyou so much for your help, you were amazingly close considering my less than ideal description of the problem 🙂 Ive also learnt alot from playing around with your measure.