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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

DAX - Get the next closest value based on Date filter

Hello,

I have this table:

miloviajando_0-1671799039721.png

 

And, based on a Calendar Date filter, I want to get the Value based on the closest date. 

 

For example

- If I filter the 7/12/2022, I want to get the Value = 23 (The next closest date in the table is 17/12/2022)

- If I filter the 22/12/2022, I want to get the Value = 21  (The next closest date in the table is 25/12/2022)

- If I filter the 20/12/2022, I want to get the Value = 15

 

 

I need to perform this on DAX. Thanks

 

1 ACCEPTED SOLUTION
AlB
Community Champion
Community Champion

Hi @Anonymous 

Try this measure. It assumes there is NO relationship between your date and fact tables. FactT is the table you show above. 

Measure_ =
VAR selected_ = SELECTEDVALUE ( CalendarT[Date] )
VAR lower_ =
    CALCULATE ( MAX ( FactT[Date] ), FactT[Date] <= selected_ )
VAR higher_ =
    CALCULATE ( MIN ( FactT[Date] ), FactT[Date] > selected_ )
RETURN
    IF (
        ABS ( higher_ - selected_ ) < ABS ( lower_ - selected_ ),
        CALCULATE ( DISTINCT ( FactT[Value] ), FactT[Date] = higher_ ),
        CALCULATE ( DISTINCT ( FactT[Value] ), FactT[Date] = lower_ )
    )

If this does not work, do share a pbix with sample data

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

View solution in original post

1 REPLY 1
AlB
Community Champion
Community Champion

Hi @Anonymous 

Try this measure. It assumes there is NO relationship between your date and fact tables. FactT is the table you show above. 

Measure_ =
VAR selected_ = SELECTEDVALUE ( CalendarT[Date] )
VAR lower_ =
    CALCULATE ( MAX ( FactT[Date] ), FactT[Date] <= selected_ )
VAR higher_ =
    CALCULATE ( MIN ( FactT[Date] ), FactT[Date] > selected_ )
RETURN
    IF (
        ABS ( higher_ - selected_ ) < ABS ( lower_ - selected_ ),
        CALCULATE ( DISTINCT ( FactT[Value] ), FactT[Date] = higher_ ),
        CALCULATE ( DISTINCT ( FactT[Value] ), FactT[Date] = lower_ )
    )

If this does not work, do share a pbix with sample data

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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