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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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