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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Number of days between yesterday and due date

hi,

Am using MS Analysis Server and need to calculate the number of days between yesterday and the due date. For yesterday i used the GetDate()-1 formula and i get a date like 2023-01-01 00:00:00:000 the due date looks similar. My script that calculates the number of days looks like this:

CASE WHEN TRY_CONVERT(DATE, CONVERT(VARCHAR(8), [DueDate]), 112) IS NULL OR TRY_CONVERT(DATE,CONVERT(VARCHAR(8), [CurrentDate]), 112) IS NULL

THEN 0

ELSE DATEDIFF(dd, TRY_CONVERT(DATE, CONVERT(VARCHAR(8), [DueDate]),112, TRY_CONVERT(DATE, CONVERT(VARCHAR(8), [CurrentDate],112))

END

now, the script is not working and gives an error message. Tried a lot of alternatives but nothing worked so far.

Anyone an idea what needs to be done???

2 REPLIES 2
Anonymous
Not applicable

Hi Mahesh, thanks for the swift response, unfortunately your suggestion gave issues as well. We are trying now "SWITCH(TRUE(), ISBLANK([DueDate]) || ISBLANK(TODAY()-1), 0, TRUE(), DATEDIFF([DueDate], TODAY()-1, DAY))"

the CurrentDate which is in fact GetDate()-1 seems to be the problem....

Mahesh0016
Super User
Super User

@Anonymous Please Try below script
CASE WHEN CONVERT(DATE,CONVERT(VARCHAR(20), [DueDate], 109),112) IS NULL OR CONVERT(DATE,CONVERT(VARCHAR(20), [CurrentDate], 109),112) IS NULL

THEN 0

ELSE DATEDIFF(dd, CONVERT(DATE,CONVERT(VARCHAR(20), [DueDate], 109),112), CONVERT(DATE,CONVERT(VARCHAR(20), [CurrentDate], 109),112))

END

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.