Forum Discussion

cham's avatar
cham
Post Patron
7 years ago

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

  • v-xicai's avatar
    v-xicai
    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.

     

    • cham's avatar
      cham
      Post Patron

      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,

      • v-xicai's avatar
        v-xicai
        Community Support

        Hi cham ,

         

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

         

         

        Best Regards,

        Amy

         

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

  • v-xicai's avatar
    v-xicai
    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

      • v-xicai's avatar
        v-xicai
        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.

         

        Best Regards,

        Amy