Forum Discussion

mgrayTCB's avatar
mgrayTCB
Icon for Helper IV rankHelper IV
1 year ago
Solved

Filter for Common years in DAX

I cant get my head around how to do this... I want to filter the table for only the years that the projects have in common. I wont know how many projects are in the table or what the common years are. I just want the common years so I can add the rent values together and calculate annual yoy rent growth for properties that existed in those years.

 

Table  
Propertyrentyear
Project 1502020
Project 11002021
Project 11202022
Project 11102023
Project 11052024
Project 11252025
Project 11002026
project 26002021
project 25502022
project 25802023
project 25702024
project 24902025
project 38002021
project 38252022
project 38752023

 

 

I would want this result

Expected Result 
Propertyrentyear
project 38002021
project 38252022
project 38752023
project 26002021
project 25502022
project 25802023
Project 11002021
Project 11202022
Project 11102023
  • Deku's avatar
    Deku
    1 year ago

    Sorry forgot to removefilters

     

    Measure =

    Var tbl=

    Calculatetable(

    Addcolumn(

    Values(table[property]),

    "@minYear", calculate( min( table[year] )),

    "@maxYear", calculate( max( table[year] ))

    ),

    Allselected()

    )

    Var minYear = MAXX( tbl, [@minYear])

    Var maxYear = MINX( tbl, [@maxYear])

    Var years =

    Filter(

    All( table[year] ),

    Table[year] >=minYear &&

    Table[year] <= maxYear

    )

    Return

    If( selectedvalue( table[year] ) in years, 1)

9 Replies

  • Deku's avatar
    Deku
    Icon for Super User rankSuper User

    Measure =

    Var tbl= Addcolumn(

    Values(table[property]),

    "@minYear", calculate( min( table[year] )),

    "@maxYear", calculate( max( table[year] ))

    )

    Var minYear = MAXX( tbl, [@minYear])

    Var maxYear = MINX( tbl, [@maxYear])

    Var years = 

    Filter(

    Values( table[year] ),

    Table[year] >=minYear &&

    Table[year] <= maxYear 

    )

    Return

    If( selectedvalue( table[year] ) in years, 1)

     

    Add the is measure to filter pane of the visual and filter to 1

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

      I am not following what this is supposed to do but it does not seem to work

       

       

      • Deku's avatar
        Deku
        Icon for Super User rankSuper User

        Rows 6 and 7 are wrong. You want the max min year and the min max year.

  • Hello mgrayTCB,

     

    Can you please try this approach:

    CommonYears =
    VAR AllProjects = DISTINCT('Table'[Property])
    VAR CommonYears =
        FILTER (
            VALUES('Table'[year]),
            CALCULATE (
                COUNTROWS (
                    FILTER (
                        AllProjects,
                        CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Property] = EARLIER('Table'[Property]) && 'Table'[year] = EARLIER('Table'[year]) ) > 0
                    )
                ) = COUNTROWS(AllProjects)
            )
        )
    RETURN
        FILTER (
            'Table',
            'Table'[year] IN CommonYears
        )
    

     

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

      Not sure why this approach is not working....

       

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi mgrayTCB,

    Thank you for reaching out to the Microsoft Fabric Forum Community.

    Also, a special thanks to Deku and Sahir_Maharaj  for the quick and helpful response.

    Just following up to check if the solution shared by our Super User Deku helped resolve your issue. If you're still facing difficulties or need further assistance, please let us know — we’re here to help!
    If the response addressed your query, we kindly request you to mark it as Accepted Solution and click Yes if you found it helpful. This supports others in the community as well.

    Best regards,
    Prasanna Kumar