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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Youcef_Data
Regular Visitor

Need help with DAX Measure in Power BI - Returning Last Available Date

Hello Power BI Community,

I'm currently working on a project where I'm facing an issue with a DAX measure, and I'm hoping someone here can assist me.

Problem:
  I have a table (Table1) with dates and corresponding items. I want to create a DAX measure that returns the "last available date less than selected date" for a selected item if the selected date doesn't exist in the table.
My issue arises when I select a date that doesn't exist; it returns a blank value.

 

Script: Here's the DAX script I've come up with:

 

" measeure =
VAR SelectedDate = MAX(Table1[DATE])
VAR SelectedKeyItem = SELECTEDVALUE(Table1[ID_ITEMS]) -- Calculate the last available date less than SelectedDate for the same item
VAR LastAvailableDate =
    CALCULATE( MAX(Table1[DATE]),
        FILTER( ALL(Table1),
             Table1[DATE] < SelectedDate &&
             Table1[ID_ITEMS] = SelectedKeyItem ) )
     -- Check if the selected date exists in the table
  VAR DateExists = COUNTROWS( FILTER( ALL(Table1),
                           Table1[DATE] = SelectedDate &&
                           Table1[ID_ITEMS] = SelectedKeyItem ) )
-- If the selected date doesn't exist, return the last available date -- Otherwise, return SelectedDate
     VAR Result = IF(
                        DateExists = 0,
                        LastAvailableDate, SelectedDate )
      RETURN
          Result
"

Sample Data:

ID_ITEMS

DATE

Moved_Qty

Running Total

INDEX

1

1/1/2015

1

1

1

1

1/2/2015

2

3

2

2

1/3/2015

10

10

1

2

1/4/2015

5

15

2

1

1/5/2015

5

8

3

1

1/5/2015

-3

5

4

3

1/7/2015

3

3

1

2

1/9/2015

-7

8

3

2

1/11/2015

-2

6

4

3

1/11/2015

-6

-3

2

     


Thank you in advance for your help!

Best regards.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Youcef_Data 

Thanks for the solution @amitchandak  provided, and i want to offer some infotmation for you to refer to.

You can create a calendar table.

Calendar = CALENDAR(DATE(2015,1,1),DATE(2015,12,31))

Then create a relationship between tables.

vxinruzhumsft_0-1712022584903.png

Then create a measure.

Measure = CALCULATE(MAX(Table1[DATE]),ALLEXCEPT('Table1',Table1[ID_ITEMS]),Table1[DATE]<=MAX('Calendar'[Date]))

Last, put the date column of the calendar to the slicer.

Output

vxinruzhumsft_1-1712022835753.png

 

Best Regards!

Yolo Zhu

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

 

 

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

Based on the data that you have shared, show the expected result clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

@Youcef_Data ,

Last ID Date = var _max = maxx(filter(ALLSELECTED(Data), Data[ID_ITEM] = Max(Data[ID_ITEM])), Data[Date])
return
CALCULATE(Sum(Data[Moved_Qty]), filter((Data) , Data[Date] =_max))

 

Also refer, for correct sum refer 2nd blog


https://amitchandak.medium.com/power-bi-get-the-last-latest-value-of-a-category-d0cf2fcf92d0

https://amitchandak.medium.com/power-bi-get-the-sum-of-the-last-latest-value-of-a-category-f1c839ee8...

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

@amitchandak


Thanks for your response,
but your measure  still return Blank value 
"

measeure 2 =
  var _max = maxx(filter(ALLSELECTED(Table1), Table1[ID_ITEMS] = Max(Table1[ID_ITEMS])), Table1[DATE])
returnCALCULATE(Sum(Table1[Moved_Qty]), filter((Table1) , Table1[DATE] =_max))"

let me explain the issue
 when I select a ID_ITEMS as filter (for example ID_ITEMS 2 ) and I select a date in power bi "don't exist in the table " ( for example  1/6/2015) 
the measure return Blank value ( it should return 1/4/2015 as last date available less than selectde date for ID_ITEMS 2 ).

Anonymous
Not applicable

Hi @Youcef_Data 

Thanks for the solution @amitchandak  provided, and i want to offer some infotmation for you to refer to.

You can create a calendar table.

Calendar = CALENDAR(DATE(2015,1,1),DATE(2015,12,31))

Then create a relationship between tables.

vxinruzhumsft_0-1712022584903.png

Then create a measure.

Measure = CALCULATE(MAX(Table1[DATE]),ALLEXCEPT('Table1',Table1[ID_ITEMS]),Table1[DATE]<=MAX('Calendar'[Date]))

Last, put the date column of the calendar to the slicer.

Output

vxinruzhumsft_1-1712022835753.png

 

Best Regards!

Yolo Zhu

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

 

 

Thanks @v-xinruzhu-msft , it's works.

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.