Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

specific replacement for countif function in power query editor

Hello, I have a large table with respondents (teachers) in rows and many columns. One of the columns contains all the subjects that the teacher teaches (mostly separated by commas). There are about ...
  • v-kkf-msft's avatar
    4 years ago

    Hi Anonymous ,

     

    I create rows for each subject instead of separate columns. Then you can display the data using the matrix visual.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY7BasNADER/ZdmzDqVJPsBN7F5SCKQ3k4O8EbZgvYaVtuC/r3ZL6a2XYWA0ejOOvvPgDy8mH6gLragcBP48RtcljLuwwC1vE04cWXcYSgrKWxL/gNG/Wf/1ZNKnObIscMU0F5yppWcLjg1RhAMMHFeH6ek+KdIXi32BLms7vdQ19dFtMWSl988SsJJa3lt0OpjcA1MKBO+0raR5hwsqTigkzbm75hK0ZPoZONQJR5Oz+532X+PxDQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [teacher = _t, Age = _t, subjects = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"teacher", type text}, {"Age", Int64.Type}, {"subjects", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Split", each Splitter.SplitTextByDelimiter(",")([subjects])),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "CombineList", each List.Distinct( List.Combine(#"Added Custom"[Split]) )),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "IsContainSubject", each let CurSplit = [Split], CurComList = [CombineList]
    in 
    List.Generate(()=>[x=0,y=List.Contains([Split],List.Single( List.Range([CombineList], 0, 1))),w=1], each [w] <= 15,each [z=[y], x=[x]+1,y=List.Contains(CurSplit, List.Single(List.Range(CurComList, x, 1))),w=[w]+1
    ],
    each [y])),
        #"Added Custom3" = Table.AddColumn(#"Added Custom2", "output", each List.Zip({[CombineList],[IsContainSubject]})),
        #"Expanded output" = Table.ExpandListColumn(#"Added Custom3", "output"),
        #"Extracted Values" = Table.TransformColumns(#"Expanded output", {"output", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "output", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"output.1", "output.2"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"output.1", type text}, {"output.2", type logical}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"subjects", "Split", "CombineList", "IsContainSubject"})
    in
        #"Removed Columns"

    TO

     

     

    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
    Best Regards,
    Winniz
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.