Forum Discussion
How to create a calculated column that will count occurrences per categories?
- 4 years ago
This simplest way to do this in your query would be with a standard Group By step, where you group on Material ID, and City (and Month or other colums if needed too). You can then summarize on the count of rows for each grouping to get your desired column. Note that this would eliminate the duplicate rows, but show you the original # of replicates in the new column. If you want to keep all the original rows, just also add an "All Rows" aggregation and then expand it, as shown below.
Just create a blank query, open the Advanced Editor, and replace the code there with the below, to see how it works.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZDLCoAgEEX/xbUb5+G+2vYH4sKgXVCQ/0+jSVBN0DDCXRzuUUMwzljTyXFtSgQkWZI4pM1Eq1LgsK7Efs15mb/An3XESN57iWOa5uXi+lcbE7KvbfspBRV7ShVKleptqrS8AdpIRBAn8/1HNK5oW5vcLR4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, City = _t, #"Product ID" = _t, #"Material ID" = _t, #"Material type" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", Int64.Type}, {"City", type text}, {"Product ID", Int64.Type}, {"Material ID", Int64.Type}, {"Material type", type text}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Material ID"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"AllRows", each _, type table [Month=nullable number, City=nullable text, Product ID=nullable number, Material ID=nullable number, Material type=nullable text]}}), #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Month", "Product ID", "Material type"}, {"Month", "Product ID", "Material type"}) in #"Expanded AllRows"Pat
This simplest way to do this in your query would be with a standard Group By step, where you group on Material ID, and City (and Month or other colums if needed too). You can then summarize on the count of rows for each grouping to get your desired column. Note that this would eliminate the duplicate rows, but show you the original # of replicates in the new column. If you want to keep all the original rows, just also add an "All Rows" aggregation and then expand it, as shown below.
Just create a blank query, open the Advanced Editor, and replace the code there with the below, to see how it works.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZDLCoAgEEX/xbUb5+G+2vYH4sKgXVCQ/0+jSVBN0DDCXRzuUUMwzljTyXFtSgQkWZI4pM1Eq1LgsK7Efs15mb/An3XESN57iWOa5uXi+lcbE7KvbfspBRV7ShVKleptqrS8AdpIRBAn8/1HNK5oW5vcLR4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, City = _t, #"Product ID" = _t, #"Material ID" = _t, #"Material type" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", Int64.Type}, {"City", type text}, {"Product ID", Int64.Type}, {"Material ID", Int64.Type}, {"Material type", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Material ID"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"AllRows", each _, type table [Month=nullable number, City=nullable text, Product ID=nullable number, Material ID=nullable number, Material type=nullable text]}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Month", "Product ID", "Material type"}, {"Month", "Product ID", "Material type"})
in
#"Expanded AllRows"
Pat
- Byte_me4 years agoFrequent Visitor
Thank you so much Pat! I will build the query over next two days and come back to report results and also to accept as solution in case it will work for future help seekers.