Forum Discussion

NumeroENAP's avatar
NumeroENAP
Helper III
6 years ago
Solved

COUNT IF in Power Query

Hi, 

 

I want to do a personnalized column that could add up every answers in each colomn to get :

  • all the «Yes» answers,
  • all the «No» answers,
  • all the «N/A» answers.

 

I tried something like that (only for the «yes» column»), but that didn't worked : 

 

= ({[Question1], each "Yes"} + {[Question2], each "Yes"} + {[Question3], each "Yes"})

 

Thank you

  • dax's avatar
    dax
    6 years ago

    Hi NumerENAP

    You could try below M code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCixNLS7JzM8zVNJRikwthpN++UqxOghpI5CQviMuaWM03SAyNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type 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}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
        #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Column1", "Value"}, {{"Count", each Table.RowCount(_), type number}}),
        #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Value]), "Value", "Count", List.Sum),
        #"Merged Queries" = Table.NestedJoin(#"Pivoted Column", {"Column1"},#"Changed Type" , {"Column1"}, "Pivoted Column", JoinKind.LeftOuter),
        #"Expanded Pivoted Column" = Table.ExpandTableColumn(#"Merged Queries", "Pivoted Column", {"Column2", "Column3", "Column4"}, {"Pivoted Column.Column2", "Pivoted Column.Column3", "Pivoted Column.Column4"})
    in
        #"Expanded Pivoted Column"

    If you just want to bold result in your result, you could try below M code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCixNLS7JzM8zVNJRikwthpN++UqxOghpI5CQviMuaWM03SAyNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type 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}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
        #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Column1", "Value"}, {{"Count", each Table.RowCount(_), type number}}),
        #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Value]), "Value", "Count", List.Sum),
        #"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,{"Yes", "No", "N/A"})
       
      in   #"Replaced Value"

    Best Regards,
    Zoe Zhi

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

4 Replies

    • NumeroENAP's avatar
      NumeroENAP
      Helper III

      This is an exemple of what I want (I want what's in bold) :

       

      Column1Column2Column3Column4Nbof_yesNbof_noNbof_NA
      Question1YesYesNo210
      Question2N/AYesNo111
      Question3YesYesYes300
      • dax's avatar
        dax
        Community Support

        Hi NumerENAP

        You could try below M code

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCixNLS7JzM8zVNJRikwthpN++UqxOghpI5CQviMuaWM03SAyNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type 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}}),
            #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
            #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Column1", "Value"}, {{"Count", each Table.RowCount(_), type number}}),
            #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Value]), "Value", "Count", List.Sum),
            #"Merged Queries" = Table.NestedJoin(#"Pivoted Column", {"Column1"},#"Changed Type" , {"Column1"}, "Pivoted Column", JoinKind.LeftOuter),
            #"Expanded Pivoted Column" = Table.ExpandTableColumn(#"Merged Queries", "Pivoted Column", {"Column2", "Column3", "Column4"}, {"Pivoted Column.Column2", "Pivoted Column.Column3", "Pivoted Column.Column4"})
        in
            #"Expanded Pivoted Column"

        If you just want to bold result in your result, you could try below M code

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCixNLS7JzM8zVNJRikwthpN++UqxOghpI5CQviMuaWM03SAyNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type 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}}),
            #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
            #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Column1", "Value"}, {{"Count", each Table.RowCount(_), type number}}),
            #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Value]), "Value", "Count", List.Sum),
            #"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,{"Yes", "No", "N/A"})
           
          in   #"Replaced Value"

        Best Regards,
        Zoe Zhi

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