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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Calculate the average of a rolling fixed day

How can you calculate the average of a fixed day? For example, only the average of the Mondays of the year. Below I have already made a calculation in which he takes the average of the course of days. I want to get this same rhythm from only fixed days.

1.png

measure test =
CALCULATE(
AVERAGEX(FILTER(ALLSELECTED('DimDate'),
        'DimDate'[Date] <= MAX('DimDate'[Date])),
        [value sum]))
1 ACCEPTED SOLUTION
daXtreme
Solution Sage
Solution Sage

 

[Measure for Mondays] =
// This variable can be made dynamic and can be harvested
// from an independent slicer. Here, I've hard-coded the
// day name but you get the gist, I hope.
var DayName = "monday"
var CurrentlyVisibleMaxDate = MAX( 'DimDate'[Date] )
var Output = 
    CALCULATE(
        AVERAGEX(
            FILTER(
                ALLSELECTED( 'DimDate' ),
                'DimDate'[Date] <= CurrentlyVisibleMaxDate,
                // Also to test, wrap this line in KEEPFILTERS to see
                // what's gonna happen, like so:
                // KEEPFILTERS( 'DimDate'[Day Name] = DayName )
                'DimDate'[Day Name] = DayName
            ),
            [value sum]
        )
    )
return
    Output

or this one (should be faster):

[Measure for Mondays] =
// This variable can be made dynamic and can be harvested
// from an independent slicer. Here, I've hard-coded the
// day name but you get the gist, I hope.
// By the way, DAX is case-insensitive.
var DayName = "monday"
var CurrentlyVisibleMaxDate = MAX( 'DimDate'[Date] )
var Output = 
    CALCULATE(
        AVERAGEX(
            VALUES( DimDate[Date] ),
            [value sum]
        ),
        'DimDate'[Date] <= CurrentlyVisibleMaxDate,
        // Also to test, wrap this line in KEEPFILTERS to see
        // what's gonna happen, like so:
        // KEEPFILTERS( 'DimDate'[Day Name] = DayName )
        'DimDate'[Day Name] = DayName,
        ALLSELECTED( 'DimDate' )
)
return
    Output

The second example is how it should be really written. First, the simple expression, then all the filters and directives to CALCULATE. This structure ensures not only clarity but also the fastest execution.

 

View solution in original post

6 REPLIES 6
v-jianboli-msft
Community Support
Community Support

Hi @Anonymous ,

 

Approve with @daXtreme , if you want make it variable, you can change the value of 

var DayName

Here is my solution:

Based on your description, I have created a simple sample:

vjianbolimsft_1-1663147275660.png

 

Measure = AVERAGEX(FILTER(ALL(DimDate),WEEKDAY([Date],2)=1&&[Date]<=MAX('DimDate'[Date])),[value sum])

You can change the fix day by changing WEEKDAY([Date],2)=1 .

Final output:

vjianbolimsft_0-1663147240828.png

Best Regards,

Jianbo Li

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

 

 

Anonymous
Not applicable

Thank you! I've already made it variable

var DayName = MAX('DimDate week'[WeekDay])

daXtreme
Solution Sage
Solution Sage

 

[Measure for Mondays] =
// This variable can be made dynamic and can be harvested
// from an independent slicer. Here, I've hard-coded the
// day name but you get the gist, I hope.
var DayName = "monday"
var CurrentlyVisibleMaxDate = MAX( 'DimDate'[Date] )
var Output = 
    CALCULATE(
        AVERAGEX(
            FILTER(
                ALLSELECTED( 'DimDate' ),
                'DimDate'[Date] <= CurrentlyVisibleMaxDate,
                // Also to test, wrap this line in KEEPFILTERS to see
                // what's gonna happen, like so:
                // KEEPFILTERS( 'DimDate'[Day Name] = DayName )
                'DimDate'[Day Name] = DayName
            ),
            [value sum]
        )
    )
return
    Output

or this one (should be faster):

[Measure for Mondays] =
// This variable can be made dynamic and can be harvested
// from an independent slicer. Here, I've hard-coded the
// day name but you get the gist, I hope.
// By the way, DAX is case-insensitive.
var DayName = "monday"
var CurrentlyVisibleMaxDate = MAX( 'DimDate'[Date] )
var Output = 
    CALCULATE(
        AVERAGEX(
            VALUES( DimDate[Date] ),
            [value sum]
        ),
        'DimDate'[Date] <= CurrentlyVisibleMaxDate,
        // Also to test, wrap this line in KEEPFILTERS to see
        // what's gonna happen, like so:
        // KEEPFILTERS( 'DimDate'[Day Name] = DayName )
        'DimDate'[Day Name] = DayName,
        ALLSELECTED( 'DimDate' )
)
return
    Output

The second example is how it should be really written. First, the simple expression, then all the filters and directives to CALCULATE. This structure ensures not only clarity but also the fastest execution.

 

Anonymous
Not applicable

Hi Daxtreme, 

I made the [measure for monday] variable. 

var DayName = MAX('DimDate week'[WeekDay]

How can you add up the outcome of the measure [measure for mondays]? So the figures from Monday to Sunday added together. To use it in a matrix.

Thank you

amitchandak
Super User
Super User

@Anonymous , You need have weekday and year column in date table with a measure value sum in model

 

try measure like

 

CALCULATE(
AVERAGEX(ALLSELECTED('DimDate'),[value sum])
Filter( 'DimDate' , ('DimDate'[weekday]) = (MAX('DimDate'[weekday])) && ('DimDate'[year]) = (MAX('DimDate'[year])),
))

 

or

 

 

CALCULATE(
AVERAGEX(ALLSELECTED('DimDate'),[value sum])
Filter( 'DimDate' , weekday('DimDate'[Date]) = weekday(MAX('DimDate'[Date])) && year('DimDate'[Date]) = year(MAX('DimDate'[Date])),
))

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Thank you for your answer. The total correct of the answer only the measure does not apply to the records. In the rightmost column is how it should be.

 

value.png

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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