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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
mgd14work
New Member

Need a working DAX on counting weeks

I am trying to figure out how to count the number of weeks where the price (TotalSales/TotalUnit) is below the average overall in respect to its year and banner. 

 

This is what I have and it is giving me blanks as an answer.

 

Promoted Week Count =
VAR TotalSales = CALCULATE(SUM('RSI Acct Merge'[Total Sales Amount]))
VAR TotalUnit = CALCULATE(SUM('RSI Acct Merge'[Total Sales Volume Units]))
VAR Avgprice = DIVIDE(TotalSales, TotalUnit)
VAR OverallAvgPrice = calculate(divide(sum('RSI Acct Merge'[Total Sales Amount]),sum('RSI Acct Merge'[Total Sales Volume Units])),ALLSELECTED('RSI Acct Merge'),values(AdWks[Year]),values('RSI Acct Merge'[BANNER NAME]))
RETURN
    CALCULATE(countrows(AdWks),filter(AdWks,Avgprice<OverallAvgPrice))
1 ACCEPTED SOLUTION
dharmendars007
Super User
Super User

Hello @mgd14work , 

 

Make sure that TotalUnit is not zero, as dividing by zero would return a blank result. You can safeguard this by adding a condition to avoid division by zero.

 

I have addedd the condition in the DAX if try this below..

 

Promoted Week Count =
VAR TotalSales = CALCULATE(SUM('RSI Acct Merge'[Total Sales Amount]))
VAR TotalUnit = CALCULATE(SUM('RSI Acct Merge'[Total Sales Volume Units]))
VAR Avgprice = IF(TotalUnit <> 0, DIVIDE(TotalSales, TotalUnit), BLANK())
VAR OverallAvgPrice = CALCULATE(
DIVIDE(SUM('RSI Acct Merge'[Total Sales Amount]), SUM('RSI Acct Merge'[Total Sales Volume Units])),
ALLSELECTED('RSI Acct Merge'),
VALUES(AdWks[Year]),VALUES('RSI Acct Merge'[BANNER NAME]))
RETURN
CALCULATE(COUNTROWS(AdWks),FILTER(AdWks, Avgprice < OverallAvgPrice))

 

If you find this helpful , please mark it as solution which will be helpful for others and Your Kudos/Likes are much appreciated!

 

Thank You

Dharmendar S

LinkedIN 

View solution in original post

1 REPLY 1
dharmendars007
Super User
Super User

Hello @mgd14work , 

 

Make sure that TotalUnit is not zero, as dividing by zero would return a blank result. You can safeguard this by adding a condition to avoid division by zero.

 

I have addedd the condition in the DAX if try this below..

 

Promoted Week Count =
VAR TotalSales = CALCULATE(SUM('RSI Acct Merge'[Total Sales Amount]))
VAR TotalUnit = CALCULATE(SUM('RSI Acct Merge'[Total Sales Volume Units]))
VAR Avgprice = IF(TotalUnit <> 0, DIVIDE(TotalSales, TotalUnit), BLANK())
VAR OverallAvgPrice = CALCULATE(
DIVIDE(SUM('RSI Acct Merge'[Total Sales Amount]), SUM('RSI Acct Merge'[Total Sales Volume Units])),
ALLSELECTED('RSI Acct Merge'),
VALUES(AdWks[Year]),VALUES('RSI Acct Merge'[BANNER NAME]))
RETURN
CALCULATE(COUNTROWS(AdWks),FILTER(AdWks, Avgprice < OverallAvgPrice))

 

If you find this helpful , please mark it as solution which will be helpful for others and Your Kudos/Likes are much appreciated!

 

Thank You

Dharmendar S

LinkedIN 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

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