Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Condition as if

Good Morning !!

it's ok guys?

i'm new here...

i need help, i have two columns, one with the same values and one with values i can change, i'm stuck creating an if,
Example:

Column1  Column2    If
5                233333    ok
5                233333    ok
1                233334    nok
5                233334    nok

1                233335    ok

5                233336    ok

5                233336    ok

I don't know how to create this condition...

  • See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlXSUTIyBgGlWB0MriGMa4Iqa4Iqa4oqa4bBjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column2"}, {{"Temp", each _, type table [Column1=nullable number, Column2=nullable number, Index=number]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let
    DummyTbl = [Temp],
    Result = Table.AddColumn(DummyTbl,"IF", each if List.AllTrue(List.Transform(DummyTbl[Column1],(x)=> x=DummyTbl[Column1]{0})) then "OK" else "NOK")
    in
    Result),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column2", "Temp"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column1", "Column2", "IF"}, {"Column1", "Column2", "IF"})
    in
        #"Expanded Custom"
  • Hi Anonymous ,

    According to your description, here's my solution. Please paste the code in a blank query to see the applied steps.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlXSUTIyBgGlWB0MriGMa4Iqa4Iqa4oqa4bBjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column2"}, {{"Count", each Table.RowCount(Table.Distinct(_)), Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "IF", each if [Count]>1 then "nok" else "ok"),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Column2"},#"Added Custom", {"Column2"},  "Added Custom", JoinKind.LeftOuter),
        #"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"IF"}, {"IF"})
    in
        #"Expanded Added Custom"

     

    Get the expected result.

    Or a calculated column is easier.

    Column =
    IF (
        CALCULATE (
            DISTINCTCOUNT ( 'Table'[Column1] ),
            ALLEXCEPT ( 'Table', 'Table'[Column2] )
        ) > 1,
        "nok",
        "ok"
    )
    

    If you have other confusions, please feel free to let me know.

    I attach my sample below for reference.

     

    Best Regards,
    Community Support Team _ kalyj

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

3 Replies

  • Hi Anonymous ,

    According to your description, here's my solution. Please paste the code in a blank query to see the applied steps.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlXSUTIyBgGlWB0MriGMa4Iqa4Iqa4oqa4bBjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column2"}, {{"Count", each Table.RowCount(Table.Distinct(_)), Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "IF", each if [Count]>1 then "nok" else "ok"),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Column2"},#"Added Custom", {"Column2"},  "Added Custom", JoinKind.LeftOuter),
        #"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"IF"}, {"IF"})
    in
        #"Expanded Added Custom"

     

    Get the expected result.

    Or a calculated column is easier.

    Column =
    IF (
        CALCULATE (
            DISTINCTCOUNT ( 'Table'[Column1] ),
            ALLEXCEPT ( 'Table', 'Table'[Column2] )
        ) > 1,
        "nok",
        "ok"
    )
    

    If you have other confusions, please feel free to let me know.

    I attach my sample below for reference.

     

    Best Regards,
    Community Support Team _ kalyj

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

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlXSUTIyBgGlWB0MriGMa4Iqa4Iqa4oqa4bBjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column2"}, {{"Temp", each _, type table [Column1=nullable number, Column2=nullable number, Index=number]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let
    DummyTbl = [Temp],
    Result = Table.AddColumn(DummyTbl,"IF", each if List.AllTrue(List.Transform(DummyTbl[Column1],(x)=> x=DummyTbl[Column1]{0})) then "OK" else "NOK")
    in
    Result),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column2", "Temp"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column1", "Column2", "IF"}, {"Column1", "Column2", "IF"})
    in
        #"Expanded Custom"
  • Hi Anonymous ,

    Is your problem solved?? If so, Would you mind accept the helpful replies as solutions? Then we are able to close the thread. More people who have the same requirement will find the solution quickly and benefit here. Thank you.

    Best Regards,
    Community Support Team _ kalyj