Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredJoin us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.
Hi, please help to write an appropriate DAX code.
The issue:
A transaction is a row that contains a Driver Number, Vehicle Number, Load Date and a Load Time.
Upon checking the fact table, if a transaction for the same driver number, same load date, and load time in the time range of an hour (before or after the current load time) is found, then return the previous vehicle number, else return blank.
Thanks!
Solved! Go to Solution.
Hey @Anonymous , how about this:
Output =
VAR CALC =
CALCULATE (
MAX ( 'Table'[Load Time] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[Load Date], 'Table'[Driver Number] ),
EARLIER ( 'Table'[Load Time] ) - 'Table'[Load Time]
)
)
VAR PreviousRow =
TOPN (
1,
FILTER (
'Table',
'Table'[Vehicle Number] < EARLIER ( 'Table'[Vehicle Number] )
),
'Table'[Vehicle Number], DESC
)
VAR PreviousValue =
MINX ( PreviousRow, 'Table'[Vehicle Number] )
RETURN
IF (
ISBLANK ( CALC ),
"BLANK",
IF (
AND (
DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) < 60,
DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) > -60
),
PreviousValue,
"BLANK"
)
)
Proud to be a Super User!
Hey @Anonymous , how about this:
Output =
VAR CALC =
CALCULATE (
MAX ( 'Table'[Load Time] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[Load Date], 'Table'[Driver Number] ),
EARLIER ( 'Table'[Load Time] ) - 'Table'[Load Time]
)
)
VAR PreviousRow =
TOPN (
1,
FILTER (
'Table',
'Table'[Vehicle Number] < EARLIER ( 'Table'[Vehicle Number] )
),
'Table'[Vehicle Number], DESC
)
VAR PreviousValue =
MINX ( PreviousRow, 'Table'[Vehicle Number] )
RETURN
IF (
ISBLANK ( CALC ),
"BLANK",
IF (
AND (
DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) < 60,
DATEDIFF ( 'Table'[Load Time], CALC, MINUTE ) > -60
),
PreviousValue,
"BLANK"
)
)
Proud to be a Super User!
| User | Count |
|---|---|
| 8 | |
| 5 | |
| 3 | |
| 3 | |
| 3 |
| User | Count |
|---|---|
| 11 | |
| 9 | |
| 8 | |
| 8 | |
| 6 |