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
ryan_b_fiting
Post Patron
Post Patron

Date Grouping based on MOST RECENT dates

Hello Community - 

I am looking to create a 4 week grouping of data that is based off of the most recent date.  By that I mean, I am looking to have 4 week groupings, but with the most recent 'group' ALWAYS having 4 weeks in it.  So if you take a look at the sample of data below the first 4 periods (4/1/20 thru 4/22/20) would be in a 4 week ended bucket of 4/22/20.  However, when I get new data for the week ended 4/29/20, I would want the weeks of 4/8/20 thru 4/29/20 to now be groups as a 4 Week ended 4/29/20. (Obviously as a single column that changes whenever data is added).

 

I know I can create groups/bins but those based off of the earliest date and when new data is added, it is sometimes only 1, 2 or 3 weeks worth of data because of the way the bins are calculated from the earliest date in the calendar table.

 

PeriodBrandSales4 Week EndedNew Data 4 Wk Ended 
4/1/20ABC12344/22/204/1/20 
4/8/20ABC23454/22/204/29/20 
4/15/20ABC34554/22/204/29/20 
4/22/20ABC45674/22/204/29/20 
      
4/29/20ABC4563 4/29/20 
      

 

Does anyone in the community know how to create this type of binning.  One that is dynamic and is always creating the 4 week bin based on the most recent date?

 

Thanks in advance for any help you can provide!

Ryan

1 ACCEPTED SOLUTION

@ryan_b_fiting,

 

Try this calculated column:

 

4 Weeks Ended = 
VAR vMaxDate =
    MAX ( Sales[Period] )
VAR vDaysDiff =
    CONVERT ( vMaxDate - Sales[Period], INTEGER )
VAR vDelta =
    ROUNDDOWN ( DIVIDE ( vDaysDiff, 28 ), 0 )
VAR vResult = vMaxDate - ( vDelta * 28 )
RETURN
    vResult

 

DataInsights_0-1603907028206.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

8 REPLIES 8
v-jayw-msft
Community Support
Community Support

Hi @ryan_b_fiting ,

 

Has the problem be solved? Please consider to mark the answer as solution if it works for you.

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

@v-jayw-msft no this has not been solved.  I have tried all the suggestions and a few others as well, but no luck yet.

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula seems to work

=CALCULATE(MAX(Table1[Period]),FILTER(Table1,Table1[Brand]=EARLIER(Table1[Brand])&&Table1[Period]<=EARLIER(Table1[Period])+21))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@ryan_b_fiting,

 

Try this calculated column:

 

4 Weeks Ended = 
VAR vMaxDate =
    MAX ( Sales[Period] )
VAR vDaysDiff =
    CONVERT ( vMaxDate - Sales[Period], INTEGER )
VAR vDelta =
    ROUNDDOWN ( DIVIDE ( vDaysDiff, 28 ), 0 )
VAR vResult = vMaxDate - ( vDelta * 28 )
RETURN
    vResult

 

DataInsights_0-1603907028206.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@DataInsights this one does work for almost everything I need.  The only time it will not work is if we are selecting a specified date range that does not include the most recent period of data.  For example if we have data through November 1, 2020.  But I wanted to look to see what my numbers were for 2019 through the same week number in November, this will not dynamically adjust the groupings based on that.

Other than that, this solves everything I asked for, so I am marking it as the solution.

 

Thanks a ton for your help @DataInsights 

@ryan_b_fiting,

 

Try this measure. It requires the following:

 

1. Slicer based on the Date table.

2. A relationship between the Sales and Date tables.

 

4 Weeks Ended Measure = 
VAR vMaxDate =
    LASTDATE ( ALLSELECTED ( 'Date'[Date] ) )
VAR vCurrentDate =
    MAX ( Sales[Period] )
VAR vDaysDiff =
    CONVERT ( vMaxDate - vCurrentDate, INTEGER )
VAR vDelta =
    ROUNDDOWN ( DIVIDE ( vDaysDiff, 28 ), 0 )
VAR vResult = vMaxDate - ( vDelta * 28 )
RETURN
    IF ( ISBLANK ( vCurrentDate ), BLANK (), vResult )

 

DataInsights_0-1605021867146.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




DataInsights
Super User
Super User

@ryan_b_fiting,

 

Try this measure:

 

4 Weeks Ended = 
VAR vMaxDate =
    CALCULATE ( MAX ( Sales[Period] ), ALL ( Sales ) )
VAR vStartDate =
    vMaxDate - 28 --subtract 4 weeks
VAR vRowDate =
    MAX ( Sales[Period] )
VAR vResult =
    IF ( vRowDate > vStartDate, vMaxDate, vRowDate )
RETURN
    vResult

 

DataInsights_1-1603489667485.png

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@DataInsights this does give me the the most recent grouping of 4 weeks it appears, but after that it is each individual date.  The groupings would need to remain in 4 week increments, just by dynamically changing as new data is added for the week.

 

So with the example I provided, when I add 3 more weeks worth of data I would expect to see this:

BrandPeriodSales4 Weeks Ended
ABC4/1/2012344/22/20
ABC

4/8/20

23454/22/20
ABC4/15/2034554/22/20
ABC4/22/2045674/22/20
ABC4/29/2045635/20/20
ABC5/6/2076545/20/20
ABC5/13/2045675/20/20
ABC5/20/2088755/20/20

 

So it always needs to be a rolling 4 week grouping.  We are usually using a date range of the last 1-2 years.

 

I think a few of the variables need to get adjusted, but I cannot figure out to what!

If you have any other ideas on how to do this, I am all ears!  I have been trying to figure this out for a while now.

 

Thanks! 

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.