Search code examples
powerbidax

How to implement the following COUNTIF function into POWER BI?


I use a COUNTIF function to count and increment the number of instances of an ID in excel.

Id_Treatment_Count ID
1 769334
1 769345
2 769345
1 769376
3 769345

In Excel, I would get the above using =COUNTIF($B$2:B2,B2) and then I would drag down (Id_treatment_count being column A and ID being column B).

How can I get the Id_treatment_Count in Power BI using DAX?


Solution

  • Use EARLIER refer to an earlier row context created by FILTER. It's a bit like saying for each row, count the number of rows including this one and any before it that have the same ID.(That's what I understood from what you provided) This gives you an incrementing count for each ID as you would get with the COUNTIF in Excel :

      Id_Treatment_Count = 
        COUNTROWS(
            FILTER(
                'Table',
                'Table'[ID] = EARLIER('Table'[ID])
                && 'Table'[Index] <= EARLIER('Table'[Index])
            )
        )