cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Regular Visitor

Group By 3 fields and Count another field based on Condition

Hi, i have a table with 4 fields and i want to Group By 3 of them and the 4th field has values of {"CM","PM","TOB"} and i want to Count each of them related to the Grouping. How can i apply this please ?

2 ACCEPTED SOLUTIONS
Super User III
Super User III

Hello

 

check out this approach. It uses Table.Group and a special function to count your specific items.

Use this variable to input your items to be counted

ItemsToCount = {"CM", "PM", "TOB"},

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSgWNnX6VYHVShAHKFQvydMMSg5icBmTAcgCkE1BmTB9OOJkGMfpAtsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Grouped Rows" = Table.Group
    (
        #"Changed Type", 
        {"Column1", "Column2", "Column3"}, 
        {
            {
                "Count", 
                (tbl)=>
                let 
                    ItemsToCount = {"CM", "PM", "TOB"},
                    CountItems = Text.Combine(List.Transform(ItemsToCount, (trans)=> trans &": " & Text.From(List.Count(List.Select(tbl[Column4],each _ = trans)))), ", ")
                in 
                    CountItems
            }
        }
    )
in
    #"Grouped Rows"

 

Jimmy801_0-1605094628897.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

Community Support
Community Support

Hi, @Mamr 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

g1.png

 

You may create a measure as below.

Result = 
var cm = 
COALESCE(
    COUNTROWS(
        FILTER(
            'Table',
            [WoType]="CM"
        )
    ),0
)
var pm = 
COALESCE(
    COUNTROWS(
        FILTER(
            'Table',
            [WoType]="PM"
        )
    ),0
)
var tob = 
COALESCE(
    COUNTROWS(
        FILTER(
            'Table',
            [WoType]="TOB"
        )
    ),0
)
return
"CM:"&cm&" PM:"&pm&" TOB:"&tob

 

Result:

g2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

5 REPLIES 5
Community Support
Community Support

Hi, @Mamr 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

g1.png

 

You may create a measure as below.

Result = 
var cm = 
COALESCE(
    COUNTROWS(
        FILTER(
            'Table',
            [WoType]="CM"
        )
    ),0
)
var pm = 
COALESCE(
    COUNTROWS(
        FILTER(
            'Table',
            [WoType]="PM"
        )
    ),0
)
var tob = 
COALESCE(
    COUNTROWS(
        FILTER(
            'Table',
            [WoType]="TOB"
        )
    ),0
)
return
"CM:"&cm&" PM:"&pm&" TOB:"&tob

 

Result:

g2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

Regular Visitor

Hi shawou459 , thank you for your reply, here is a screenshoot for my power bi desktop interface.Data.PNG

@Mamr , DAX is born for such a statistical job. It's only a matter of several drag-n-drop into a matrix viz. It takes 10 seconds only; then you can sit back and relax...

Screenshot 2020-11-11 142043.png

Super User III
Super User III

Hello

 

check out this approach. It uses Table.Group and a special function to count your specific items.

Use this variable to input your items to be counted

ItemsToCount = {"CM", "PM", "TOB"},

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSgWNnX6VYHVShAHKFQvydMMSg5icBmTAcgCkE1BmTB9OOJkGMfpAtsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Grouped Rows" = Table.Group
    (
        #"Changed Type", 
        {"Column1", "Column2", "Column3"}, 
        {
            {
                "Count", 
                (tbl)=>
                let 
                    ItemsToCount = {"CM", "PM", "TOB"},
                    CountItems = Text.Combine(List.Transform(ItemsToCount, (trans)=> trans &": " & Text.From(List.Count(List.Select(tbl[Column4],each _ = trans)))), ", ")
                in 
                    CountItems
            }
        }
    )
in
    #"Grouped Rows"

 

Jimmy801_0-1605094628897.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

Resolver II
Resolver II

Hi @Mamr , could you post some sample data?

Helpful resources

Announcements
secondImage

Congratulations!

We are excited to announce the Power BI Super Users!

Microsoft Ignite

Microsoft Ignite with Arun Ulag

Featured Session Drive Data Culture with Power BI: Vision, Strategy & Roadmap. Register here https://myignite.microsoft.com #MSIgnite #PowerPlatform #Power BI ​

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors