Create a group number that advances when value is found

amulder

New Member
Joined
Oct 28, 2016
Messages
20
I have the following data:

Event IDVehicleStatus Code
P160399541102DP
P160399541102AM
P160399541102DP
P160399541102AR
P160399541102AM
P160399541301DP
P160399541301AM
P160399551299DP
P160399561299DP
P160399561301DP

<tbody>
</tbody>

I would like to create a new value called Group Number that advances by 1 every time the Status Code of DP is encountered. The result should appear like the following:

Event IDVehicleStatus CodeGroup Number
P160399541102DP1
P160399541102AM1
P160399541102DP2
P160399541102AR2
P160399541102AM2
P160399541301DP3
P160399541301AM3
P160399551299DP4
P160399561299DP5
P160399561301DP6

<tbody>
</tbody>

I currently have the data within PowerPivot but could also do the calculation in Excel.

Thanks in advance for your help.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Power Pivot doesn't have a concept of "row order". All the data is loaded and processed in any order. The issue therefore in creating this column is i don't see any way how to determine the order before the new column is calculated. I would therefore do this either in the source, in Excel or maybe Power Query
 
Upvote 0
In Excel:
Code:
=COUNTIF(C$2:C2,"DP")

The following Power Query code was created this way
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event ID", type text}, {"Vehicle", Int64.Type}, {"Status Code", type text}, {"Group Number Excel", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Calculated Count" = List.NonNullCount(#"Added Index"[Index]),
    Group = List.Buffer(
                        List.Generate(() => [Counter = 0, Group = if #"Added Index"[#"Status Code"]{0} = "DP" then 1 else 0],
                                each [Counter] < #"Calculated Count", 
                                each [Counter = [Counter] + 1, Group = if #"Added Index"[#"Status Code"]{[Counter]} = "DP" then [Group] + 1 else [Group]], 
                                each [Group])
                        ),    
    #"Added Custom" = Table.AddColumn(#"Added Index", "Group Number PQ", each Group{[Index]}),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"
 
Upvote 0
Small correction in the Power Query Code (+1 added, see bold red):
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event ID", type text}, {"Vehicle", Int64.Type}, {"Status Code", type text}, {"Group Number Excel", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Calculated Count" = List.NonNullCount(#"Added Index"[Index]),
    Group = List.Buffer(
                        List.Generate(() => [Counter = 0, Group = if #"Added Index"[#"Status Code"]{0} = "DP" then 1 else 0],
                                each [Counter] < #"Calculated Count", 
                                each [Counter = [Counter] + 1, Group = if #"Added Index"[#"Status Code"]{[Counter] [COLOR=#FF0000][B]+ 1[/B][/COLOR]}[COLOR=#FF0000][/COLOR] = "DP" then [Group] + 1 else [Group]], 
                                each [Group])
                        ),    
    #"Added Custom" = Table.AddColumn(#"Added Index", "Group Number PQ", each Group{[Index]}),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"
 
Upvote 0
In Excel:
Code:
=COUNTIF(C$2:C2,"DP")
This code may cause an issue when adding rows to the table, because the range reference (C$2:Cn) will be automatically adjusted for the previous last row.

In order to avoid that issue, you can use the following in D2 and copy down
Code:
=SUM([@[Status Code]]="DP",OFFSET(D2,-1,0))
 
Last edited:
Upvote 0
Hi MarcelBeug :))
Think it is a simpler way ;)
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event ID", type text}, {"Vehicle", Int64.Type}, {"Status Code", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Grouped Rows" = Table.Group(#"Added Index", {"Status Code"}, {{"tbl", each Table.AddIndexColumn(_, "Group Number", 1, 1), type table}}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Status Code"}),
    #"Expanded {0}" = Table.ExpandTableColumn(#"Removed Columns", "tbl", {"Event ID", "Group Number", "Index", "Status Code", "Vehicle"}, {"Event ID", "Group Number", "Index", "Status Code", "Vehicle"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded {0}",{"Event ID", "Vehicle", "Status Code", "Group Number", "Index"}),
    #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Index", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index"})
in
    #"Removed Columns1"

Regards
 
Upvote 0
Hi Bill,

Well appreciated. As a matter of fact I'm studying Microsoft's PQFL (M) Reference thoroughly from top to bottom and coincidentally today I was about to cover " Table.Group" which is an important part of your solution. So I will examine your solution more thoroughly as well. I already studied "List.Generate" which I regard as a very powerful function and quite suitable for this topic.

Otherwise please note that your solution always starts with group number 1, even if the first row doesn't have Status Code DP. In such a case, my solution would start with group number 0.
I guess it's up to the topic starter to judge on which one is correct.

Regards,
Marcel
 
Upvote 0
Hi Marcel,
Usually, there is more than one way to skin a cat in PQ ;)
So, for this case i can also imagine something like these below :)
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event ID", type text}, {"Vehicle", Int64.Type}, {"Status Code", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Removed Duplicates" = Table.Distinct(#"Added Index", {"Status Code"})[[Status Code]],
    #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", (x) => Table.AddIndexColumn(Table.SelectRows(#"Added Index", each [Status Code] = x[Status Code]), "Group Number", 1, 1)),
    CombineTables = Table.Combine( #"Added Custom"[Custom]),
    #"Sorted Rows" = Table.Sort(CombineTables,{{"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"})
in
    #"Removed Columns"

or

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event ID", type text}, {"Vehicle", Int64.Type}, {"Status Code", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Indeks", 1, 1),
    HandWritten = Table.AddColumn(#"Added Index", "Group Number",(x) => List.Count(List.Select(List.Range(#"Added Index"[Status Code], 0, x[Indeks]), each  _ = x[Status Code]))),
    #"Removed Columns" = Table.RemoveColumns(HandWritten,{"Indeks"})
in
    #"Removed Columns"

Regards :)
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top