Forum Discussion

CleliaComes's avatar
CleliaComes
Helper I
5 years ago
Solved

If statement with double condition

Hi    I am learning Power Query and Power BI and I have some troubles using an if function with a double condition. A sample of the data below.   In column "Type rekening"  there is a label "kla...
  • PhilipTreacy's avatar
    PhilipTreacy
    5 years ago

    Hi CleliaComes 

    This query does what you are asking. Copy paste into PBI Advanced Query editor.  It contain a snapshot of your data as shown in your image.

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyivNyVHSgVGeeSWpRXmpCpllSToKBYlF2alFqXk6CkmJednZ+cUlOgqpJcl6QHUGSrE60UreOYl5JQjNMK6hobmesQVChYK+gk9qWWpRYl5yZmoRmnoMSV0zCz1TQ7BurLrQVBsbGFroWRiSa5uJqZ6ZpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Type rekening" = _t, Categorie = _t, #"Type rekening + categorie" = _t, #"Cash in - cash out" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Type rekening", type text}, {"Categorie", type text}, {"Type rekening + categorie", type text}, {"Cash in - cash out", type number}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Type rekening] = "Klant / Leverancier" and [#"Cash in - cash out"] > 0 then "Klant" else if [Type rekening] = "Klant / Leverancier" and [#"Cash in - cash out"] < 0 then "Leverancier" else null)
    in
        #"Added Custom"

     

     

    If you want to just add the single line of code to your file to create the Custom Column, this is the line to enter into the Create Custom Column dialog box

     

    if [Type rekening] = "Klant / Leverancier" and [#"Cash in - cash out"] > 0 then "Klant" else if [Type rekening] = "Klant / Leverancier" and [#"Cash in - cash out"] < 0 then "Leverancier" else null

     

     

    NOTE Your data shows the text string "Klant / Leverancier" but the code you tried to enter for the if statement showed "Klant/Leverancier" - notice the lack of spaces between the words.  Make sure you are checking for the correct text. My query checks for the text as shown in your data.

    Phil


    If I answered your question please mark my post as the solution.
    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.