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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
SuzieKidd
Frequent Visitor

SQL to DAX

How do I write the following SQL in DAX please

 

DATEADD(s,-1,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)))

 

Thank you

1 ACCEPTED SOLUTION
smpa01
Community Champion
Community Champion

@SuzieKidd 

 

 

select DATEADD(s,-1,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,DATEFROMPARTS(2023,8,15)),0)))

 

 

returns

 

 

2023-07-31 23:59:59.000

 

 

You need DAX like this

 

 

Column = 
VAR dt = DATE(2023,8,15)
VAR yr = YEAR(dt)
VAR mo = MONTH(dt)
VAR date2 = DATE(yr,mo,1)
RETURN (date2-1)+0.99999

or 

Column = 
VAR dt = DATE(2023,8,15)
VAR yr = YEAR(dt)
VAR mo = MONTH(dt)
VAR date2 = DATE(yr,mo,1)
RETURN (date2-1)+CONVERT(TIME(23,59,59),DOUBLE)

 

 

 

smpa01_0-1675790584176.png

replace GETTODAY() with TODAY() in DAX

Why 0.99999 works here I have no idea,

@AlexisOlson  do you know why it works? Can we achieve that part through a calculation rather than hardcoding

 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

View solution in original post

5 REPLIES 5
smpa01
Community Champion
Community Champion

@SuzieKidd 

 

 

select DATEADD(s,-1,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,DATEFROMPARTS(2023,8,15)),0)))

 

 

returns

 

 

2023-07-31 23:59:59.000

 

 

You need DAX like this

 

 

Column = 
VAR dt = DATE(2023,8,15)
VAR yr = YEAR(dt)
VAR mo = MONTH(dt)
VAR date2 = DATE(yr,mo,1)
RETURN (date2-1)+0.99999

or 

Column = 
VAR dt = DATE(2023,8,15)
VAR yr = YEAR(dt)
VAR mo = MONTH(dt)
VAR date2 = DATE(yr,mo,1)
RETURN (date2-1)+CONVERT(TIME(23,59,59),DOUBLE)

 

 

 

smpa01_0-1675790584176.png

replace GETTODAY() with TODAY() in DAX

Why 0.99999 works here I have no idea,

@AlexisOlson  do you know why it works? Can we achieve that part through a calculation rather than hardcoding

 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

@smpa01 You're subtracting a day and then adding back almost a whole day.

 

(1-0.99999) days = 0.000001 days = 0.864 seconds.

 

So, essentially, you're taking midnight on the first of the month and subtracting roughly one second.

 

Since this is in the Power Query forum, I'd recommend

Date.StartOfMonth(DateTime.LocalNow()) - #duration(0,0,0,1)

 

In DAX, I'd recommend

EOMONTH ( TODAY(), -1 ) + TIME ( 23, 59, 59 )

 

Thank you I'll have a play with this.

Daryl-Lynch-Bzy
Community Champion
Community Champion

Hi @SuzieKidd - Could you please explain what you are trying to achieve with this formula? 

Please note that the DAX DATEADD function does not support seconds, minutes and hours like its SQL cousin.  You may consider using the add duration functionality in Power Query, or add a DAX functions that convert datetime to seconds (a bit like UNIX Timestamp).

Thank you

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.