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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

calculate current week data using current week and previous week info in DAX

Hi,

I hope everyone's well.

I am trying to create a measure that uses previous week and current week data. The table looks like the following-

 

week_endprofitlossreference_for_1_maynet_product_margin
29/05/2022308640 339779
22/05/20225761366 340111
15/05/20226581494 340901
08/05/20226001198 341737
01/05/20226121794342335342335

 

I am trying to create a measure net_product_margin which is calculated by using last week's net_product_margin value - current week 'loss' + current week 'gain'. Here's the code that I tried but it doesn't work perfectly. This does calculate the net_product_margin value correctly for 8th May 2022 but since I am using the column reference_for_1_may it doesn't calculate the later week net_product_margin value correctly.

 

 

net_product_margin = var _lastweek = CALCULATE('All Measures'[reference_for_1_may], FILTER(ALL('Dim_Date'),'Dim_Date'[End_of_Week] = SELECTEDVALUE(Dim_Date[End_of_Week]) -7 )) RETURN _lastweek - 'All Measures'[loss] + 'All Measures'[profit]

 

 

This is how the formula looks in excel.

net_margin1.pngnet_margin2.png

 

I have the data from 1st May 2022 in my table so to have a reference I have added a reference column called reference_for_1_may that acts as reference data to calculate later week net_product_margin. I am scratching my head on this. Any help would be much appreciated.

Thank you.

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 
Here is the sample file with the solution https://www.dropbox.com/t/6o38jT13uTKnx2Fh

1.png

New Column

net_product_margin_calculated = 
VAR ReferenceValue = 342335
VAR FirstDateEver = MIN ( 'P&L'[week_end] )
RETURN
    SUMX ( 
        FILTER ( 
            'P&L', 
            'P&L'[week_end] <= EARLIER ( 'P&L'[week_end] ) 
                && 'P&L'[week_end] > FirstDateEver
        ), 
        'P&L'[profit] - 'P&L'[loss]
    ) 
    + ReferenceValue

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @tamerj1 

Thank you so much for the response. I am trying to replicate this but getting an error with EARLIER function. error.JPG

 

@Anonymous 

The solution is based on a calculated column not a measure 

tamerj1
Super User
Super User

Hi @Anonymous 
Here is the sample file with the solution https://www.dropbox.com/t/6o38jT13uTKnx2Fh

1.png

New Column

net_product_margin_calculated = 
VAR ReferenceValue = 342335
VAR FirstDateEver = MIN ( 'P&L'[week_end] )
RETURN
    SUMX ( 
        FILTER ( 
            'P&L', 
            'P&L'[week_end] <= EARLIER ( 'P&L'[week_end] ) 
                && 'P&L'[week_end] > FirstDateEver
        ), 
        'P&L'[profit] - 'P&L'[loss]
    ) 
    + ReferenceValue

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.