Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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

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

3 Replies

  • Anonymous 

    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"

     

    ________________________

    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





    • Anonymous's avatar
      Anonymous
      Not applicable

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

       

  • AllisonKennedy's avatar
    AllisonKennedy
    Icon for Community Champion rankCommunity Champion
    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]))