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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Creating a counter in PowerBI based on one column

Hi all, I am trying to create a calculated column in PowerBI based on another column. There is a column "Shift_ID" that contains a number based on the shift I am looking at. However, dependent on the amount of entries this one might appear multiple times. I would like to create a counter that can give me the number of lines corresponding to that specific number, and that could also be applied as a filter in some of the reports. I know how to do it in Excel with a simpel if-statement (if(row-row[-1]=0;1+row[-1];1), however, that does not seem to apply to Power BI.

 

Any ideas?

 

I added the sample Shift_ID data below and the desired outcome in the "Counter"-column.

 

Shift_IDCounter
44537_031
44536_031
44526_031
44526_032
44526_033
44526_021
44526_022
44526_023
44526_011
44526_012
44526_013
44526_014
44525_011
44525_031
44525_032
44525_033
44525_021
44525_022
44525_023
44523_011
44523_021
44522_031
44522_021
44522_011
44519_011
1 ACCEPTED SOLUTION
Samarth_18
Community Champion
Community Champion

Hi @Anonymous ,

 

First create a index column from PQ then create a custom with below code:-

Counter_ = 
CALCULATE (
    COUNT ([Shift_ID] ),
    FILTER (
        ALL('Table'),
        [Index] <= EARLIER ( [Index] )
            && [Shift_ID] = EARLIER ( [Shift_ID] )
    )
)

 

Output:-

image.png

 

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @Samarth_18 ,

 

Thanks for this. This seems indeed to be what I want to do, however, I do get a Syntax error when adding this in the custom column. I am not really sure why.

 

The error I get:

Expression.SyntaxError: Token Literal expected.

 

I have checked and the table-name is indeed "Table", so that cannot be the issue.

 

Here a screenshot of the error: 

melissa2305_0-1646925686593.png

 

Any idea how to solve this?

@Anonymous Please create this column out of Power query not in the Power query.

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Samarth_18
Community Champion
Community Champion

Hi @Anonymous ,

 

First create a index column from PQ then create a custom with below code:-

Counter_ = 
CALCULATE (
    COUNT ([Shift_ID] ),
    FILTER (
        ALL('Table'),
        [Index] <= EARLIER ( [Index] )
            && [Shift_ID] = EARLIER ( [Shift_ID] )
    )
)

 

Output:-

image.png

 

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors