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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
patri0t82
Post Patron
Post Patron

Finding the latest date with a nonblank value

Hello, I am trying to create a measure that will look in my table and find the last date in the [Month / Year] column where there is a value in the [Hours] column. Part of the problem is, I'm trying to select future dates. So if I've selected a date where there are no records, I need to find the most recent month WITH records. Here's my code so far:

 

TotalHours = 
VAR SelectedOperatingArea = SELECTEDVALUE('Hours - Tabular'[Operating Area])
VAR SelectedYear = SELECTEDVALUE(CalendarTable[Year])
VAR SelectedMonth = SELECTEDVALUE(CalendarTable[Month Name])
VAR SelectedDate = DATE(SelectedYear, MONTH("1 " & SelectedMonth & " 2000"), 1)
VAR MaxDateWithHours = CALCULATE(MAX('Hours - Tabular'[Month / Year]), 'Hours - Tabular'[Operating Area] = SelectedOperatingArea, NOT(ISBLANK('Hours - Tabular'[Hours])))

RETURN
IF(
    ISBLANK(CALCULATE(SUM('Hours - Tabular'[Hours]),
    'Hours - Tabular'[Month / Year] = SelectedDate,
    'Hours - Tabular'[Operating Area] = SelectedOperatingArea)),
    
    CALCULATE(SUM('Hours - Tabular'[Hours]),
    'Hours - Tabular'[Month / Year] = MaxDateWithHours,
    'Hours - Tabular'[Operating Area] = SelectedOperatingArea),

    CALCULATE(SUM('Hours - Tabular'[Hours]),
    'Hours - Tabular'[Month / Year] = SelectedDate,
    'Hours - Tabular'[Operating Area] = SelectedOperatingArea)
)

 

The problem is, it keeps returning (Blank) and I'm not sure how to fix it.

1 ACCEPTED SOLUTION

Hi, @patri0t82 

According to your description, do you want to get the maximum [Year/Month] when [hours] is not empty? I don’t have your test data. I created some test data based on your dax for testing. The specific steps are as follows:
(1)This is  my test data:

vyueyunzhmsft_0-1686118152491.png

And we do not need to create any relationship between two tables:

vyueyunzhmsft_1-1686118172894.png

(2)Then we can create a measure like this:

Measure = 
var _slicer=  MAX('Calendar'[Month / Year])
var _area=  MAX('Table'[Operating Area])
var _t =ADDCOLUMNS( FILTER( ALLSELECTED('Table') , 'Table'[Operating Area]=_area && 'Table'[Hours] <> BLANK()) , "Duration" ,ABS( [Month / Year] -_slicer))
var _min_duration=MINX(_t,[Duration])
var _min_date =  FILTER(_t,[Duration]=_min_duration)
return
minx(_min_date,[Month / Year])

 

Then we can get the result as follows:

vyueyunzhmsft_2-1686118208274.png

 

 

 

 

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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
patri0t82
Post Patron
Post Patron

To simplify the code a bit, I've done this and tried the ALL function: 

 

SelectedDate = 
VAR SelectedOperatingArea = SELECTEDVALUE('Hours - Tabular'[Operating Area])

RETURN
CALCULATE(LASTDATE(ALL('Hours - Tabular'[Month / Year])), 'Hours - Tabular'[Operating Area] = SelectedOperatingArea, NOT(ISBLANK('Hours - Tabular'[Hours])))

 

 

What this does though is brings back the date 12/31/2023, and ignores the other aspects of the code  (   

Hours - Tabular'[Operating Area] = SelectedOperatingArea, NOT(ISBLANK('Hours - Tabular'[Hours]))    )

Hi, @patri0t82 

According to your description, do you want to get the maximum [Year/Month] when [hours] is not empty? I don’t have your test data. I created some test data based on your dax for testing. The specific steps are as follows:
(1)This is  my test data:

vyueyunzhmsft_0-1686118152491.png

And we do not need to create any relationship between two tables:

vyueyunzhmsft_1-1686118172894.png

(2)Then we can create a measure like this:

Measure = 
var _slicer=  MAX('Calendar'[Month / Year])
var _area=  MAX('Table'[Operating Area])
var _t =ADDCOLUMNS( FILTER( ALLSELECTED('Table') , 'Table'[Operating Area]=_area && 'Table'[Hours] <> BLANK()) , "Duration" ,ABS( [Month / Year] -_slicer))
var _min_duration=MINX(_t,[Duration])
var _min_date =  FILTER(_t,[Duration]=_min_duration)
return
minx(_min_date,[Month / Year])

 

Then we can get the result as follows:

vyueyunzhmsft_2-1686118208274.png

 

 

 

 

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

 

Thank you so much for this! It's really close to what I was hoping for. Perhaps I ought to have been a bit more clear, but this is essentially how I wanted to view it.

 

It appears to even take the year into account, which is nice. The last thing I'd ask - instead of returning the maximum month, can I return the maximum Hours value for that month?

 

patri0t82_0-1686138551736.png

 

I've tried using your measure as a column to see if it would work, and it kind of does,

patri0t82_0-1686139019289.png

In this picture though, it seems like it's not taking the [Operating Area] into account. What I'd like to have happen here is repeat 15924.37 in the new column on the right, because it's the Hours value for the maximum date for that Operating Area

 

I think I figured it out!

 

Thanks again!

Column = 
VAR _slicer = MAX([Month / Year])
VAR _area = [Operating Area]  // Use the Operating Area for each row
VAR _t = ADDCOLUMNS(
    FILTER(
        ALLSELECTED('UPDATED - Hours'),
        [Operating Area] = _area && [Hours] <> BLANK()
    ),
    "Duration", ABS([Month / Year] - _slicer)
)
VAR _min_duration = MINX(_t, [Duration])
VAR _min_date = FILTER(_t, [Duration] = _min_duration)
RETURN 
IF(ISBLANK([Hours]),MINX(_min_date, [Hours]),[Hours])

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.