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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Add Minutes to DateTime (but only over working hours)

All,

 

I receive the following error:

"Query (21, 20) Table variable 'TopValues' cannot be used in current context because a base table is expected."


when executing the below expression:

// This expression adds minutes to a datetime, but only distributes them over specified work days (mon-fri) and work hours (9-5).

EVALUATE

VAR startDatetime = DATE ( 2018, 12, 28 ) + TIME ( 16, 0, 0 )
    
VAR MinutesToAdd = 720

VAR Range = GENERATESERIES ( startDatetime, startDatetime + 90, TIME ( 0, 1, 0 ) ) // the 90 days is an abritary number.
    
VAR FilteredRange =
    FILTER (
        Range,
        NOT WEEKDAY ( [Value] ) IN { 1, 7 } // Include only weekdays
            && HOUR ( [Value] ) >= 9 && HOUR ( [Value] ) < 17 // 9 AM to 5 PM
            && NOT ( MONTH ( [Value] ) = 12 && DAY ( [Value] ) = 25 ) // Exclude Christmas
    )
    
VAR TopValues = TOPN ( MinutesToAdd, FilteredRange, [Value], ASC )
    
VAR Result = MAX ( TopValues[Value] )

RETURN
    Result

Any thoughts on a solution?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Fixed it.  For some reason, changing MAX to MAXX works:

 

 
EVALUATE
{
VAR startDatetime = DATE ( 2018, 12, 28 ) + TIME ( 16, 0, 0 )
    
VAR MinutesToAdd = 720

VAR Range = GENERATESERIES ( startDatetime, startDatetime + 90, TIME ( 0, 1, 0 ) ) // the 90 days is an abritary number.
    
VAR FilteredRange =
    FILTER (
        Range,
        NOT WEEKDAY ( [Value] ) IN { 1, 7 } // Include only weekdays
            && HOUR ( [Value] ) >= 9 && HOUR ( [Value] ) < 17 // 9 AM to 5 PM
            && NOT ( MONTH ( [Value] ) = 12 && DAY ( [Value] ) = 25 ) // Exclude Christmas
    )
    
VAR TopValues = TOPN ( MinutesToAdd, FilteredRange, [Value], ASC )
    
VAR Result = MAXX ( TopValues, [Value] ) // Must be changed to MAXX

RETURN
    Result
    }

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Fixed it.  For some reason, changing MAX to MAXX works:

 

 
EVALUATE
{
VAR startDatetime = DATE ( 2018, 12, 28 ) + TIME ( 16, 0, 0 )
    
VAR MinutesToAdd = 720

VAR Range = GENERATESERIES ( startDatetime, startDatetime + 90, TIME ( 0, 1, 0 ) ) // the 90 days is an abritary number.
    
VAR FilteredRange =
    FILTER (
        Range,
        NOT WEEKDAY ( [Value] ) IN { 1, 7 } // Include only weekdays
            && HOUR ( [Value] ) >= 9 && HOUR ( [Value] ) < 17 // 9 AM to 5 PM
            && NOT ( MONTH ( [Value] ) = 12 && DAY ( [Value] ) = 25 ) // Exclude Christmas
    )
    
VAR TopValues = TOPN ( MinutesToAdd, FilteredRange, [Value], ASC )
    
VAR Result = MAXX ( TopValues, [Value] ) // Must be changed to MAXX

RETURN
    Result
    }
Greg_Deckler
Community Champion
Community Champion

Right, TOPN requires a Table, not a Table or Table Expression. 

 

https://docs.microsoft.com/en-us/dax/topn-function-dax

 

I'm not entirely certain I understand your need for that since the very next line you just grab the MAX. Just grab the MAX of your FilteredRange?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

The TOPN (in conjunction with MAX()) is required for the described business logic to work (i.e., add time to a datetime but only have it apply to work days and work hours).  As an alternative to TOPN and MAX(), I may have added an index and then applied FILTER() to return [value] where [index] = MinutesToAdd, but TOPN is an elegant solution with an optimized query plan.

 

Anyhoo, this point is moot because TOPN is not the issue. You can confirm this in DAX Studio by evaluating that variable, i.e., this works:

 

VAR TopValues = TOPN ( MinutesToAdd, FilteredRange, [Value], ASC ) 
//VAR Result = MAX ( TopValues[Value] )
RETURN
TopValues

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Solution Authors