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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
bhickok
Frequent Visitor

How to calculate avg price for the same period last year for sum totals in Dax

I have a table of Invoices that are for different products over multiple years. I am attempting to get the previous years avg price for each item by dividing the Calculate(Sum(sales)/Sum(qtyShipped),SamePeriodLastYear(DateDim[Date]))
When I do this calculation I get NaN returned. If I change the SamePeriodLastYear to ParallelPeriod(DateDim[Date],-12,Month) I get a 0 value. 

In Power BI i'm building the Matrix by 

Rows: 

Item

Month

Columns:

Year

Values:

Qty Shipped

Total Sales

Avg Price (Totalsales/qtyshipped)

Previous Years avg Price (I want this to be the samePeriodlastyear of the totalSales/Qtyshipped)

 

There is one more hiccup in here. If the previous year has a 0 value for the avg price I want it to use the current year as the pervious years avg price.  This is an example of the table I am attempting to return in powerbi except with a column that will in the previous years avg price. These number are not correct it is just a dummy data table. 

bhickok_0-1682367046088.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @bhickok ,

 

You need to divide previous year's sales by previous year's qtyshipped in the formula.

Here's an example.

_presale returns the previous year's sales.

Measure = var _presale=CALCULATE(SUM('Table'[sale]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
return _presale

vstephenmsft_2-1682478105368.png

_preqty returns the previous year's qryshipped.

Measure = var _presale=CALCULATE(SUM('Table'[sale]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
var _preqty=CALCULATE(SUM('Table'[qtyshipped]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
return _preqty

vstephenmsft_3-1682478239821.png

 

Finally, divide the two.

Measure = var _presale=CALCULATE(SUM('Table'[sale]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
var _preqty=CALCULATE(SUM('Table'[qtyshipped]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
return DIVIDE(_presale,_preqty)

vstephenmsft_4-1682478256595.png

Hope the above example helps you.

 

 

Best Regards,

Stephen Tao

 

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
Anonymous
Not applicable

Hi @bhickok ,

 

You need to divide previous year's sales by previous year's qtyshipped in the formula.

Here's an example.

_presale returns the previous year's sales.

Measure = var _presale=CALCULATE(SUM('Table'[sale]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
return _presale

vstephenmsft_2-1682478105368.png

_preqty returns the previous year's qryshipped.

Measure = var _presale=CALCULATE(SUM('Table'[sale]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
var _preqty=CALCULATE(SUM('Table'[qtyshipped]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
return _preqty

vstephenmsft_3-1682478239821.png

 

Finally, divide the two.

Measure = var _presale=CALCULATE(SUM('Table'[sale]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
var _preqty=CALCULATE(SUM('Table'[qtyshipped]),FILTER(ALLSELECTED('Table'),[item number]=MAX('Table'[item number])&&[Month]=MAX('Table'[Month])&&[Year]=MAX('Table'[Year])-1))
return DIVIDE(_presale,_preqty)

vstephenmsft_4-1682478256595.png

Hope the above example helps you.

 

 

Best Regards,

Stephen Tao

 

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

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.