Forum Discussion

abhishekdas72's avatar
abhishekdas72
Frequent Visitor
3 years ago

week over week calculation

Hello Power BI Community,

I need to calculate week over week variance from a source table in SQL DB . Please help me out with the below situation

there is a heirarchy which goes like this

Region > Trade > Manager > Supplier > Part

 

regionTrademanagersupplierpartspend
R2T3M3S2P5               1,051,229
R2T3M1S2P7               1,669,068
R2T3M1S4P3               6,473,159
R2T1M2S1P1               3,540,942
R2T3M3S4P1               9,691,689
R2T2M3S3P2               3,726,536
R2T2M1S3P8               9,836,488

 

the heirarchical structure looks like this

 

What happens is each week the actuals are posted in same source table and i need to figure out a way to retain figures for the previous week only so that i can subtract from current week actuals and check which region > trade > manager > supplier >part has shown the high variance. 

variance = current week actuals - previous week actuals 

If suppose a part actuals has not been posted or there is no weekly history it should be added to the list under the same herirachy region > trade > manager > supplier >part.

i have connected to the SQL databse using import query method actuals are posted to the same table from which i am calculating the values .can this be done in power BI or this need something to be done at source level while importing the data or in SQL database( this is not an option as admin only grant generic READ only acess to the DB).  
DimaMD amitchandak olgad DimaD  Please help

4 Replies

    • DimaMD's avatar
      DimaMD
      Solution Sage

      abhishekdas72 Provide more detailed sample data, or attach a pbix file, without confidential data

  • DimaMD's avatar
    DimaMD
    Solution Sage

    abhishekdas72 I have created an example, see the attached file and try to implement it in your report.
    first measure (example)

    1 - 
    spend PW = 
     VAR CurrentWeek = SELECTEDVALUE(dates[Weekmun])
     VAR currentYear = SELECTEDVALUE(dates[Year])
     VAR maxWeeknum = CALCULATE( MAX( 'dates'[Weekmun]), ALL( dates))
     return
     SUMX( 
         FILTER( ALL(dates),
         IF( CurrentWeek = 1, 
         'dates'[Weekmun] = maxWeeknum && 'dates'[Year] = currentYear -1,
         'dates'[Weekmun] = CurrentWeek - 1 && 'dates'[Year] = currentYear)),
         [Total spend]
     )


     second measure (example)

    Previous week = 
     CALCULATE( [Total spend], 
     FILTER( ALL(dates) , dates[Weekmun] = SELECTEDVALUE(dates[Weekmun]) - 1 && dates[Year] = SELECTEDVALUE(dates[Year])))