Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Thimios
Helper II
Helper II

Avg calculation filtered for existing data

Hi all,

I need to calculate the Average Sales only for the period that Sales data exist (red line).

The measure I created though expands to all dates in my Date table (blue line).

How can I fix that using DAX?

I tried DATESBETWEEN as shown, with no success.

 

Thimios_0-1691558682551.png

 

3 REPLIES 3
johnt75
Super User
Super User

Try

Sales avg all dates =
VAR FirstSale =
    CALCULATE ( MIN ( 'Sales'[Date] ), REMOVEFILTERS ( 'Dates' ) )
VAR FirstVisibleDate =
    EOMONTH ( FirstSale, -1 ) + 1
VAR LastSale =
    CALCULATE ( MAX ( 'Sales'[Date] ), REMOVEFILTERS ( 'Dates' ) )
VAR LastVisibleDate =
    EOMONTH ( FirstSale, 0 )
VAR MinDate =
    MIN ( 'Dates'[Date] )
VAR MaxDate =
    MAX ( 'Dates'[Date] )
VAR Result =
    IF (
        MinDate >= FirstVisibleDate
            && MaxDate <= LastVisibleDate,
        CALCULATE (
            AVERAGEX ( VALUES ( 'Dates'[Month & Year] ), [Sales] ),
            REMOVEFILTERS ( 'Dates' )
        )
    )
RETURN
    Result

Thank you @johnt75 ,

Suggested measure works as far as calculation is concerned, but not as expected in visual.

Thimios_0-1691576074702.png

 

Use Performance Analyzer to copy the query for the visual into DAX Studio. You can then use the Define Measure feature in DAX Studio to change the measure to return each of the intermediate variables in turn, that should identify which variable is being calculated incorrectly.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors