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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
StoryDashboard
Frequent Visitor

Datediff range category defined by selectedvalue

Hi,

 

We all know the Datadiff function where we show the time difference between two dates. Of course we can calculate the difference in days, months etc....

But if I want the user to choose their own interval in stead of a hardcoded interval :

 

Example : 

Study Duration Months = DATEDIFF(REGISTRATIONS[REGISTER_DATE]; TODAY();MONTH)  (this is a column field!)
 
Here I want to replace MONTH by a selectedvalue value that comes from my own table where I have my list of choices  (MONTH, DAY, WEEK , QUARTER)
 
It seems that powerbi is not accepting this syntax?!  Smiley Mad  Anyone a solution?
1 ACCEPTED SOLUTION

@StoryDashboard  As I've mentioned above, I've implemented the same for DateAdd and thought you will follow the same logic for DateDiff.... Anyway, here is the logic for Datediff.... Same Table Visual will show the dates difference in different values based on the type selection. Create a New Measure as below

 

Test278_1 = 
VAR _Selection = SELECTEDVALUE(Test278Type[Type])
VAR _DateVal = MAX(Test278DateDiffDynamic[Date])
VAR _Day = DATEDIFF(_DateVal,NOW(),DAY)
VAR _Week = DATEDIFF(_DateVal,NOW(),WEEK)
VAR _Month = DATEDIFF(_DateVal,NOW(),MONTH)
VAR _Year = DATEDIFF(_DateVal,NOW(),YEAR)
VAR _Quarter = DATEDIFF(_DateVal,NOW(),QUARTER)

RETURN 
SWITCH(_Selection,
        "DAY",_Day,
        "WEEK",_Week,
        "MONTH",_Month,
        "QUARTER",_Quarter,
        "YEAR",_Year
      )

image.pngimage.pngimage.pngimage.pngimage.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




View solution in original post

3 REPLIES 3
PattemManohar
Community Champion
Community Champion

@StoryDashboard  Please create a new measure as below (Assuming that you have a lookup table which you will be using as filter to select Day,Month,Quarter,Year) In this case, the table name is Type. I've done DateAdd, you can do the same with DateDiff

 

Test278 = 
VAR _Selection = SELECTEDVALUE(Test278Type[Type])
VAR _DateVal = MAX(Test278DateDiffDynamic[Date])

RETURN 
SWITCH(_Selection,
        "DAY",_DateVal+1,
        "MONTH",EDATE(_DateVal,1),
        "QUARTER",EDATE(_DateVal,3),
        "YEAR",EDATE(_DateVal,12)
      )

image.pngimage.pngimage.pngimage.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Hi @PattemManohar ,

 

This is not where my question was about. The goal is to choose the interval from a lookup table with such values (DAY, MONTH, WEEK,  QUARTER). In that way i could use the same visual where the user can switch if the number of time between two dates is aligned with his choice...

 

So for instance we have two dates 01/01/2019 and today (e.g. 26/03/2019).

If we choose days the duration time is expressed in days and this record got he value 84 because the number of days between today and 01-01-2019 is 84 days.

Now another user with another dataneed chooses for weeks....The user chooses "WEEK" from the Slicer and then he got the answer 4+4+3 = approximately  11 weeks (I do this by heart). All data is now split by duration in weeks! He can use the same visual.Smiley Happy

 

>> It seems that the last argument of the DATEDIFF DAX function cannot be replaced by a self chosen field!

@StoryDashboard  As I've mentioned above, I've implemented the same for DateAdd and thought you will follow the same logic for DateDiff.... Anyway, here is the logic for Datediff.... Same Table Visual will show the dates difference in different values based on the type selection. Create a New Measure as below

 

Test278_1 = 
VAR _Selection = SELECTEDVALUE(Test278Type[Type])
VAR _DateVal = MAX(Test278DateDiffDynamic[Date])
VAR _Day = DATEDIFF(_DateVal,NOW(),DAY)
VAR _Week = DATEDIFF(_DateVal,NOW(),WEEK)
VAR _Month = DATEDIFF(_DateVal,NOW(),MONTH)
VAR _Year = DATEDIFF(_DateVal,NOW(),YEAR)
VAR _Quarter = DATEDIFF(_DateVal,NOW(),QUARTER)

RETURN 
SWITCH(_Selection,
        "DAY",_Day,
        "WEEK",_Week,
        "MONTH",_Month,
        "QUARTER",_Quarter,
        "YEAR",_Year
      )

image.pngimage.pngimage.pngimage.pngimage.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.