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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

VAR works with a constant date but not in a formula

I'm already tired searching

 

This works in dax:

MySales =
   VAR ReportingMonth = month(DATEVALUE("05/01/2020"))
   RETURN CALCULATE(SUM(fDespesas[Dispêndio]);FILTER(dCalendario;MONTH(dCalendario[Date])<=ReportingMonth);KEEPFILTERS(fDespesas[Dispêndio]))
 
This doens't:
MySales =
VAR ReportingMonth = month(LASTDATE(fDespesas[data]))
RETURN CALCULATE(SUM(fDespesas[Dispêndio]);FILTER(dCalendario;MONTH(dCalendario[Date])<=ReportingMonth);KEEPFILTERS(fDespesas[Dispêndio]))
 
This "month(LASTDATE(fDespesas[data]))" in a measure returns 5 but using "VAR ReportingMonth = 5" works also.
 
This variable is accepting only a constant to work fine.
 
I don't want to put a 5 but want the DAX to calculate that 5.
 
Any solution?
1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

If you try the following measure in your scenario what result do you get?
MySales =
VAR ReportingMonth = month(LASTDATE(fDespesas[data]))
RETURN
ReportingMonth

Not sure if it might work with MAX? https://www.sqlbi.com/blog/marco/2013/10/22/difference-between-lastdate-and-max-for-semi-additive-me...

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

5 REPLIES 5
amitchandak
Super User
Super User

@Anonymous , Try this

MySales =
VAR ReportingMonth = month(maxx(allselected(fDespesas),fDespesas[data]))
RETURN CALCULATE(SUM(fDespesas[Dispêndio]);FILTER(dCalendario;MONTH(dCalendario[Date])<=ReportingMonth);KEEPFILTERS(fDespesas[Dispêndio]))

 

or

 


MySales =
VAR ReportingMonth = calculate(month(LASTDATE(fDespesas[data])))
RETURN CALCULATE(SUM(fDespesas[Dispêndio]);FILTER(dCalendario;MONTH(dCalendario[Date])<=ReportingMonth);KEEPFILTERS(fDespesas[Dispêndio]))

Anonymous
Not applicable

It worked only with MAXX. Calculate brought the same as month_Lastdate.

Icey
Community Support
Community Support

Hi @Anonymous ,

 

Glad to hear the problem has been solved. You may help accept the replies making sense as solution above. Your contribution is highly appreciated.

 

 

Best Regards,

Icey

AllisonKennedy
Super User
Super User

If you try the following measure in your scenario what result do you get?
MySales =
VAR ReportingMonth = month(LASTDATE(fDespesas[data]))
RETURN
ReportingMonth

Not sure if it might work with MAX? https://www.sqlbi.com/blog/marco/2013/10/22/difference-between-lastdate-and-max-for-semi-additive-me...

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Anonymous
Not applicable

It worked with MAXX

 

Thanks!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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