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
dokat
Post Prodigy
Post Prodigy

Calculated Column not returning correct values

Hi,

 

I'd like to create a calculated column based on date column. I'd like calculated column measure to return below  values. I have a code below that i tested. However it only returns values for YTD,Last Month, and Last Year. Please see below screenshot of the output, I'd like it to also return corresponding YTDLY, Last MonthLY, and Last YearLY values.

 

Date Column             Calculated Column

1/31/2022                      YTD

1/31/2021                      YTDLY

1/1/2022                        Last Month

1/1/2021                        Last MonthLY

12/31/2021                    Last Year

12/31/2020                    Last YearLY

 

 

 

Slicer = switch(
true(), 
CY[Calendar Year]= calculate(max(CY[Calendar Year]),all()),"YTD", 
CY[Calendar Year]= calculate(Max('CY'[Calendar Year]),DATESYTD(dateadd('CY'[Calendar Year],-1,Year))),"YTDLY",
CY[Calendar Year]= calculate(Max('CY'[Calendar Year]),DATESMTD(dateadd('CY'[Calendar Year],-1,MONTH))),"Last Month",
CY[Calendar Year]= CALCULATE(Max('CY'[Calendar Year]),DATESMTD(dateadd('CY'[Calendar Year],-12,MONTH))),"Last MonthLY", 
CY[Calendar Year]= DATE ( YEAR (calculate(max(CY[Calendar Year]),all()))-1, 12, 31 ),"Last Year",
CY[Calendar Year]= DATE ( YEAR (calculate(max(CY[Calendar Year]),all()))-2, 12, 31 ),"Last YearLY")

 

 

 

Below you will see the screenshot of the output. It is missing YTDLY, Last MonthLY, and Last YearLY. Appreciate any help to modify the code

 

dokat_0-1645634774596.png

 

I also tried the below if functions to create the measure for calculated column but still getting the same error message.

 

 

Slicer = IF(CY[Calendar Year]= calculate(max(CY[Calendar Year]),all()),"YTD", 
IF(CY[Calendar Year]= LASTDATE(DATEADD(CY[Calendar Year],-12,MONTH)),"YTDLY",
IF(CY[Calendar Year]= calculate(Max('CY'[Calendar Year]),DATESMTD(dateadd('CY'[Calendar Year],-1,MONTH))),"Last Month",
IF(CY[Calendar Year]= CALCULATE(Max('CY'[Calendar Year]),DATESMTD(dateadd('CY'[Calendar Year],-12,MONTH))),"Last MonthLY", 
IF(CY[Calendar Year]= DATE ( YEAR (calculate(max(CY[Calendar Year]),all()))-1, 12, 31 ),"Last Year",
IF(CY[Calendar Year]= DATE ( YEAR (calculate(max(CY[Calendar Year]),all()))-2, 12, 31 ),"Last YearLY")

)))))

 

 

1 ACCEPTED SOLUTION
dokat
Post Prodigy
Post Prodigy

I modified the code slightly, below worked for me.

 

 

Slicer = var maxdate = calculate(max(CY[Calendar Year]),all())return switch(true(), 
CY[Calendar Year]= maxdate,"YTD",
CY[Calendar Year]= maxdate-365,"YTDLY",
CY[Calendar Year]= DATE ( YEAR (maxdate)-1, 12, 31 ),"Last Year",
CY[Calendar Year]= DATE ( YEAR (maxdate)-2, 12, 31 ),"Last YearLY",
and(MONTH(CY[Calendar Year]) = month(maxdate),YEAR(CY[Calendar Year]) = YEAR(maxdate)),"Last Month",
and(MONTH(CY[Calendar Year]) = month(maxdate),YEAR(CY[Calendar Year]) = Year(maxdate)-1),"Last MonthLY")

 

 

View solution in original post

1 REPLY 1
dokat
Post Prodigy
Post Prodigy

I modified the code slightly, below worked for me.

 

 

Slicer = var maxdate = calculate(max(CY[Calendar Year]),all())return switch(true(), 
CY[Calendar Year]= maxdate,"YTD",
CY[Calendar Year]= maxdate-365,"YTDLY",
CY[Calendar Year]= DATE ( YEAR (maxdate)-1, 12, 31 ),"Last Year",
CY[Calendar Year]= DATE ( YEAR (maxdate)-2, 12, 31 ),"Last YearLY",
and(MONTH(CY[Calendar Year]) = month(maxdate),YEAR(CY[Calendar Year]) = YEAR(maxdate)),"Last Month",
and(MONTH(CY[Calendar Year]) = month(maxdate),YEAR(CY[Calendar Year]) = Year(maxdate)-1),"Last MonthLY")

 

 

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.