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

Day function not referencing dates column

I am trying to run a DAX calculation to clean up my dates. I want to create an IF function that checks if a date is on the 1st of the month, and if it is, subtracts 1 day to put it at the end of the previous month. However, I find myself unable to reference my dates column using the day function. Here is the syntax I am trying to use:

Date (Corrected) = CALCULATE(IF(DAY("date")=1,DATEADD("Date",-1,DAY),"date"))

2 ACCEPTED SOLUTIONS
SpartaBI
Community Champion
Community Champion

@Anonymous write this:

 

Date (Corrected) = 
IF(DAY([Date]) = 1, [Date] - 1, [Date])

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

View solution in original post

@Anonymous You are indeed in the right direction. That's why my intitial tought was to give you the code for the calculated column. You can use what I sent in the first message or create it in Power Query or in the data source.

Please don't forget to accept the relevant message as a solution for community visibility.
P.S. Check out my showcase report - got some high level stuff there. Sure you will find there a lot of cool ideas. 
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
Give it a thumbs up over there if you liked it 🙂

View solution in original post

6 REPLIES 6
SpartaBI
Community Champion
Community Champion

@Anonymous write this:

 

Date (Corrected) = 
IF(DAY([Date]) = 1, [Date] - 1, [Date])

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

Anonymous
Not applicable

The problem is that the Day function isn't recognizing the name of my dates column. Unless i enclose it with a sum function first Day(Sum("date")). It only allows me to select calculated measures.

 

@Anonymous are you trying to create a measure or a calculated column? If it's a measure , then yes, you need to wrap any column with an aggregate. So, you could use:

 

Date (Corrected) Measure =
VAR _current_date =  SELECTEDVALUE('Table'[Date])
RETURN
IF(DAY(_current_date) = 1, _current_date - 1, _current_date)

 




2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

Anonymous
Not applicable

Yes I was building a measure and your formula worked! I realized however that I cannot use a measure as a primary key in a relationship to another dataset. I imagine I should actually be using a calculated column in the "Transform Data" editor, is that right?

 

The problem is that most of my dates in the original dataset are correctly entered as end of month, but there are 3 or 4 each month that are entered as the 1st of the following month. Hence my need to clean the data so that my visuals are accurate. I hope this makes sense. Really appreciate the help.

@Anonymous You are indeed in the right direction. That's why my intitial tought was to give you the code for the calculated column. You can use what I sent in the first message or create it in Power Query or in the data source.

Please don't forget to accept the relevant message as a solution for community visibility.
P.S. Check out my showcase report - got some high level stuff there. Sure you will find there a lot of cool ideas. 
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
Give it a thumbs up over there if you liked it 🙂

Anonymous
Not applicable

Yes I was building a measure and your formula worked! I realized however that I cannot use a measure as a primary key in a relationship to another dataset. I imagine I should actually be using a calculated column in the "Transform Data" editor, is that right?

 

The problem is that most of my dates in the original dataset are correctly entered as end of month, but there are 3 or 4 each month that are entered as the 1st of the following month. Hence my need to clean the data so that my visuals are accurate. I hope this makes sense. Really appreciate the help.

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.