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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
alexbjorlig
Helper IV
Helper IV

Help improve my DAX measure - how can I filter my fact to only use latest values from related table?

I have a data-set, that is connected to a autogenerated calendar. For each year, I would like a distinct count of report keys - but it should only include the latest report per location. Links to resources in the bottom. Dataset looks like this:

 

Screen Shot 2021-12-02 at 14.54.49.png

 

Year=2018

The count should here return 2, because it includes Report E (latest for location YYYYY) and Report C (latest for location XXXXX).

 

Year=2019

The count should here return 2. Report B latest for location XXXXX and D latest for location YYYYY.

 Screen Shot 2021-12-02 at 14.54.49.png

Year 2020

The count should here return 2. Report A latest for location XXXXX and D latest for location YYYYY.

 

 

Attempt so far

 

CALCULATE(
    DISTINCTCOUNTNOBLANK('Fact'[ReportKey])
    ,REMOVEFILTERS('Calendar')
    ,'Calendar'[Date] <= MAX('Calendar'[Date])
    ,FILTER(
        ALL(ReportDim),
        VAR LOC = ReportDim[Location] RETURN
        VAR LATEST = CALCULATE(MAX(ReportDim[PublishDate]), ReportDim[Location] = LOC) RETURN
        ReportDim[PublishDate] = LATEST
    )
)

 

 

But the measure does not work - it does not successfully filter to only latest reports.

 

Screen Shot 2021-12-02 at 19.32.34.png

 

Please find Power BI report is here, and you find the dataset here.

1 ACCEPTED SOLUTION
alexbjorlig
Helper IV
Helper IV

After some (more) research, I think this is the best solution (in my original power bi file we use bi-directional filters, and the solution utilizes that fact):

 

VAR ONLY_LATEST = CALCULATETABLE(
    FILTER(
        ReportDim,
        VAR LOC = ReportDim[Location] RETURN
        VAR LATEST = CALCULATE(MAX(ReportDim[PublishDate]), REMOVEFILTERS(ReportDim), ReportDim[Location] = LOC) RETURN
        ReportDim[PublishDate] = LATEST
    )
    ,REMOVEFILTERS('Calendar')
    ,'Calendar'[Date] <= MAX('Calendar'[Date])
)
RETURN CONCATENATEX(ONLY_LATEST, ReportDim[ReportKey])

 

View solution in original post

3 REPLIES 3
alexbjorlig
Helper IV
Helper IV

After some (more) research, I think this is the best solution (in my original power bi file we use bi-directional filters, and the solution utilizes that fact):

 

VAR ONLY_LATEST = CALCULATETABLE(
    FILTER(
        ReportDim,
        VAR LOC = ReportDim[Location] RETURN
        VAR LATEST = CALCULATE(MAX(ReportDim[PublishDate]), REMOVEFILTERS(ReportDim), ReportDim[Location] = LOC) RETURN
        ReportDim[PublishDate] = LATEST
    )
    ,REMOVEFILTERS('Calendar')
    ,'Calendar'[Date] <= MAX('Calendar'[Date])
)
RETURN CONCATENATEX(ONLY_LATEST, ReportDim[ReportKey])

 

alexbjorlig
Helper IV
Helper IV

The solution seems to be pretty close, but does not give correct result when there is a slicer on reports, check image:

 

Screen Shot 2021-12-03 at 10.53.35.png

 

wdx223_Daniel
Super User
Super User

not sure this is really what you want, buy it feedback the correct result ,lol

_m4 =
VAR _MaxDate =
    MAX ( 'Calendar'[Date] )
VAR _LastDates =
    FILTER (
        'ReportDim',
        'ReportDim'[PublishDate]
            = CALCULATE (
                MAX ( 'ReportDim'[PublishDate] ),
                'ReportDim'[PublishDate] <= _MaxDate,
                ALLEXCEPT ( ReportDim, ReportDim[Location] )
            )
    )
VAR _t =
    CALCULATETABLE (
        FILTER (
            'Fact',
            CONTAINS (
                _LastDates,
                ReportDim[ReportKey], 'Fact'[ReportKey],
                ReportDim[PublishDate], 'Fact'[Date]
            )
        ),
        'Calendar'[Date] <= _maxdate
    )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Fact'[ReportKey] ),
        _t,
        'Calendar'[Date] <= _maxdate
    )

 

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.