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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Pete_81
Frequent Visitor

Using a date variable in a CALCULATE and COUNTA expression?

I have these 2 DAX expressions :

ThisWeek = 
CALCULATE(
	COUNTA('MyTable'[Report Date])+0,
	'MyTable'[Report Date]
		IN { DATE(2021, 04, 02) }
)

PreviousWeek = 
CALCULATE(
	COUNTA('MyTable'[Report Date])+0,
	'MyTable'[Report Date]
		IN { DATE(2021, 03, 26) }
)

 But I want to alter them so that instead of specifing dates, they use the 2 most recent dates from the Report Dates column.

 

Merging this in, somehow:

CalcThisWeek = FORMAT(MAXX('MyTable','MyTable'[Report Date]),"YYYY/mm/dd")

 

Any ideas?

 

Thank you.

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Pete_81 , Try measures like

 

measure max Date =
var _max = maxx(allselected('MyTable'),'MyTable'[Report Date])
return
CALCULATE(
COUNTA('MyTable'[Report Date])+0,
filter('MyTable', 'MyTable'[Report Date] = _max)
)

measure 2nd max Date =
var _max1 = maxx(allselected('MyTable'),'MyTable'[Report Date])
var _max = maxx(filter(allselected('MyTable'),'MyTable'[Report Date] <_max1) ,'MyTable'[Report Date])
return
CALCULATE(
COUNTA('MyTable'[Report Date])+0,
filter('MyTable', 'MyTable'[Report Date] = _max)
)

View solution in original post

1 REPLY 1
amitchandak
Super User
Super User

@Pete_81 , Try measures like

 

measure max Date =
var _max = maxx(allselected('MyTable'),'MyTable'[Report Date])
return
CALCULATE(
COUNTA('MyTable'[Report Date])+0,
filter('MyTable', 'MyTable'[Report Date] = _max)
)

measure 2nd max Date =
var _max1 = maxx(allselected('MyTable'),'MyTable'[Report Date])
var _max = maxx(filter(allselected('MyTable'),'MyTable'[Report Date] <_max1) ,'MyTable'[Report Date])
return
CALCULATE(
COUNTA('MyTable'[Report Date])+0,
filter('MyTable', 'MyTable'[Report Date] = _max)
)

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Power BI Header Change

Power BI Header Update

Read about the exciting updates for the Power BI forum.

Top Solution Authors