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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
cham
Post Patron
Post Patron

Difference between previous week data

Hi ,

 

I used following measures to calculate difference between previous week data,

 

table = Calendar (dateStart;dateEnd)

 

Measure = calculate(sum('Sheet1'[Total]),
filter('Table', 'Table'[Column] = WEEKNUM(max('Sheet1'[Date]),1)))
 
Measure 2 = calculate(sum('Sheet1'[Total]),
filter('Table', 'Table'[Column] = WEEKNUM(max('Sheet1'[Date]),1)-1))
 
Measure 3 = [Measure]/[Measure 2] - 1
 
But it didnt work, Following are my results,
week.PNG
 
Regards,
Cham
8 REPLIES 8
Anonymous
Not applicable

@cham  - You can use a variable in your "previous week" measure:

Measure 2 = 
var prev_week_num = WEEKNUM(max('Sheet1'[Date]),1)-1
calculate(
    sum('Sheet1'[Total]),
    'Table'[Column] = prev_week_num 
)

Hope this helps,

Nathan

Hi @Anonymous 

 

Do I need to only use this measure to calculate the difference between previous week data?

 

Regards,

Cham

Anonymous
Not applicable

@cham - No, I was substituting the measure 2 with the one I mentioned.

Also, @parry2k gave good advice about adding a date table to your model. That table can include WeekNumber and it would be best to also have a "RelativeWeek" or "WeekKey" which is what you would use to calculate the previous week. If you need assistance with that, let us know.

The solution I provided will not actually work when the weeks go across years.

Hi @Anonymous 

 

Yes I alredy create a new calender in my sheet and i make a relationship between calender table and my sheet date. I want to know what is wrong with my measures which i show in earlier. Can you please explain it to me?

Anonymous
Not applicable

@cham - Here are some issues:

1. The visual you show appears to use the date from Sheet1. You will want to use the date from the date table in any visual.

2. Consider the following measure:

 

Measure = calculate(
    sum('Sheet1'[Total]),
    filter(
        'Table', 
       'Table'[Column] = WEEKNUM(max('Sheet1'[Date]),1)
    )
)

I think you are trying to find the week number of the max date in Sheet1.

 

1.a. One issue is that WEEKNUM returns a number, but it is being used to filter a date column (I think).

1.b. Another problem is WEEKNUM function will not work when crossing over years.

The measure instead could be like this:

 

Measure = 
--var max_date = CALCULATE(max('Sheet1'[Date]), ALL('Date')) --If you need to only find the most current date in the fact table, you can use these 2 lines.
--var week_num = CALCULATE(MAX('Date'[WeekNumCumulative]), 'Date'[Date] = max_date)
var week_num = MAX('Date'[WeekNumCumulative]) --Find the maximum week number associated with the relevant date. return CALCULATE( SUM('Sheet1'[Total]), 'Date'[WeekNumCumulative] = week_num )

This, of course, requires you to have a Date table that includes a column WeekNumCumulative, that doesn't reset every year. This could be done by calculating the number of weeks between some start date and the date for the row in the date table. Here is an example of a Calculated Column in DAX:

 

WeekNumCumulative = FLOOR(DATEDIFF(date(2017,1,1),[Date],DAY) / 7,1)

The previous week measure would be almost identical to the current week, except:

    Date'[WeekNumCumulative] = week_num - 1

Hope this helps,

Nathan

 

 

 

 

 

Hi @Anonymous 

 

I tried the mesaure you mentioned but it did not work. 

 

Here is my data set,

Date SpendPay
22-07-19589.32425.22
24-07-193652.851833.72
21-07-193659.52744.64
24-07-192312.591726.67
24-07-19519.59347.25
24-07-1921178.4817955.15
24-07-193016.453734.56
22-07-192130.751562.65
31-07-19107.5268.16
31-07-196545.5
31-07-191430.741078.24
31-07-19108.315534.32
01-08-191011.2310910.91

 

I created a mesaure using this data set as below,

%Amount = (SUM('Sheet1'[Spend]) - SUM('Sheet1'[Total Pay])) / SUM('Sheet1'[Spend])

 

I want to get the diffrence between previous week %Amoun?with this week. How can i do that?Hope this will help you because i provide the data set.

Anonymous
Not applicable

@cham - 

To compare 2 different time periods, you need to "modify the filter context". The way to do this is with CALCULATE function, like this:

%Amount vs Previous Week = 
var prev_week_num = MAX('Date'[WeekNumCumulative]) - 1 --Find the maximum week number associated with the relevant date and subtract one to get the previous week.
var prev_week_value = CALCULATE(
[%Amount],
'Date'[WeekNumCumulative] = week_num
)
return [%Amount] - prev_week_value
I hope this helps. If it does, please Mark as a solution.
I also appreciate Kudos.
Nathan Peterson

@cham I know @Anonymous solution will work but one should always add Datetable in your model and work thru that for all time intelligence related reporting.

 

There are many way to add date table in your table either using DAX or M



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.