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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Anonymous
Not applicable

Last week's sales by product

Hi Community! 

 

I'm aiming to calculate last week's sales for each product. 

 

My primary sales table has four column: 

Date

Product ID

Date

Quantity Sold

 

I have tried the following formula, but it's yielding an aggregate result that does not disaggregate by product. 

 

CALCULATE(SUMX(Sales,Sales[amount]),
FILTER(ALL('Calendar'),
'Calendar'[week_num] = SELECTEDVALUE('Calendar'[week_num]) - 1 &&
'Calendar'[year] = SELECTEDVALUE('Calendar'[year])))
 
Any help to get the disaggregated results by product would be super helpful! 

 

1 ACCEPTED SOLUTION

Try

 

Prev Week =
var _year =MAX ( 'Calendar','Calendar'[year] )
var _date = date(_year-1,12,31)
var _maxPweek = weekno(_date)
VAR _maxWeek1 = MAXX('Calendar', 'Calendar'[week_num] )
VAR _maxWeek = if([_maxWeek1]=1 ,_maxPweek, _maxWeek1-1
var _cYear = MAXX ( 'Calendar','Calendar'[year] )
Var _maxYear = if([_maxWeek1]=1,_cYear-1,_cYear)
return 
CALCULATE (
SUM ( Sales[amount] ),
FILTER ( all('Calendar'),'Calendar'[year] = _maxYear && 'Calendar'[week_num] = _maxWeek),
)


This Week =
VAR _maxWeek = MAXX('Calendar', 'Calendar'[week_num] )
var _maxYear = MAXX ( 'Calendar','Calendar'[year] )
return 
CALCULATE (
SUM ( Sales[amount] ),
FILTER ( all('Calendar'),'Calendar'[year] = _maxYear && 'Calendar'[week_num] = _maxWeek),
)
Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

4 REPLIES 4
menoguera21
Regular Visitor

Hello Community,

 

I would like to ask for help regarding my concern. I am trying to get the total of each product type through measure and comparing it from last week sold and this week sold but I can only get the total sold of all products when I am filtering my product type. Is there a way to get the total on each product type? Please see below my measures.

 

Sold Last Week = CALCULATE(SUM('Product Database'[Total Sold]),FILTER(ALL('Product Database'),'Product Database'[Week Rank]=MAX('Product Database'[Week Rank])-1))

 

Sold This Week = CALCULATE(SUM('Product Database'[Total Sold]),FILTER(ALL('Product Database'),'Product Database'[Week Rank]=MAX('Product Database'[Week Rank])))

 

Sold Weekly by % = ([Sold This Week]-[Sold Last Week])/'Product Database'[Sold Last Week]

 

Thank you for your help on this matter.

 

 

amitchandak
Super User
Super User

Create a rank on the week start/end date of week no (YYYYWW) and then use that. Date is my Calendar table here

 

Week Start date = DATEADD('Date'[Date],-1*WEEKDAY('Date'[Date])+1,DAY)
Week End date = DATEADD('Date'[Date],7-1*WEEKDAY('Date'[Date]),DAY)
Week Num = if('Date'[Week Number] <10 ,'Date'[Year]*10 & 'Date'[Week Number], FORMAT('Date'[Date],"YYYYWW") )

Week Rank = RANKX('Date','Date'[Week Start date],,ASC,Dense)

Measure = CALCULATE([AH], all('Date'),filter('Date','Date'[Week Rank]=max('Date'[Week Rank])-1))

this Week using rank = CALCULATE(sum(Sales[Amount]),all('Date'),filter('Date','Date'[Week Rank]=max('Date'[Week Rank])))

Last Week using rank = CALCULATE(sum(Sales[Amount]),all('Date'),filter('Date','Date'[Week Rank]=max('Date'[Week Rank])-1))

 

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
Winner-Topper-on-Map-How-to-Color-States-on-a-Map-with-Winners , HR-Analytics-Active-Employee-Hire-and-Termination-trend
Power-BI-Working-with-Non-Standard-Time-Periods And Comparing-Data-Across-Date-Ranges

Connect on Linkedin

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

Hi! Thank you for the response, but I'm looking for a simpler solution. Right now, the following formula works but only with one of the conditions (currYear or currWeek), not both. Do you know what I may be doing wrong here?

 

Diff =
VAR currYear = MAX ( 'Calendar'[year] )
VAR currWeek = MAX( 'Calendar'[week_num] )
RETURN
CALCULATE (
SUM ( Sales[amount] ),
FILTER ( ALLSELECTED('Calendar'),'Calendar'[year] = currYear && 'Calendar'[week_num] = currWeek),
VALUES ( Sales[product_id] )
)

Try

 

Prev Week =
var _year =MAX ( 'Calendar','Calendar'[year] )
var _date = date(_year-1,12,31)
var _maxPweek = weekno(_date)
VAR _maxWeek1 = MAXX('Calendar', 'Calendar'[week_num] )
VAR _maxWeek = if([_maxWeek1]=1 ,_maxPweek, _maxWeek1-1
var _cYear = MAXX ( 'Calendar','Calendar'[year] )
Var _maxYear = if([_maxWeek1]=1,_cYear-1,_cYear)
return 
CALCULATE (
SUM ( Sales[amount] ),
FILTER ( all('Calendar'),'Calendar'[year] = _maxYear && 'Calendar'[week_num] = _maxWeek),
)


This Week =
VAR _maxWeek = MAXX('Calendar', 'Calendar'[week_num] )
var _maxYear = MAXX ( 'Calendar','Calendar'[year] )
return 
CALCULATE (
SUM ( Sales[amount] ),
FILTER ( all('Calendar'),'Calendar'[year] = _maxYear && 'Calendar'[week_num] = _maxWeek),
)
Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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