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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

IF Statement help

Good afternoon,

Can someone tell me why this is not working?

 

IF (
YEAR( 'Casino Project Feb 2020'[Date] ) = YEAR (LASTDATE('Casino Project Feb 2020'[Date]))
&& MONTH( 'Casino Project Feb 2020'[Date]) = MONTH ( LASTDATE('Casino Project Feb 2020'[Date])),
"Yes",
"No"
)
 
I am trying to calculate MTD numbers without using a slicer (the higher ups do not want a slicer) based on the lastdate of the information in the table.  The reason I can't use it based on today is because for example, the last date of information I have in my table is Feb 29, 2020.  I want it to return MTD based on Feb. 29, 2020.  If I can get the yes/no's correct, then I can calculate the mtd by filtering.  Any help is greatly appreciated and if you have a better way, I am all ears.
1 ACCEPTED SOLUTION
Anonymous
Not applicable

@Anonymous 

 

This is because lastdate returns the current row, you will need to add All(table) as the context:

 

Column = 
var Lastdate_= CALCULATE(LASTDATE('Casino Project Feb 2020'[Date]),ALL('Casino Project Feb 2020'))
Return 
IF(YEAR( 'Casino Project Feb 2020'[Date] ) = YEAR (Lastdate_)
&& MONTH( 'Casino Project Feb 2020'[Date]) = MONTH (Lastdate_),
"Yes","No")

 

 

Best regards 

Paul Zheng

View solution in original post

6 REPLIES 6
ahmedoye
Responsive Resident
Responsive Resident

@Anonymous , try using this measure for a try:

VAR SelectedYear = YEAR(SELECTEDVALUE(Casino Project Feb 2020'[Date] ))
VAR SelectedMonth = YEAR(SELECTEDVALUE(Casino Project Feb 2020'[Date] ))
VAR LatestDate = MAX('Casino Project Feb 2020'[Date])
VAR LatestYear = YEAR(LatestDate)
VAR LatestMonth = MONTH(LatestDate)

RETURN IF(SelectedYear = LatestYear && SelectedMonth = LatestMonth, "Yes", "No")

if this Solution works for you, kindly kudo and mark as solution to enable others benefit from it.

Anonymous
Not applicable

@ahmedoye, In the second line, do you mean:

VAR SelectedMonth = MONTH(SELECTEDVALUE(Casino Project Feb 2020'[Date] ))

 

I changed it to Month and now I am getting all "no"'s  so it still isn't doing it correctly....  Do you have any other thoughts?  Thank you so much for looking at this.

Anonymous
Not applicable

@Anonymous 

 

This is because lastdate returns the current row, you will need to add All(table) as the context:

 

Column = 
var Lastdate_= CALCULATE(LASTDATE('Casino Project Feb 2020'[Date]),ALL('Casino Project Feb 2020'))
Return 
IF(YEAR( 'Casino Project Feb 2020'[Date] ) = YEAR (Lastdate_)
&& MONTH( 'Casino Project Feb 2020'[Date]) = MONTH (Lastdate_),
"Yes","No")

 

 

Best regards 

Paul Zheng

@Anonymous , I figure you are doing this inside a Calculated Column. Edit your Initial DAX to look like this:

IF (
YEAR( 'Casino Project Feb 2020'[Date] ) = YEAR (LASTDATE(ALL('Casino Project Feb 2020'[Date])))
&& MONTH( 'Casino Project Feb 2020'[Date]) = MONTH (LASTDATE(ALL('Casino Project Feb 2020'[Date]))),
"Yes",
"No"
)
 
If this solution works for you, kindly give a kudo and mark it as the solution to enable others benefit from it.
Anonymous
Not applicable

In the second line, do you mean:

VAR SelectedMonth = MONTH(SELECTEDVALUE(Casino Project Feb 2020'[Date] ))

 

I changed it to Month and now I am getting all "no"'s  so it still isn't doing it correctly....

Anonymous
Not applicable

Also, the calculated column is returning "yes" for all rows even when it should be "no"

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.