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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
anin
Frequent Visitor

Help calculating the closest less than date

Hello,

 

I am new to PowerBI and I am struggling with filtering data closest to a selected day.

My data table looks like this:

 

ID    Date             Value

1      1/2-2020      a

1      1/5-2020      b

1      1/8-2020      c

2      1/2-2020      x

2      1/5-2020      y

2      1/8-2020      z

 

I essentially want to select two dates to compare the values of the entries closest less than date.

So for example I want to be able chose the dates 1/3-2020 and 1/9-2020 in a slicer and get the following outcome:

 

ID     Value1     Value2

1       a              c

2       x              z

 

Kind regards

Anders

1 ACCEPTED SOLUTION
v-deddai1-msft
Community Support
Community Support

Hi @anin ,

 

Based on your sample data, you need to create two date table for two slicers:

Datetable1 = CALENDAR(DATE(2020,1,1),DATE(2020,1,31)) 

Datetable2 = Datetable1 

 

Then create two measures for value1 and value2:

 

Value1 = var a = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[Date]<=MAX(Datetable1[Date]))) return CALCULATE(MAX('Table'[Value]),'Table'[Date] = a)

Value2 = var a = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[Date]<=MAX(Datetable2[Date]))) return CALCULATE(MAX('Table'[Value]),'Table'[Date] = a)

 

1.gif

 

For more details, please refer to the pbix file: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EdnzOF02owBEjhNCEd...

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,

Dedmon Dai

View solution in original post

3 REPLIES 3
v-deddai1-msft
Community Support
Community Support

Hi @anin ,

 

Based on your sample data, you need to create two date table for two slicers:

Datetable1 = CALENDAR(DATE(2020,1,1),DATE(2020,1,31)) 

Datetable2 = Datetable1 

 

Then create two measures for value1 and value2:

 

Value1 = var a = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[Date]<=MAX(Datetable1[Date]))) return CALCULATE(MAX('Table'[Value]),'Table'[Date] = a)

Value2 = var a = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[Date]<=MAX(Datetable2[Date]))) return CALCULATE(MAX('Table'[Value]),'Table'[Date] = a)

 

1.gif

 

For more details, please refer to the pbix file: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EdnzOF02owBEjhNCEd...

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,

Dedmon Dai

anin
Frequent Visitor

Hello @amitchandak ,

 

Thank you for your response, however I am trouble getting it to work, as I am not getting any results in my table.

Can you please take a look at these screenshots from my PowerBI file, and help me identify what is going wrong?

 

Kind regards

AndersClip1.PNGClip2.PNGClip3.PNGClip4.PNGClip5.PNGClip6.PNG

amitchandak
Super User
Super User

@anin , Use an independent date table for the slicer , and then try these formula's

 

Value1 =
var _min = minx(allselected(Date), Date[Date])
var _max = maxx(filter(allselected(Date), Date[Date] <=_min),Date[Date] )
return
calculate(sum(Table[Value]), filter(Table, Table[date] =_max))


Value2 =
var _min = maxx(allselected(Date), Date[Date])
var _max = maxx(filter(allselected(Date), Date[Date] <=_min),Date[Date] )
return
calculate(sum(Table[Value]), filter(Table, Table[date] =_max))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.