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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
yagoframos
New Member

Find Nearest Date in a Calendar

I've a data base, for Invoices paid on the last few months. I've Invoice Date, Invoice Creation Date, Invoice Due Date and Invoice Payment Date. In other table I've a calendar with Dates based on a cycle(Image below)

 

yagoframos_0-1676994933693.png

 

 

My use case is:

Invoice NumberDue DatePayment DateInvoice DateInvoice Creation DateCycle Date
0392/28/20222/28/20222/18/20222/28/2022?

 

In summary I need to find the closest date related to my "Due Date". In that example I'll use Cycle 9, Cycle date 2/28/2022.

1 ACCEPTED SOLUTION

Hi @yagoframos 
Please find attached sample file containing both options.

1.png

Cycle Date 2 = 
MAXX (
    TOPN (
        1,
        FILTER (
            'Calendar',
            'Calendar'[Month Number] = MONTH ( PaidInvoices[Due Date] )
                && PaidInvoices[Due Date] - 'Calendar'[Date] >= 0
        ),
        PaidInvoices[Due Date] - 'Calendar'[Date],
        ASC
    ),
    'Calendar'[Date]
)

 

 

View solution in original post

6 REPLIES 6
tamerj1
Super User
Super User

Hi @yagoframos 
Assuming that you are creating a calculated column and that the Calendar table is unpivotted as suggested by @FreemanZ then you may try the following

Cycle Date =
MAXX (
    TOPN (
        1,
        FILTER (
            'Calendar',
            'Calendar'[Month Number] = MONTH ( PaidInvoices[Due Date] )
        ),
        ABS ( PaidInvoices[Due Date] - 'Calendar'[Date] ), ASC
    ),
    'Calendar'[Date]
)

Awesome!!! That works for the next Cycle, and if I want to see the first one before the due date?

Hi @yagoframos 
Please find attached sample file containing both options.

1.png

Cycle Date 2 = 
MAXX (
    TOPN (
        1,
        FILTER (
            'Calendar',
            'Calendar'[Month Number] = MONTH ( PaidInvoices[Due Date] )
                && PaidInvoices[Due Date] - 'Calendar'[Date] >= 0
        ),
        PaidInvoices[Due Date] - 'Calendar'[Date],
        ASC
    ),
    'Calendar'[Date]
)

 

 

Thank you!!!

FreemanZ
Super User
Super User

hi @yagoframos 

the point might be to unpivot your table first to like:

MonthMonthNoCycleDate
Jan1Cycle11/4/2022
   

 

then everything become easier.

That should't be a problem, my only concern is the best way to use the LOOKUP to find the right date.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.