Forum Discussion
DAX If Statement Based on Time via Direct Query
- 4 years ago
hankmobley
Here is the complete solution
When using direct query, creating a calculated column based on time calculations is not allowed. Therefore, we can follow these steps to work around it
1. Create Day/Night measure based on time calculationsDayNightt Measure = MAXX ( autofxx_order_leg, VARCurrentTimee = TIME ( HOUR (autofxx_order_leg[execution_time] ), MINUTE (autofxx_order_leg[execution_time] ), SECOND (autofxx_order_leg[execution_time] ) ) RETURN IF ( CurrentTimee > TIMEVALUE ( "07:00 AM" ) &&CurrentTimee <= TIMEVALUE ( "09:00 PM" ), "Day", "Night" ) )2. Create a disconnected slicer table:
Slicer Day/Night = SELECTCOLUMNS ( { "Day", "Night" }, "Day/Night", [Value] )3. Recreate the existing measures following this template
New Measure = VARSelectedDayNightt = SELECTEDVALUE ( 'Slicer Day/Night'[Day/Night] ) RETURN CALCULATE ( [Old Measure], FILTER (autofxx_order_leg, [DayNight Measure] =SelectedDayNightt ) )4. Create the slicer from the Day/Night slicer table and use the new measure in your visual
5. If columns are required in the visual they shall be added as measures, example:Profit = VARSelectedDayNightt = SELECTEDVALUE ( 'Slicer Day/Night'[Day/Night] ) RETURN IF ( HASONEVALUE ( 'Slicer Day/Night'[Day/Night] ), CALCULATE ( SUM (autofxx_order_leg[portfolio_base_pnl] ), FILTER (autofxx_order_leg, [DayNight Measure] =SelectedDayNightt ) ), SUM (autofxx_order_leg[portfolio_base_pnl] ) )
hankmobley
Here is the complete solution
When using direct query, creating a calculated column based on time calculations is not allowed. Therefore, we can follow these steps to work around it
1. Create Day/Night measure based on time calculations
DayNightt Measure =
MAXX (
autofxx_order_leg,
VARCurrentTimee =
TIME ( HOUR (autofxx_order_leg[execution_time] ), MINUTE (autofxx_order_leg[execution_time] ), SECOND (autofxx_order_leg[execution_time] ) )
RETURN
IF (
CurrentTimee > TIMEVALUE ( "07:00 AM" )
&&CurrentTimee <= TIMEVALUE ( "09:00 PM" ),
"Day",
"Night"
)
)
2. Create a disconnected slicer table:
Slicer Day/Night = SELECTCOLUMNS ( { "Day", "Night" }, "Day/Night", [Value] )
3. Recreate the existing measures following this template
New Measure =
VARSelectedDayNightt =
SELECTEDVALUE ( 'Slicer Day/Night'[Day/Night] )
RETURN
CALCULATE (
[Old Measure],
FILTER (autofxx_order_leg, [DayNight Measure] =SelectedDayNightt )
)
4. Create the slicer from the Day/Night slicer table and use the new measure in your visual
5. If columns are required in the visual they shall be added as measures, example:
Profit =
VARSelectedDayNightt =
SELECTEDVALUE ( 'Slicer Day/Night'[Day/Night] )
RETURN
IF (
HASONEVALUE ( 'Slicer Day/Night'[Day/Night] ),
CALCULATE (
SUM (autofxx_order_leg[portfolio_base_pnl] ),
FILTER (autofxx_order_leg, [DayNight Measure] =SelectedDayNightt )
),
SUM (autofxx_order_leg[portfolio_base_pnl] )
)
- hankmobley4 years agoHelper I
Thank you so much tamerj1 , that works great!
I really appreciate the time and effort you have taken to helping me 😊