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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Nietzsche
Helper V
Helper V

How to create a calculated column as a change counter for another column?

Hello everyone,

 

I would like to create a calculated column that keep count of changes in each row in another column.

 

See image below.

Nietzsche_0-1723442789604.png

 

 

Everytime value in the Type column changed to another value or blank/null, the calculated column counter should increase by 1.

 

I have tried with the RANK() function but can not seem to get it to work. Any help would be highly appreciated.

 

Thank you for reading.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Nietzsche,

 

In my testing, an index column is created in Power Query.

vdengllimsft_0-1723451582977.png

 

Then add a calculated column (ChangeCounter) to determine whether the value of the current row has changed from the previous row.

If there is a change, it is 1, and if not, it is 0.

vdengllimsft_1-1723451600380.png

ChangeCounter = 
VAR CurrentRow = 'Table'[Type]
VAR PreviousRow = 
    IF(
        'Table'[Index] = 1, 
        BLANK(), 
        CALCULATE(
            MAX('Table'[Type]), 
            FILTER(
                'Table', 
                'Table'[Index] = EARLIER('Table'[Index]) - 1
            )
        )
    )
RETURN
    IF(
        ISBLANK(PreviousRow) || CurrentRow <> PreviousRow, 
        1, 
        0
    )

 

Finally, add a calculated column (CumulativeChangeCounter) to show the sum of the number of changes in the current column compared to all previous columns.

vdengllimsft_2-1723451673462.png

CumulativeChangeCounter = 
CALCULATE(
    SUM('Table'[ChangeCounter]), 
    FILTER(
        ALL('Table'), 
        'Table'[Index] <= EARLIER('Table'[Index])
    )
)

 

Best Regards,

Dengliang Li

 

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

View solution in original post

2 REPLIES 2
DataNinja777
Super User
Super User

Hi @Nietzsche ,

 

You can achieve the desired output by creating an Index column using Power Query, as the table in your data model does not inherently store the order in which it is displayed.  Once index column is created in Power Query, create a change counter dax column to identify the change in Type from the previous row.  

Change Counter dax = 
IF(
    'Table'[Index] = 1,
    0, -- No change for the first row
    IF(
        'Table'[Type] <> 
        CALCULATE(
            MAX('Table'[Type]),
            FILTER(
                'Table',
                'Table'[Index] = EARLIER('Table'[Index]) - 1
            )
        ),
        1,
        0
    )
)

Then, you can sum it up cumulatively based on the order of the Index column.

Change counted =
CALCULATE (
    SUM ( 'Table'[Change Counter dax] ),
    FILTER ( 'Table', 'Table'[Index] <= EARLIER ( 'Table'[Index] ) )
) + 1



The resultant output is as shown below which matches with your required output. 

DataNinja777_0-1723452230409.png

I have attached an example pbix file.  

Best regards,

Anonymous
Not applicable

Hi @Nietzsche,

 

In my testing, an index column is created in Power Query.

vdengllimsft_0-1723451582977.png

 

Then add a calculated column (ChangeCounter) to determine whether the value of the current row has changed from the previous row.

If there is a change, it is 1, and if not, it is 0.

vdengllimsft_1-1723451600380.png

ChangeCounter = 
VAR CurrentRow = 'Table'[Type]
VAR PreviousRow = 
    IF(
        'Table'[Index] = 1, 
        BLANK(), 
        CALCULATE(
            MAX('Table'[Type]), 
            FILTER(
                'Table', 
                'Table'[Index] = EARLIER('Table'[Index]) - 1
            )
        )
    )
RETURN
    IF(
        ISBLANK(PreviousRow) || CurrentRow <> PreviousRow, 
        1, 
        0
    )

 

Finally, add a calculated column (CumulativeChangeCounter) to show the sum of the number of changes in the current column compared to all previous columns.

vdengllimsft_2-1723451673462.png

CumulativeChangeCounter = 
CALCULATE(
    SUM('Table'[ChangeCounter]), 
    FILTER(
        ALL('Table'), 
        'Table'[Index] <= EARLIER('Table'[Index])
    )
)

 

Best Regards,

Dengliang Li

 

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

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.