Forum Discussion

Patv's avatar
Patv
Icon for Helper II rankHelper II
4 years ago
Solved

Add new column to table in power Query with may be using If-ELSE codition

Hi All, 

I have a simple requirement to create a new column  ( dynemically changes every year) to  one of the existing table.

example: I have two one column in table called Year_id and I want to add new column ( e.g. called new_column) where it shows 1 where year_id is current year and current year+1. so for current year 2022, new column shows 1 for 2022 and 2023, but  Next Year ( in 2023) the new column shows 1 for 2023 and 2024, and so on.. hence I don't have to change the flag every year....

 

I assume it's simple in to write DAX but I am having hardtime so need help here. Thank you in advance!

  • Patv's avatar
    Patv
    4 years ago

    Thank you AlexisOlson. That is what I was looking for. I did not know the DAX formula. It worked. 

    my formula is like below now and it worked. Thank you again!

     

    Column = IF ( TIME_DIMENSION_YEAR[YEAR_ID] IN { ,Year(TODAY())+1, Year(TODAY()) }, 1, 0 )

2 Replies

  • You can use TODAY to get the current date and, thus, the current year.

     

    IF ( YEAR ( TODAY() ) IN { Table1[Year_id], Table1[Year_id] - 1 }, 1, 0 )

     

    Edit: Sorry. ^This^ is for a calculated column in DAX, not the Power Query language M but you can write similar code there.

    if List.Contains({[Year_id], [Year_id]+1}, Date.Year(DateTime.LocalNow())) then 1 else 0

     

    • Patv's avatar
      Patv
      Icon for Helper II rankHelper II

      Thank you AlexisOlson. That is what I was looking for. I did not know the DAX formula. It worked. 

      my formula is like below now and it worked. Thank you again!

       

      Column = IF ( TIME_DIMENSION_YEAR[YEAR_ID] IN { ,Year(TODAY())+1, Year(TODAY()) }, 1, 0 )