Forum Discussion

alexbjorlig's avatar
alexbjorlig
Icon for Helper IV rankHelper IV
4 years ago
Solved

How do I filter table, based on latest date in related table

I'm trying to learn DAX, and have watched this great video explaining filter context and row context.

However I'm still struggeling to achive the objective; Filter Sales table to only include 

 

Sales

 

TypeReportId    value    
123A5
123A6
123B2

 

Reports

 

ReportId    PublishDate    
A1 jan 2020
B6 feb 2021

 

 

Desired result

 

TypeReportId    value    
123B2

 

 

Thanks for your help.

  • PaulDBrown's avatar
    PaulDBrown
    4 years ago

    Oh ok! Now I get it...

     

    Ok, so you can use the following as a variables:

    Filter as variable = 
    VAR MaxD = CALCULATE(MAX(Reports[PublishDate]), ALL(Reports))
    VAR SalesRows = FILTER(Reports, Reports[PublishDate] = MaxD)
    RETURN
    .....Expression

    (Just beware that the SalesRows delivers a table)

     

    You can also create a measure to filter visuals using:

    Filter rows for max publish date = 
    VAR MaxD = CALCULATE(MAX(Reports[PublishDate]), ALL(Reports))
    VAR SalesRows = FILTER(Reports, Reports[PublishDate] = MaxD)
    RETURN
    COUNTROWS(SalesRows)

    and you can add this measure to the filters on the visual, set the value to 1 and it will filter the rows accordingly

     

9 Replies

  • PaulDBrown's avatar
    PaulDBrown
    Icon for Community Champion rankCommunity Champion

    There are many ways to do this: using a slicer, a measure, a filter in the filter pane...

    Even so, the very first step is setting up the model with dimension tables with the relevant relationships. 
    how is your model set up?

    do you want the filtering to be dynamic?

     

    • alexbjorlig's avatar
      alexbjorlig
      Icon for Helper IV rankHelper IV

      So I wan't this to happen in a measure, the model is star schema and set up with the relationship defined on the ReportId column. The filtering should be dynamic.

       

       

      • PaulDBrown's avatar
        PaulDBrown
        Icon for Community Champion rankCommunity Champion

        Ok, to calculate the values for the most recent report date:

        Value on Last published date =
        VAR MaxD =
            CALCULATE ( MAX ( Reports[PublishDate] ), ALL ( Reports ) )
        RETURN
            CALCULATE ( SUM ( Sales[value] ), Reports[PublishDate] = MaxD )