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

Dax measure to get previous week and previous month record count

Hi,

 

Any help will be much appricatieated with following problem.

image001.jpg

 

Thanks

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi,

 

My dax Measures are fixed.

 

Last Week Count =
var MaxSelectedDate= CALCULATE( MAX ( 'Date'[Date]), ALLSELECTED ( 'Date'[Date] ))
Var SelectedWeek = CALCULATE(max('Date'[Week Number]),'Date'[Date]=MaxSelectedDate)
Var SelectedYear = CALCULATE(max('Date'[Year]),'Date'[Date]=MaxSelectedDate)
Var MaxWeek = CALCULATE(max('Date'[Week Number]),ALL('Date'))


return
calculate([Total Record Count],
FILTER(ALL('Date'),IF(SelectedWeek=1,
'Date'[Week Number]=MaxWeek && 'Date'[Year]=SelectedYear-1,
'Date'[Week Number]=SelectedWeek-1 && 'Date'[Year]=SelectedYear))
)
 
 
Last Month Count =
var MaxSelectedDate= CALCULATE( MAX ('Date'[Date]), ALLSELECTED ('Date'[Date] ))
Var SelectedMonth = CALCULATE(max('Date'[Monthnumber]),'Date'[Date]=MaxSelectedDate)
Var SelectedYear = CALCULATE(max('Date'[Year]),'Date'[Date]=MaxSelectedDate)
Var MaxMonth= CALCULATE(max('Date'[Monthnumber]),ALL('Date'))


return
CALCULATE([Total Record Count],
FILTER(ALL('Date'),IF(SelectedMonth=1,
'Date'[Monthnumber]=MaxMonth && 'Date'[Year]=SelectedYear-1,
'Date'[Monthnumber]=SelectedMonth-1 && 'Date'[Year]=SelectedYear))
)
 
It's working fine.
 
Thanks
 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi,

 

My dax Measures are fixed.

 

Last Week Count =
var MaxSelectedDate= CALCULATE( MAX ( 'Date'[Date]), ALLSELECTED ( 'Date'[Date] ))
Var SelectedWeek = CALCULATE(max('Date'[Week Number]),'Date'[Date]=MaxSelectedDate)
Var SelectedYear = CALCULATE(max('Date'[Year]),'Date'[Date]=MaxSelectedDate)
Var MaxWeek = CALCULATE(max('Date'[Week Number]),ALL('Date'))


return
calculate([Total Record Count],
FILTER(ALL('Date'),IF(SelectedWeek=1,
'Date'[Week Number]=MaxWeek && 'Date'[Year]=SelectedYear-1,
'Date'[Week Number]=SelectedWeek-1 && 'Date'[Year]=SelectedYear))
)
 
 
Last Month Count =
var MaxSelectedDate= CALCULATE( MAX ('Date'[Date]), ALLSELECTED ('Date'[Date] ))
Var SelectedMonth = CALCULATE(max('Date'[Monthnumber]),'Date'[Date]=MaxSelectedDate)
Var SelectedYear = CALCULATE(max('Date'[Year]),'Date'[Date]=MaxSelectedDate)
Var MaxMonth= CALCULATE(max('Date'[Monthnumber]),ALL('Date'))


return
CALCULATE([Total Record Count],
FILTER(ALL('Date'),IF(SelectedMonth=1,
'Date'[Monthnumber]=MaxMonth && 'Date'[Year]=SelectedYear-1,
'Date'[Monthnumber]=SelectedMonth-1 && 'Date'[Year]=SelectedYear))
)
 
It's working fine.
 
Thanks
 
Anonymous
Not applicable

Hi Amit,

 

Thank you for you reply.

I visited your blog. It's very informative but my requirement is little bit diffrent.

I got following 3 Measures. Only "Total Record Count" is working fine other 2 producting wrong result.

 

Total Record Count = COUNT(Supplier[Supplier])
 
Last Week Count =
var MaxSelectedDate= CALCULATE( MAX ( 'Date'[Date]), ALLSELECTED ( 'Date'[Date] ))
Var SelectedWeek = CALCULATE(max('Date'[Week Number]),'Date'[Date]=MaxSelectedDate)
Var SelectedYear = CALCULATE(max('Date'[Year]),'Date'[Date]=MaxSelectedDate)
Var MaxWeek = CALCULATE(max('Date'[Week Number]),ALL('Date'))


return
COUNTX(
FILTER(ALL('Date'),IF(SelectedWeek=1,
'Date'[Week Number]=MaxWeek && 'Date'[Year]=SelectedYear-1,
'Date'[Week Number]=SelectedWeek-1 && 'Date'[Year]=SelectedYear)),
[Total Record Count])
 
Last Month Count =
var MaxSelectedDate= CALCULATE( MAX ('Date'[Date]), ALLSELECTED ('Date'[Date] ))
Var SelectedMonth = CALCULATE(max('Date'[Monthnumber]),'Date'[Date]=MaxSelectedDate)
Var SelectedYear = CALCULATE(max('Date'[Year]),'Date'[Date]=MaxSelectedDate)
Var MaxMonth= CALCULATE(max('Date'[Monthnumber]),ALL('Date'))


return
COUNTX(
FILTER(ALL('Date'),IF(SelectedMonth=1,
'Date'[Monthnumber]=MaxMonth && 'Date'[Year]=SelectedYear-1,
'Date'[Week Number]=SelectedMonth-1 && 'Date'[Year]=SelectedYear)),
[Total Record Count])
Count Last week.JPG

 

 

 

Thanks

 

amitchandak
Super User
Super User

@Anonymous , Refer a my blog on Week vs Week

 

Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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