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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mhendel
Helper III
Helper III

add a counter based on values in another column

Hello,

 

I have a query-table which contains this : 

 

EmployeePhase
MikePhase 1
MikePhase 3
MikePhase 4
MikePhase 6
MikePhase 8
MikePhase 9
PatrickPhase 1
PatrickPhase 2
PatrickPhase 5
PatrickPhase 4
SophiaPhase 3
SophiaPhase 5
SophiaPhase 7
SophiaPhase 8
AnniePhase 2
AnniePhase 3
AnniePahse 5
AnniePhase 6
AnniePhase 7
Anniephase 8

 

I would like to add a column which is a sort of counter : 

 

EmployeePhaseCounter
MikePhase 11
MikePhase 32
MikePhase 43
MikePhase 64
MikePhase 85
MikePhase 96
PatrickPhase 11
PatrickPhase 22
PatrickPhase 53
PatrickPhase 44
SophiaPhase 31
SophiaPhase 52
SophiaPhase 73
SophiaPhase 84
AnniePhase 21
AnniePhase 32
AnniePahse 53
AnniePhase 64
AnniePhase 75
Anniephase 86

 

For each row you have the same employee, it increments the counter by 1.  Otherwise, it begins again at 1.  I hope i am clear here ;-).

 

Can someone help me ?

 

Thanks,

 

Michael

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

Looks like you're wanting to do this within the current sort of your table? Careful as the sort order may not be as you expect, do you have a Date or Index column that determines the sort? If so you can use RANKX

Or you could get the count by using EARLIER, but you still need a way to tell Power BI which rows are first, second, third, etc.

If you don't have an index or ranking column, just add on in Power Query:
Home > Transform Data > Add Column > Index > Close & Apply

Then you can use EARLIER to add a column in DAX:
Column = COUNTROWS(FILTER(ALL(Table), Table[Employee]=EARLIER(Table[Employee]) && Table[Index] >=EARLIER(Table[Index]))

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

3 REPLIES 3
AllisonKennedy
Super User
Super User

Looks like you're wanting to do this within the current sort of your table? Careful as the sort order may not be as you expect, do you have a Date or Index column that determines the sort? If so you can use RANKX

Or you could get the count by using EARLIER, but you still need a way to tell Power BI which rows are first, second, third, etc.

If you don't have an index or ranking column, just add on in Power Query:
Home > Transform Data > Add Column > Index > Close & Apply

Then you can use EARLIER to add a column in DAX:
Column = COUNTROWS(FILTER(ALL(Table), Table[Employee]=EARLIER(Table[Employee]) && Table[Index] >=EARLIER(Table[Index]))

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Fowmy
Super User
Super User

@mhendel 

Paste the below code in a blank Query in Power Query and check the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc65CoAwEEXRX5GpbdwSLS0sBcEyWAQJJLgFtfHvBUUhM9MeHryrFDSLn7fLGIihs/owMMQKWjf9ECWUMko5JUGppFQ91Olzd+OETrGmrBasvkH95q3TKBxhwaHk8M2v19UZ1BRaFpq2/0u4E4zJwPz3O9w=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Grouped Rows" = Table.Group(#"Promoted Headers", {"Employee"}, {{"Count", each _, type table [Employee=nullable text, Phase=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Counter", 1, 1)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Employee", "Phase", "Counter"}, {"Employee", "Phase", "Counter"})
in
    #"Expanded Custom"

Fowmy_0-1600599394031.png

 

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn





Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thanks a lot, you've made my day!!

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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