Forum Discussion

elguestca1's avatar
elguestca1
Advocate I
2 years ago
Solved

Count based on condition

Hello, I am quite new to PBI and I am having issues with one measure. I need to count how many ID's are having "Max date" greater than "First date" excluding blank values. For example 99046 should co...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi elguestca1 ,

    I'm sorry, but because you have two tables and the table relationships are many-to-many, the feature is not possible in this case. I can only manually merge the two tables you give into one table and then use DAX to calculate them.
    Then I need to correct one mistake you made in the issue you asked, id 95525 should count as 0 instead of 1 the same as id 99046:

    I created a slicer based on the ID:

    You can use the following DAX to create a measure:

    CountID = 
    CALCULATE(
        COUNTROWS('Table3'),
        NOT(ISBLANK('Table3'[Max Date])),
        NOT(ISBLANK('Table3'[First Date])),
        'Table3'[Max Date] > 'Table3'[First Date],
        'Table3'[ID] IN VALUES(Table3[ID])
    )
    

    And the final output is shown in the following figure:

     

    Then I strongly discourage you from using a many-to-many relationship, it has a lot of limitations, you can choose to merge two tables manually, or you can add a bridge table to optimize many-to-many relationships, you can refer to the following documents:
    Many-to-many relationships in Power BI Desktop - Power BI | Microsoft Learn
    Many-to-many relationship guidance - Power BI | Microsoft Learn

    Best Regards,

    Dino Tao

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