cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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

Accepted Solutions
Super User II
Super User II

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]))

 


______________


Has this post solved your problem? Please mark it as a 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.


I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 


excelwithallison.com

View solution in original post

3 REPLIES 3
Highlighted
Community Champion
Community Champion

@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





Super User II
Super User II

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]))

 


______________


Has this post solved your problem? Please mark it as a 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.


I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 


excelwithallison.com

View solution in original post

Highlighted

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

 

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Community Conference

Microsoft Power Platform Communities

Check out the Winners!

secondImage

Create an end-to-end data and analytics solution

Learn how Power BI works with the latest Azure data and analytics innovations at the digital event with Microsoft CEO Satya Nadella.

Top Solution Authors
Top Kudoed Authors