Forum Discussion

Hadi14's avatar
Hadi14
Regular Visitor
1 year ago
Solved

countrow in power query whitout help table in M language

I have a table that contains national codes. I want to add a column to this table without an auxiliary table that displays the number of occurrences of each national code.

  • = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))

     

     

    To paste into Advanced Editor:

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)),{"name","code"}),
    AddCustom = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))
    in
    AddCustom

     

     

6 Replies

  • Chewdata's avatar
    Chewdata
    Icon for Responsive Resident rankResponsive Resident

    Hey,

    Can you provide a workable sample of your data (not a screenshot). Please do not share sensative data.

  • Hadi14's avatar
    Hadi14
    Regular Visitor

    yes

     

    namecode
    ali2
    reza3
    john2
    sara2

     

    result:

    namecode 
    ali23
    reza31
    john23
    sara23
    • p45cal's avatar
      p45cal
      Icon for Solution Supplier rankSolution Supplier

      = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))

       

       

      To paste into Advanced Editor:

      let
      Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)),{"name","code"}),
      AddCustom = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))
      in
      AddCustom

       

       

  • Chewdata's avatar
    Chewdata
    Icon for Responsive Resident rankResponsive Resident

    I am not really sure what your question is. But let's say you have the following data:

    You can select both columns, right click and choose group by:

    It then standard gives you a count of the rows, counting how often each combination of name and code.

     

    result:


    the code:

    let
        Source = YOURDATA,
        #"Grouped Rows" = Table.Group(Source, {"name", "code"}, {{"Count", each Table.RowCount(_), Int64.Type}})
    in
        #"Grouped Rows"

     



  • dufoq3's avatar
    dufoq3
    Icon for Community Champion rankCommunity Champion

    Hi Hadi14, another solution:

     

    Output

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, code = _t]),
        CodeCount = [ codes = List.Buffer(Source[code]),
        occur = List.Transform(List.Distinct(codes), (x)=> {x, List.Count(List.Select(codes, (y)=> y = x))}),
        tbl = Table.FromRows(occur, type table[code=Int64.Type, count=Int64.Type])
      ][tbl],
        Ad_Count = Table.Join(Source, "code", CodeCount, "code")
    in
        Ad_Count
  • Hi Hadi Jan,

    TO solve this problem, right click on the code column and select group by by the below setting.

     

    Then exapnd the all row column. below is the full solution

     

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, code = _t]),
        #"Grouped Rows" = Table.Group(Source, {"code"}, {{"Rows", each _, type table [name=nullable text, code=nullable text]}, {"Count", each Table.RowCount(_), Int64.Type}}),
        #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"name"}, {"name"})
    in
        #"Expanded Rows"