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
cham
Post Patron
Post Patron

DAX Formula to calculate difference between previous week data

Hi,

 

Can anyone please tell me is following DAX formula is correct or wrong,

 

Previous Week = IF(ISBLANK([%GP]), BLANK(),
VAR CurrentWeek = SELECTEDVALUE('Calendar'[WeekNum])
VAR CurrentYear = SELECTEDVALUE('Calendar'[Year])
VAR MaxWeekNumber = CALCULATE(MAX('Calendar'[WeekNum]), ALL('Calendar'))

RETURN SUMX(FILTER(ALL('Calendar'),
IF(CurrentWeek = 1,
'Calendar'[WeekNum] = MaxWeekNumber && 'Calendar'[Year] = CurrentYear - 1,
'Calendar'[WeekNum] = CurrentWeek - 1 && 'Calendar'[Year] = CurrentYear)),
[%GP]))
 
%GP = (SUM(spend) - sum(pay)) / sum(spend)
%GP is in percentage format. Do above DAX formula giving the correct answer for difference between previous %GP data.
 
Please anyone can help me it would be great. I want to get the difference between previous week data. Can anyone giv me the answer to crete that measure.
 
Regards,
C
7 REPLIES 7
v-xicai
Community Support
Community Support

Hi @cham  ,

 

Does that make sense? If so, kindly mark my answer as a solution to help others having the similar issue and close the case. If not, let me know and I'll try to help you further.

 

Best regards

Amy

Hi @v-xicai ,

 

Its giving me the same error,

 

E.PNG

v-xicai
Community Support
Community Support

Hi @cham ,

 

Please check your formula carefully, the red parts in following places could be same. The second one refers to the first one.25.png

 

Best Regards,

Amy

Hi @v-xicai 

 

I follow the same measure as you written. But Its giving me an error.

 

Regards,

Cham

v-xicai
Community Support
Community Support

Hi @cham ,

 

You may try to create measures like DAX below.

 

MaxWeekNumber = CALCULATE(MAX('Calendar'[WeekNum]), FILTER(ALL('Calendar'), 'Calendar'[Year]=MAX('Calendar'[Year)))

Previous Week =
VAR CurrentWeek = SELECTEDVALUE('Calendar'[WeekNum])
VAR CurrentYear = SELECTEDVALUE('Calendar'[Year])
VAR Filter=IF(CurrentWeek = 1, FILTER(ALL('Calendar'), 'Calendar'[WeekNum] =MAXX('Calendar', [MaxWeekNumber] ) && 'Calendar'[Year] = CurrentYear - 1), FILTER(ALL('Calendar'), 'Calendar'[WeekNum] =CurrentWeek - 1 && 'Calendar'[Year] = CurrentYear))
RETURN
IF(ISBLANK([%GP]), BLANK(), SUMX(Filter, [%GP]))

If I misunderstood it, could you please share your sample data and desired output screenshots for further analysis? You can also upload sample pbix to OneDrive and post the link here. Do mask sensitive data before uploading.

 

Best Regards,

Amy

 

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

 

Hi @v-xicai 

 

Please find the excel sheet,

Date SpendPay%GPSum %GPDifference between previous week %GP 
13-07-19125623.8818910.999973913  
14-07-192328891.88167210.999974547  
15-07-191231292.110110.9999801542.999928614-
22-07-19112589.32425.220.999980486  
24-07-191233652.851833.720.999980629  
21-07-19122333659.52744.640.999981238  
24-07-196252312.591726.670.999981396  
24-07-197282519.59347.250.999981963  
24-07-19282822117817955.150.999982039  
24-07-192723016.453734.560.99356672  
22-07-19232130.751562.650.9878149117.981269382-4.981340768
31-07-19234107.5268.160.987229998  
31-07-194556545.50.985716413  
31-07-19551430.741078.240.985395205  
31-07-19222108.315534.320.9801628973.9385045124.04276487
01-08-191111011.2310910.910.9901792980.9901792982.948325214


I want to get the difference between previous week %GP as I shown in Excel.

The measure is giving me an error,

p.PNG

v-xicai
Community Support
Community Support

Hi @cham ,

 

You may change the red part to "_filter", and try it again.

 

24.png

 

Best Regards,

Amy

 

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.

Top Solution Authors
Top Kudoed Authors