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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Byte_me
Frequent Visitor

How to create a calculated column that will count occurrences per categories?

Hi PowerBi community!
I have particual issue. Each month we generate material consumption report. For each product sometimes we have doubled material of the same category (due to ad-hoc changes our system shows that we consumed two caps for one bottle, obviously it's not possible, but it happens if we have to quickly use another cap than orginaly designed).

I would like to create a calculated coulumn that for each product ID would flag the duplications (the material ID is different, but it is the same material type). Bascially I need to count amount of occurences of string in "material type" column in group "product ID" that is limited by city and month. 

MonthCityProduct IDMaterial IDMaterial typeOccurence
1A1111111112342344Cap2
1A1111111121321321Bottle1
1A1111111112342344Cap2
1A1111111124534666Label1
1B1111111112354356Case2
2B1111111112342344Cap1
2B1111111124534666Label1
2B1111111112354356Case2
2A222222223242355Bottle1
2A222222223453456Cap1

 

Kind Regards

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

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.

 

mahoneypat_0-1662866544565.png

 

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

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
mahoneypat
Microsoft Employee
Microsoft Employee

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.

 

mahoneypat_0-1662866544565.png

 

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

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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. 

Byte_me
Frequent Visitor

It works! Thank you so much! How I could turn that into PowerQuery langugage so it would become a static "flag" in my data base instead of dynamic calculation? 

tamerj1
Super User
Super User

Hi @Byte_me 

please use

=
COUNTROWS (
    CALCULATETABLE (
        'Table'[Material type],
        ALLEXCEPT (
            'Table',
            'Table'[Month],
            'Table'[City],
            'Table'[Product ID],
            'Table'[Material Type]
        )
    )
)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.