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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
PrakashPalle
Helper II
Helper II

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (8,679)