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
DM_95
Frequent Visitor

Measure to calculate previous week of selected week. No selection=previous week of current week

Hi All,
 
I need some help regarding a measure to calculate the net sales of previous week when a week in slicer 'Week' is selected e.g. when Week 46 is selected in the slicer, I want the net sales of Week 45 (It also needs to take into account when Week 1 is selected, I would need the net sales of Week 52 of previous year). When there is no selection in slicer 'Week', it needs to show the net sales of previous week of the current week. When multiple weeks are selected in the slicer, it needs to show the previous week of the earliest week. For example when weeks 3 and 4 are selected, it needs to show the net sales of week 2.
 
I have created this measure for previous month of the selected month, but I can't seem to figure it out for previous week. Any help is welcome 🙂
 
VAR _max1 = IF(ISFILTERED('Invoice Date'[MonthInYear]), MAX('Invoice Date'[DateFull]), TODAY())
VAR _max = EOMONTH(_max1,-1)
VAR _min = EOMONTH(_max1,-2) +1
VAR _1month = CALCULATE([Net Sales], DATESBETWEEN('Invoice Date'[DateFull],_min,_max))
VAR _max3 = IF(ISFILTERED('Invoice Date'[MonthInYear]), MIN('Invoice Date'[DateFull]), TODAY())
VAR _max2 = EOMONTH(_max3,-1)
VAR _min2 = EOMONTH(_max3,-2) +1
VAR _xmonths = CALCULATE([Net Sales], DATESBETWEEN('Invoice Date'[DateFull],_min2,_max2))
RETURN
IF(COUNTROWS(ALLSELECTED('Invoice Date'[MonthInYear]))=1, _1month, _xmonths)
1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @DM_95 ,

 

Based on your description I have created a simple sample:

vjianbolimsft_2-1668651650188.png

 

vjianbolimsft_1-1668651635024.png

Then create a slicer:

vjianbolimsft_3-1668653816233.png

Apply the measure:

Measure = 
var _a = SELECTCOLUMNS('Date',"Week",[Week])
var _b = MINX(_a,[Week])-1
return CALCULATE(SUM(Sales[Net Sales]),FILTER(ALL('Date'),[Week]=_b))

Final output:

vjianbolimsft_4-1668653859437.png

Best Regards,

Jianbo Li

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

1 REPLY 1
v-jianboli-msft
Community Support
Community Support

Hi @DM_95 ,

 

Based on your description I have created a simple sample:

vjianbolimsft_2-1668651650188.png

 

vjianbolimsft_1-1668651635024.png

Then create a slicer:

vjianbolimsft_3-1668653816233.png

Apply the measure:

Measure = 
var _a = SELECTCOLUMNS('Date',"Week",[Week])
var _b = MINX(_a,[Week])-1
return CALCULATE(SUM(Sales[Net Sales]),FILTER(ALL('Date'),[Week]=_b))

Final output:

vjianbolimsft_4-1668653859437.png

Best Regards,

Jianbo Li

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

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.