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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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