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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
elandry9840
Frequent Visitor

Problems with COUNT

Hi,

First post here. This should be simple for an experienced DAX person, and I'd greatly appreciate a suggestion or two. I have a table with weekly stock closing prices for a few thousand tickers. I want to calc the 40-week moving average for each ticker on each week, then count the number of tickers trading below their moving average for each week. Simple, but I can't get the last part...the count. 

 

Here is the MA calc, which works perfectly:

 

40-week MA =
AVERAGEX(
DATESBETWEEN('Calendar'[Date], max('Calendar'[Date]) - 200, max('Calendar'[Date])),
Calculate(sum(PriceTable[closeadj]))
)
 
To count the number trading below the MA I'm using:
 
Number of Stocks < 200Day MA =
COUNTROWS(
filter('PriceTable',
PriceTable[closeadj] < [40-week MA]
)
)
 
This returns nothing, unfortunately. 
 
The model is simple, with only 1 fact table (weekly pricetable) and two DIM tables (Calendar and Tickers). Can somebody please suggest what I'm doing wrong?
 
Thanks
 

 

19 REPLIES 19
tamerj1
Super User
Super User

Hi @elandry9840 

how does your report look like? Can you share a screenshot of your data model?

Anonymous
Not applicable

Hi @elandry9840 ,

 

Not sure, is the first measure wrong? 
Please use this measure and test.

40-week MA =
CALCULATE (
    AVERAGE ( PriceTable[closeadj] ),
    FILTER (
        'Calendar',
        'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
            && 'Calendar'[Date]
                >= MAX ( 'Calendar'[Date] ) - 200
    )
)

vcgaomsft_0-1651736024591.png

vcgaomsft_1-1651736066649.png

vcgaomsft_2-1651736095780.png

Attached PBIX file for reference.

 

Best Regards,

Gao
Community Support Team 

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems with it, please feel free to let us know. Thanks a lot!

Hi v-cgao-msft,

 

Appreciate the reply!! I'm not sure it's working tho. Take a look:

 

40-week MA (New) =
CALCULATE (
AVERAGE ( PriceTable[closeadj] ),
FILTER (
'Calendar',
'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
&& 'Calendar'[Date]
>= MAX ( 'Calendar'[Date] ) - 200
)
)
 
40-week MA =
AVERAGEX(
DATESBETWEEN('Calendar'[Date], max('Calendar'[Date]) - 200, max('Calendar'[Date])),
calculate(sum(PriceTable[closeadj]))
)
 
Looks like [40-Wk MA (New)] is returning the closeadj. See below:
 
elandry9840_0-1651769684885.png

 

Could it have something to do with the price table having data only once per week? I noticed your price table had dailt prices. 

 

Thanks again!

Sure thing! Thanks for the reply!!

 

elandry9840_0-1651518991769.png

Not sure if this will be big enough. If not, let me know. 

@elandry9840 

The screenshot is very small. However please try

Number of Stocks < 200Day MA =
VAR FortyWeekMA = [40-week MA]
RETURN
COUNTROWS ( FILTER ( 'PriceTable', PriceTable[closeadj] < FortyWeekMA ) )

 

@elandry9840 

Have you tried above code?

Yes. Thank you, but doesn't seem to work. Must be something in my model that is peculiar. 

 

elandry9840_0-1651521195289.png

 

Sorry. Here it is:

 

elandry9840_0-1651523020305.png

 

@elandry9840 

Would you please add the moving average measure and snother measure: COUNTROWS ( 'PriceTable')

elandry9840_0-1651524505691.png

Thank you so much for the help! I have to step out for an hour, but will be back after that and will reply promptly to whatever you need. 

@elandry9840 

I apologize as well. It was too late and I fell asleep. 
Are you sure you have a relationship. The results are not filtered at all. There is something wrong here

Thanks. I went back and deleted them then recreated them. Still no luck. 

 

elandry9840_0-1651588384101.png

 

elandry9840_1-1651588580827.png

 

If possible, can you share a sample file?

Sure. How do I do that?

You can upload for example to We Transfer, DropBox or OneDrive and share the link

@elandry9840 

Please use the variable name inside FILTER same as the code I have shared

Replied to above

This is better:

 

elandry9840_1-1651519118511.png

 

Best:

elandry9840_2-1651519299934.png

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.