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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.