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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
dange187
Frequent Visitor

Calculate up to selected date, including average of last 4 weeks

Hi community,

I have a semi complex problem I cant get my head around.

I have a measure which I want to restrict the visible data based on date selection. So if I select todays date then data until today is shown in line chart but if select a date 2 months ago, it only shows up to that date. I have it working for basic measures using the method on SQLBI ( show previous 6 months ) which I've adapted for my custom calendar (based on weeks).

Problem I have is if I try to apply to a measure that includes time based calculations, they all get removed too and breaks the result.

My dax code is: 

Stock Weeks Cover TY =
VAR _maxweek = MAX(dimcal[WeekID])
VAR _minweek = MAX(dimcal[FWYear])
VAR _filtertab = FILTER(SUMMARIZE(dimcalprevious,dimcalprevious[WeekID]),dimcalprevious[WeekID]>=_minweek && dimcalprevious[WeekID]<=_maxweek)
VAR _result =
            CALCULATE(
                [Stock Weeks Cover],
                REMOVEFILTERS(dimcal),
                KEEPFILTERS(_filtertab),
                USERELATIONSHIP(dimcal[WeekID],dimcalprevious[WeekID])
            )
RETURN
    _result
 
where [Stock Weeks Cover] = DIVIDE([Stock Qty],[Sales Qty P4W AVG])
 
The [Sales Qty P4W AVG} looks at the last 4 weeks cumulative sales (using dimcal and weekid to navigate the relevant weeks) and divides by 4.
I think this is the part that is breaking. So when I remove filters for dimcal its not working this part out correctly and dividing [Stock Qty] by an incorrect number. 
Any help in keeping the selection range but also keeping the calculation filters would be helpful? Thanks.
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @dange187 ,

 

Based on the information you provided, you can follow these steps:

 

1.Add new measure.

(1)

Average =

VAR _value =

    CALCULATE (

        AVERAGE ( 'Table'[amount] ),

        FILTER (

            ALL ( 'Table' ),

            [week] <= SELECTEDVALUE ( 'Table'[week] )

                && [week]

                    >= SELECTEDVALUE ( 'Table'[week] ) - 3

        )

    )

RETURN

    IF ( SELECTEDVALUE ( 'Table'[week] ) < 4, BLANK (), _value )

 

(2)

4WeekAverage =

VAR _1 =

    MAXX ( ALLSELECTED ( 'Table 2' ), 'Table 2'[week] )

RETURN

    IF (

        MAX ( 'Table'[week] ) >= ( _1 - 3 )

            && MAX ( 'Table'[week] ) <= _1,

        [Average],

        BLANK ()

    )

 

 

Final output:

vyifanwmsft_0-1704793590506.png

 

vyifanwmsft_1-1704793590508.png

 

 

How to Get Your Question Answered Quickly - Microsoft Fabric Community

If it does not help, please provide more details with your desired out put and pbix file without privacy information.

 

Best Regards,

Ada Wang

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

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @dange187 ,

 

Based on the information you provided, you can follow these steps:

 

1.Add new measure.

(1)

Average =

VAR _value =

    CALCULATE (

        AVERAGE ( 'Table'[amount] ),

        FILTER (

            ALL ( 'Table' ),

            [week] <= SELECTEDVALUE ( 'Table'[week] )

                && [week]

                    >= SELECTEDVALUE ( 'Table'[week] ) - 3

        )

    )

RETURN

    IF ( SELECTEDVALUE ( 'Table'[week] ) < 4, BLANK (), _value )

 

(2)

4WeekAverage =

VAR _1 =

    MAXX ( ALLSELECTED ( 'Table 2' ), 'Table 2'[week] )

RETURN

    IF (

        MAX ( 'Table'[week] ) >= ( _1 - 3 )

            && MAX ( 'Table'[week] ) <= _1,

        [Average],

        BLANK ()

    )

 

 

Final output:

vyifanwmsft_0-1704793590506.png

 

vyifanwmsft_1-1704793590508.png

 

 

How to Get Your Question Answered Quickly - Microsoft Fabric Community

If it does not help, please provide more details with your desired out put and pbix file without privacy information.

 

Best Regards,

Ada Wang

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

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.