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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
PrakashPalle
Helper I
Helper I

Condition based on date time field fall between two date time columns

Hi Power BI Experts,

I want to calculate condition column based on the date time field will fall in between, before & after conditions. Sample data below. Expected output in "condition" column. Please provide the dax expression. I tried the below dax code but it doesn't given correct output what i am looking. Thanks in advance.

 

Date TimeFrom Date TimeTo Date Time IDCondition
11/16/2020 0:0111/16/2020 0:0011/18/2020 0:006107With In Range
7/9/2020 14:457/10/2020 0:007/15/2020 0:003734Before Range
3/7/2020 22:003/7/2020 0:003/17/2020 0:001478With In Range
11/8/2020 1:5210/27/2020 0:0011/4/2020 0:005717After Range
4/16/2020 15:304/12/2020 0:004/22/2020 0:002039With In Range
5/31/2020 3:476/1/2020 12:006/5/2020 12:002834Before Range

 

Dax Expression:

Condition =
vAR _DATE='Date Time'[Date Time]
var _With_In=COUNTROWS(FILTER('Date Time','Date Time'[From Date Time]<=_DATE && 'Date Time'[To Date Time]>=_DATE))
var _Before=COUNTROWS(FILTER('Date Time','Date Time'[From Date Time]<='Date Time'[Date Time]))
var _After=COUNTROWS(FILTER('Date Time','Date Time'[To Date Time]>=_DATE))
RETURN
IF(_With_In=1,"With in",IF(_After=1,"After",IF(_Before=1,"Before")))

 

1 ACCEPTED SOLUTION
dulan_kavinda
Helper I
Helper I

Right click on the table and add "New Column". Use below DAX (Modify the table name).

 

Condition =
VAR DateTimeValue = 'Table_name'[Date Time]
VAR FromDateTimeValue = 'Table_name'[From Date Time]
VAR ToDateTimeValue = 'Table_name'[To Date Time]
RETURN
SWITCH(
TRUE(),
DateTimeValue >= FromDateTimeValue && DateTimeValue <= ToDateTimeValue, "With In Range",
DateTimeValue < FromDateTimeValue, "Before Range",
DateTimeValue > ToDateTimeValue, "After Range"
)

 

Screenshot_2024-06-24-22-33-27-718_com.microsoft.rdc.androidx.jpg

View solution in original post

1 REPLY 1
dulan_kavinda
Helper I
Helper I

Right click on the table and add "New Column". Use below DAX (Modify the table name).

 

Condition =
VAR DateTimeValue = 'Table_name'[Date Time]
VAR FromDateTimeValue = 'Table_name'[From Date Time]
VAR ToDateTimeValue = 'Table_name'[To Date Time]
RETURN
SWITCH(
TRUE(),
DateTimeValue >= FromDateTimeValue && DateTimeValue <= ToDateTimeValue, "With In Range",
DateTimeValue < FromDateTimeValue, "Before Range",
DateTimeValue > ToDateTimeValue, "After Range"
)

 

Screenshot_2024-06-24-22-33-27-718_com.microsoft.rdc.androidx.jpg

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.