Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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 plea...
  • Jimmy801's avatar
    5 years ago

    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"

     

     

    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

     

  • v-alq-msft's avatar
    5 years ago

    Hi, Anonymous 

     

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

    Table:

     

    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:

     

    Best Regards

    Allan

     

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