Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Plump_Lil_Monk
Frequent Visitor

Custom Column: Conditional Value Return

Hi, I have a problem that I am trying to figure out.

I need to create a custom column that enters a value based on a high-low condition in another column.

 

I have a year column with 2024 and 2025 in it, next year these values will change to 2025 and 2026.

 

I need some M which will automatically identify the lowest value (current year) in my year column and then output it as 'Current Year' and Identify the highest value (next year) and output it, in the custom column, as 'Current Year +1'. 

 

Any help would be appreciated. 

 

How I would need it to look.

123                                         YearAny                           Current Not current
2025Current +1
2024Current
2024Current
2025Current +1
2025Current +1
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Plump_Lil_Monk ,

Please try this M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlGK1UFlmGIwsErFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
  let 
    currentYear = Date.Year(DateTime.LocalNow()),
    AddedCustom = if [Year] = currentYear then "Current" else "Current + " & Text.From([Year] - currentYear)
  in 
    AddedCustom)
in
    #"Added Custom"

Put all of it into the advanced editor, and the final output is below:

vjunyantmsft_0-1706751107269.png

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

View solution in original post

2 REPLIES 2
Plump_Lil_Monk
Frequent Visitor

Thanks for the help that worked.

 

But after playing around I found a slightly different way still using your method:

 

Instead of:

 

currentYear = Date.Year(DateTime.LocalNow()),
    AddedCustom = if [Year] = currentYear then "Current" else "Current + " & Text.From([Year] - currentYear)

I used:

    AddedCustom = if [Year] = Date.Year(DateTime.LocalNow()) then "Current" else "Current +"

 

Anonymous
Not applicable

Hi @Plump_Lil_Monk ,

Please try this M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlGK1UFlmGIwsErFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
  let 
    currentYear = Date.Year(DateTime.LocalNow()),
    AddedCustom = if [Year] = currentYear then "Current" else "Current + " & Text.From([Year] - currentYear)
  in 
    AddedCustom)
in
    #"Added Custom"

Put all of it into the advanced editor, and the final output is below:

vjunyantmsft_0-1706751107269.png

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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.