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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Mamr
Frequent 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
Jimmy801
Community Champion
Community Champion

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

v-alq-msft
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
v-alq-msft
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.

 

Mamr
Frequent Visitor

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

CNENFRNL
Community Champion
Community Champion

@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


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Jimmy801
Community Champion
Community Champion

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

 

shaowu459
Resolver II
Resolver II

Hi @Mamr , could you post some sample data?

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors