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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Weekly average when daily calculation involves lag (PREVIOUSDAY (or DATEADD('Date', -1, DAY)))

I have the following dataset:

 

datasetdataset

 

The formula for storage flow is HeldInStorage (day X) + Supply (day X) - Demand (day X) - HeldInStorage (day X - 1).

 

So for a daily measure, the following DAX code works fine:

 

 

Complete = 
VAR StorePrevDay = CALCULATE(SUM(GasBBActualFlowStorage[HeldInStorage]), DATEADD ('Date'[Date], -1, DAY))
VAR StoreDay = 
    SUMX ( 
        'GasBBActualFlowStorage',
        'GasBBActualFlowStorage'[HeldInStorage] + 'GasBBActualFlowStorage'[Supply] - 'GasBBActualFlowStorage'[Demand]
    )
VAR Result = StoreDay - StorePrevDay
RETURN Result

 

 

But when I try to get a weekly average, the following formula fails (probably because of wrong filter context, I think)

 

 

AvgStorageFlow = 
VAR LastVisibleDate = MAX ( 'Date'[Date] )
VAR NumberOfDays = 7
VAR PeriodToUse =
    FILTER (
        ALL ('Date'),
        AND (
            'Date'[Date] > LastVisibleDate - NumberOfDays,
            'Date'[Date] <= LastVisibleDate
        )
    )
VAR Result =
    CALCULATE( 
        AVERAGEX( 'Date', [Complete] ),
        PeriodToUse
    )
RETURN Result

 

 

What's the DAX code for calculating weekly average of storage flow?

 

PS: I already generated a Week End Date column in my calendar table with the following:

 

 

Week End Date = 
CALCULATE ( 
    MAX ('Date'[Date] ),
    ALLEXCEPT ('Date','Date'[Week Number],'Date'[Year] )
)

 

 

@dax 

1 ACCEPTED SOLUTION
daXtreme
Solution Sage
Solution Sage

Hi @Anonymous 

 

What exactly does it mean "the formula fails"? You have not shown any output. At first glance the formulas seem to be OK but without seeing the wrong results and the expected output it's not possible to diagnose.

View solution in original post

3 REPLIES 3
daXtreme
Solution Sage
Solution Sage

Hi @Anonymous 

 

What exactly does it mean "the formula fails"? You have not shown any output. At first glance the formulas seem to be OK but without seeing the wrong results and the expected output it's not possible to diagnose.

Anonymous
Not applicable

When I prepared the results to answer you, I noticed the results were fine.

I must have made a mistake when checking the first time.

Cheers

 

results matchresults match

SpartaBI
Community Champion
Community Champion

@Anonymous hard to say, need more info. If you want I'm available now for a quick zoom call to look together

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.