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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

sum weekly total of a page, then compare difference between weeks

HI all. I'm trying to compare weekly total of different pages and not able to figure a way to 1) calculate weekly sum 2) setup filter to compare week vs. week.
Hope can get insight from expert here. Thanks a lot.

 

Raw data:

DateweekNumPageviewNum
2020 Apr 1week13A10
2020 Apr 2week13A10
2020 Apr 8week14A50
2020 Apr 15week15A2
2020 Apr 20week16A100
2020 Apr 1week13B20
2020 Apr 2week13B50
2020 Apr 8week14B10
2020 Apr 15week15B6
2020 Apr 20week16B7
2020 Apr 19week16B13

 

 

Output table:

select week 14 vs 13. 

A30       (=50-20)
B-60      (=10-70)

 

select week 16 vs 13. 

A80       (=100-20)
B-50      (=20-70)

 

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

1.Create a new table based on your weeknum as slicer.

Table 2 = DISTINCT('Table'[weekNum])

2.Sort weeknum column

Rank = RANKX(FILTER('Table',EARLIER('Table'[Page])='Table'[Page]),RIGHT('Table'[weekNum],2),,ASC)

3.Calculate the total viewnumber based on the slicer value

sum_viewnumber =
IF (
    MAX ( 'Table'[weekNum] ) IN VALUES ( 'Table 2'[weekNum] ),
    CALCULATE (
        SUM ( 'Table'[viewNum] ),
        ALLEXCEPT ( 'Table', 'Table'[Page], 'Table'[weekNum] )
    )
)

4.Get the max and min weeks of the selected weeknum according to the rank column and set the filter conditions(max_rank,min_rank).

Calculate the total viewnumber of the max and min weeks by page grouping(max_week,min_week)

Measure = 
var min_rank = CALCULATE(MIN('Table'[Rank]),FILTER(ALL('Table'),NOT(ISBLANK([sum_viewnumber]))))
var max_rank = CALCULATE(MAX('Table'[Rank]),FILTER(ALL('Table'),NOT(ISBLANK([sum_viewnumber]))))
var max_week = CALCULATE([sum_viewnumber],FILTER(ALLEXCEPT('Table','Table'[Page]),'Table'[Rank]=max_rank))
var min_week = CALCULATE([sum_viewnumber],FILTER(ALLEXCEPT('Table','Table'[Page]),'Table'[Rank]=min_rank))
return IF([sum_viewnumber]=min_week,0,max_week-min_week)

Again,you can refer to the pbix for more details.

 

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

View solution in original post

8 REPLIES 8
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

Based on your description, I first created a calculation column to sort the weeks, and then I created two measures, one to calculate the value of the selected week and use as a visual level filter, and the other to compare the week with the week.

test_compare difference between weeks.PNG

Sample .pbix

 

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

Anonymous
Not applicable

HI Liang, your solution is exactly what I want.
But I tried to replicate your formula on my actual value, the week total is working fine, but the week difference is not working.

 

I try to further simpify my question.
What if I have page + date + pageview below, I want to calulate difference based on 2 date picked. How to come up with difference?

PageDatePageViewDifference
A2019 Mar 215- or 0 or 15 is fine.
A2019 Mar 3205

 

Hi @Anonymous ,

 

Change the return value to this.

return IF([Measure 2]=min_week,0,max_week-min_week)

test_difference2.PNG

Sample .pbix has been updated.

 

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

Anonymous
Not applicable

Liang, I can hardly get what's doing here. Would you mind sharing a bit more what each line doing.
 
Measure =
var min_rank = CALCULATE(MIN('Table'[Column]),FILTER(ALL('Table'),NOT(ISBLANK([Measure 2]))))
var max_rank = CALCULATE(MAX('Table'[Column]),FILTER(ALL('Table'),NOT(ISBLANK([Measure 2]))))
var max_week = CALCULATE([Measure 2],FILTER(ALLEXCEPT('Table','Table'[Page]),'Table'[Column]=max_rank))
var min_week = CALCULATE([Measure 2],FILTER(ALLEXCEPT('Table','Table'[Page]),'Table'[Column]=min_rank))
return IF([Measure 2]=min_week,0,max_week-min_week)

----------
would be great if you can guide me how to calculate the diff between 2 dates, i.e. how to get the difference column.
PageDatePageViewDifference
A2019 Mar 215- or 0 or 15 is fine.
A2019 Mar 3205

Hi @Anonymous ,

 

1.Create a new table based on your weeknum as slicer.

Table 2 = DISTINCT('Table'[weekNum])

2.Sort weeknum column

Rank = RANKX(FILTER('Table',EARLIER('Table'[Page])='Table'[Page]),RIGHT('Table'[weekNum],2),,ASC)

3.Calculate the total viewnumber based on the slicer value

sum_viewnumber =
IF (
    MAX ( 'Table'[weekNum] ) IN VALUES ( 'Table 2'[weekNum] ),
    CALCULATE (
        SUM ( 'Table'[viewNum] ),
        ALLEXCEPT ( 'Table', 'Table'[Page], 'Table'[weekNum] )
    )
)

4.Get the max and min weeks of the selected weeknum according to the rank column and set the filter conditions(max_rank,min_rank).

Calculate the total viewnumber of the max and min weeks by page grouping(max_week,min_week)

Measure = 
var min_rank = CALCULATE(MIN('Table'[Rank]),FILTER(ALL('Table'),NOT(ISBLANK([sum_viewnumber]))))
var max_rank = CALCULATE(MAX('Table'[Rank]),FILTER(ALL('Table'),NOT(ISBLANK([sum_viewnumber]))))
var max_week = CALCULATE([sum_viewnumber],FILTER(ALLEXCEPT('Table','Table'[Page]),'Table'[Rank]=max_rank))
var min_week = CALCULATE([sum_viewnumber],FILTER(ALLEXCEPT('Table','Table'[Page]),'Table'[Rank]=min_rank))
return IF([sum_viewnumber]=min_week,0,max_week-min_week)

Again,you can refer to the pbix for more details.

 

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

Anonymous
Not applicable

Thanks mate.

 

I tried with 

ThisWeekTotal = CALCULATE (
SUM ('All'[viewNum]),
FILTER (
ALL ( 'All' ),
'All'[WeekNum] = MAX ( 'All'[WeekNum] )
)
)

Seems it sum up all my A & B page together.
I want to see A's weekly & B's weekly total seperately.

@Anonymous 

You should have a week in a different table.

If you add page(A or B) in visual, it should display value separately

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
amitchandak
Super User
Super User

Refer, if this can help, How I used week Rank to get current and Prior

https://www.dropbox.com/s/d9898a48e76wmvl/sales_analytics_weekWise.pbix?dl=0

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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