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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
unknown917
Helper III
Helper III

Count based on variable date range and start date

I have a table with a column that supplies a start date for a reopening of any given ID.  In another table I have the current and historical operating data for the ID by date with key items like sales qty.

 

I am in need of taking the reopening date as the starting point and calculate out in 28 day (4wk) increments the sales qty in each cycle before and since the reopen date. This is to run as a comparison of how the ID has performed before and since the repopen.

 

Since every reopen date can be different, how can I accomplish the date variability with the incremental summing or counting?

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Hi @unknown917 please try this

 

create a calculated column in your sales table for the week buckets 

 

Week_Bucket =
VAR ReOpenDate = LOOKUPVALUE('reopen'[Reopen Date], 'reopen'[ID], 'sales'[ID])
VAR DaysSince = DATEDIFF(ReOpenDate, 'sales'[Date], DAY)
RETURN
SWITCH(
    TRUE(),
    DaysSince >= 0 && DaysSince < 28, "Week 1-4 After",
    DaysSince >= 28 && DaysSince < 56, "Week 5-8 After",
    DaysSince >= 56 && DaysSince < 84, "Week 9-12 After",
    DaysSince < 0 && DaysSince >= -28, "Week 1-4 Before",
    DaysSince < -28 && DaysSince >= -56, "Week 5-8 Before",
    DaysSince < -56 && DaysSince >= -84, "Week 9-12 Before",
    "Other"
)

 

And then create the measure to sum the sales based on the buckets

 

Sales_by_Weeks =
VAR SelectedWeek = SELECTEDVALUE('SalesData'[Week_Bucket])
RETURN
CALCULATE(SUM('SalesData'[Sales Qty]), 'SalesData'[Week_Bucket] = SelectedWeek)

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

View solution in original post

6 REPLIES 6
techies
Super User
Super User

 Hi @unknown917 please share the sample pbix file if possible.

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

My apologies, @techies  - I am unable to share a file, but here is a sample of the tables I'm referring to:

 

table 1
IDReOpenDate
11/6/2025
22/17/2025
31/13/2025
411/4/2024
53/10/2025
table 2
IDInv DateSales Qty
19/9/2024150
19/12/2024175
19/16/2024205
19/19/2024234
211/13/202417
21/15/202512
32/13/2025100
32/20/2025125
33/11/2025123
43/12/20255
510/22/2024500
510/24/2024400

 

Intended result is to select the ID with a slicer and see performance after reopen:

IDwk 1-4 (28days)wk 5-8 (28days)wk 9-12 (28 days)
1   
2   
30225123
4   
5   

 

Metrics before re-open date could also be valuable, not sure if that is also possible

Hi @unknown917 please try this

 

create a calculated column in your sales table for the week buckets 

 

Week_Bucket =
VAR ReOpenDate = LOOKUPVALUE('reopen'[Reopen Date], 'reopen'[ID], 'sales'[ID])
VAR DaysSince = DATEDIFF(ReOpenDate, 'sales'[Date], DAY)
RETURN
SWITCH(
    TRUE(),
    DaysSince >= 0 && DaysSince < 28, "Week 1-4 After",
    DaysSince >= 28 && DaysSince < 56, "Week 5-8 After",
    DaysSince >= 56 && DaysSince < 84, "Week 9-12 After",
    DaysSince < 0 && DaysSince >= -28, "Week 1-4 Before",
    DaysSince < -28 && DaysSince >= -56, "Week 5-8 Before",
    DaysSince < -56 && DaysSince >= -84, "Week 9-12 Before",
    "Other"
)

 

And then create the measure to sum the sales based on the buckets

 

Sales_by_Weeks =
VAR SelectedWeek = SELECTEDVALUE('SalesData'[Week_Bucket])
RETURN
CALCULATE(SUM('SalesData'[Sales Qty]), 'SalesData'[Week_Bucket] = SelectedWeek)

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

Genius @techies !  I can make this work!  Thank you for your help!

techies
Super User
Super User

Hi @unknown917 

As I understand, create a RelativePeriod column that groups sales into 4-week periods before and after reopening by dividing the day difference by 28.

 

RelativePeriod =
VAR ReopenDate = LOOKUPVALUE(reopen[Reopen Date], reopen[ID], sales[ID])
VAR DaysDifference = DATEDIFF(ReopenDate, sales[Date], DAY)
RETURN INT(DaysDifference / 28)

 

And the Sales_4WeekPeriod measure then sums sales for each period, allowing performance comparison over time.

 

Sales_4WeekPeriod =
SUMX(
    VALUES(sales[RelativePeriod]),
    CALCULATE(
        SUM(sales[Sales Qty])
    )
)
 
Hope this works.
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

Thanks, @techies  - 

 

this resulted in returning all sales for the entire date range in 2nd table.  I need to be able to represent the 4 week increments, based on the go -live date

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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