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

Dateadd function returning error

Hi,

 

I am trying to create a calculate dcolumn using values in Calender Year Column. To calculate YTDLY i am using below dateadd function line but it returns following error. Can anyone have solve for this problem?

 

"The syntax for ',' is incorrect. (DAX(var maxdate = calculate(max(CY[Calendar Year]),all())return switch(true(), CY[Calendar Year]= maxdate,"YTD",CY[Calendar Year]= dateadd(var maxdate, -1,year),"YTDLY")))."

 

CY[Calendar Year]= dateadd(var maxdate, -1,year),"YTDLY") -> however it gives 

 

Slicer = var maxdate = calculate(max(CY[Calendar Year]),all())return switch(true(), 
CY[Calendar Year]= maxdate,"YTD",
CY[Calendar Year]= dateadd(var maxdate, -1,year),"YTDLY")

 

1 ACCEPTED SOLUTION

@PhilipTreacy Please disregard my questions. I modified the code and 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

8 REPLIES 8
PhilipTreacy
Super User
Super User

@dokat 

 

Please supply your file otherwise we have to recreate everything by hand.

regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


@PhilipTreacy Please disregard my questions. I modified the code and 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")

@PhilipTreacy I uploaded the file to dropbox. Please see below

 

https://www.dropbox.com/s/lj93ly0uw1utq20/Test.pbix?dl=0

TheoC
Super User
Super User

Hi @dokat 

 

Is there a reason the "ALL" function does not have a table or column name (just before the RETURN in your first line of code)?

 

Cheers,

Theo

 

 

 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

@TheoC I modified the code slightly and it worked FOR ytdly however now I can't figure out Last MonthLY value. Please see below screenshot. I am trying to 01/01/2021 should be Last MonthLY

 

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)-12,YEAR(CY[Calendar Year]) = YEAR(maxdate)),"Last MonthLY")

 

dokat_0-1645762359971.png

 

I am trying to get the max date based on selection of slicers thats wy there is not table for all.

PhilipTreacy
Super User
Super User

Hi @dokat 

I'm a bit unclear about the code you've written, it's always best to supply the file you are using, but, when you declare a VAR you don't subsequently reference it using the VAR keyword.  You also need to supply a default return value for SWITCH.  Try this

 

Slicer = 

VAR maxdate = calculate(max(CY[Calendar Year]),all())

RETURN SWITCH( TRUE(), 

    CY[Calendar Year]= maxdate,"YTD",

    CY[Calendar Year] = dateadd(maxdate, -1,year),"YTDLY",

    "DEFAULT VALUE"

)

 

regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


@PhilipTreacy Thanks for your response. still receiving below error message

 

The first argument to 'DATEADD' must specify a column.

 

Below line worked for me

 

CY[Calendar Year]= maxdate-365,"YTDLY"

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.