Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

filter the final output based on column_ mquery

hello, 

 

i have below condition to implemnt in power query, 

 

if source column contain source 1, final output should only have source 1,else source 2. cant be both.

idvar1var2var3Source
1101215source 1
2141414source 2
3111511source 2
4111815source 1
5111012source 1
6202015source 2
7131212source 2
8161516source 1
9111213source 1
10121913source 1

 

  • Hi Anonymous 

    Paste this M code in a blank query to see the steps:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0ABFGIMIUSBTnlxYlpyoYKsXqRCuBRU1QCKi8EVjeGCRqCNMKZqHIm8DlLbCaD9eFcASKvBlQwMgARiDrh5hvDhI1hrvfCF0ebKsZ3H1m6OZbwu03gpmEIo8UNpaYCmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, var1 = _t, var2 = _t, var3 = _t, Source = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"var1", Int64.Type}, {"var2", Int64.Type}, {"var3", Int64.Type}, {"Source", type text}}),
        hasSource1 = List.Contains(#"Changed Type"[Source], "source 1"),
        res = Table.SelectRows(#"Changed Type", each if hasSource1 then [Source]="source 1"  else [Source]="source 2")
    in
        res

     Please mark the question solved when done and consider giving kudos if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

1 Reply

  • AlB's avatar
    AlB
    Community Champion

    Hi Anonymous 

    Paste this M code in a blank query to see the steps:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0ABFGIMIUSBTnlxYlpyoYKsXqRCuBRU1QCKi8EVjeGCRqCNMKZqHIm8DlLbCaD9eFcASKvBlQwMgARiDrh5hvDhI1hrvfCF0ebKsZ3H1m6OZbwu03gpmEIo8UNpaYCmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, var1 = _t, var2 = _t, var3 = _t, Source = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"var1", Int64.Type}, {"var2", Int64.Type}, {"var3", Int64.Type}, {"Source", type text}}),
        hasSource1 = List.Contains(#"Changed Type"[Source], "source 1"),
        res = Table.SelectRows(#"Changed Type", each if hasSource1 then [Source]="source 1"  else [Source]="source 2")
    in
        res

     Please mark the question solved when done and consider giving kudos if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers