Forum Discussion

revisr's avatar
revisr
New Member
2 years ago
Solved

If statement too many arguments

I am brand new to DAX. In one of my lessons I was told to enter an IF statement:

 

Customer Age =
IF(
    FORMAT('Customer'[Birth Date], "mmdd") <= FORMAT(TODAY(), "mmdd"),
    DATEDIFF('Customer'[Birth Date], TODAY(), YEAR),
    DATEDIFF('Customer'[Birth Date], TODAY(), YEAR),-1)
It is telling me I have more than 3 arguments. My column has error. Where wm I going wrong?
  • Hello revisr,

     

    If statement syntax is IF(<logical_test>, <value_if_true>[, <value_if_false>])

    IF function (DAX) - DAX | Microsoft Learn

    Therefore, you code should be like this:


    IF(
       FORMAT('Customer'[Birth Date]"mmdd") <= FORMAT(TODAY(), "mmdd"),

       DATEDIFF('Customer'[Birth Date]TODAY(), YEAR),

           IF(

              FORMAT('Customer'[Birth Date]"mmdd") > FORMAT(TODAY(), "mmdd"),

             DATEDIFF('Customer'[Birth Date], TODAY(), YEAR),

             1

               )

    )

     

    Kind Regards,
    Gรถkberk UzuntaลŸ

    ๐Ÿ“Œ If this post helps, then please consider Accepting it as a solution and giving Kudos โ€” it helps other members find answers faster!

    ๐Ÿ”— Stay Connected:
    ๐Ÿ“˜ Medium |
    ๐Ÿ“บ YouTube |
    ๐Ÿ’ผ LinkedIn |
    ๐Ÿ“ท Instagram |
    ๐Ÿฆ X |
    ๐Ÿ‘ฝ Reddit |
    ๐ŸŒ Website |
    ๐ŸŽต TikTok |

     

     

3 Replies

  • Hello revisr,

     

    If statement syntax is IF(<logical_test>, <value_if_true>[, <value_if_false>])

    IF function (DAX) - DAX | Microsoft Learn

    Therefore, you code should be like this:


    IF(
       FORMAT('Customer'[Birth Date]"mmdd") <= FORMAT(TODAY(), "mmdd"),

       DATEDIFF('Customer'[Birth Date]TODAY(), YEAR),

           IF(

              FORMAT('Customer'[Birth Date]"mmdd") > FORMAT(TODAY(), "mmdd"),

             DATEDIFF('Customer'[Birth Date], TODAY(), YEAR),

             1

               )

    )

     

    Kind Regards,
    Gรถkberk UzuntaลŸ

    ๐Ÿ“Œ If this post helps, then please consider Accepting it as a solution and giving Kudos โ€” it helps other members find answers faster!

    ๐Ÿ”— Stay Connected:
    ๐Ÿ“˜ Medium |
    ๐Ÿ“บ YouTube |
    ๐Ÿ’ผ LinkedIn |
    ๐Ÿ“ท Instagram |
    ๐Ÿฆ X |
    ๐Ÿ‘ฝ Reddit |
    ๐ŸŒ Website |
    ๐ŸŽต TikTok |

     

     

    • Wilson_'s avatar
      Wilson_
      Memorable Member

      Hi revisr,

       

      Looks like the formula uzuntasgokberk provided was unnecessarily complex. You were on the right track with your original measure. You just had an extra comma in your measure, before the -1 at the end. The comma made Power BI think the -1 was a fourth argument in your IF function. Hopefully that makes sense.

       

      You had:

       

      IF (
          FORMAT ( 'Customer'[Birth Date], "mmdd" ) <= FORMAT ( TODAY(), "mmdd" ),
          DATEDIFF ( 'Customer'[Birth Date], TODAY(), YEAR ),
          DATEDIFF ( 'Customer'[Birth Date], TODAY(), YEAR ), - 1
      )

       

       

      However, what you meant was:

       

      IF (
          FORMAT ( 'Customer'[Birth Date], "mmdd" ) <= FORMAT ( TODAY(), "mmdd" ),
          DATEDIFF ( 'Customer'[Birth Date], TODAY(), YEAR ),
          DATEDIFF ( 'Customer'[Birth Date], TODAY(), YEAR ) - 1
      )