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
makarifgmmh
Regular Visitor

New column comparing two dates

Hi! I have a table like the following:

 

Report DateProduct CodeProduction Date
31/08/23ABC21/08/23
31/08/23XYZ11/08/23
31/08/23YZX21/08/23
30/09/23ABC20/09/23
30/09/23XYZ13/09/23
30/09/23MNO05/09/23
30/09/23EFG05/09/23

I need to add a column in Power Query. If the product is not in production anymore, I need to mark it as terminated. If the product was not in production I need to mark as New and otherwise existing. Can you please help me to write the code for the new column? Thanks in advance!

 

Here is the example:

 

Report DateProduct CodeProduction DateDescription
31/08/23ABC21/08/23Existing
31/08/23XYZ11/08/23Existing
31/08/23YZX21/08/23Terminated
30/09/23ABC20/09/23Existing
30/09/23XYZ13/09/23Existing
30/09/23MNO05/09/23

New

30/09/23EFG05/09/23

New

1 REPLY 1
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjbUN7DQNzJW0lFydHIGkkYwgVgdFNmIyCggaYhDNjIqAkOvgb6BJYrJMAE0WajJxthlff38gaSBKXZZVzd3FNlYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Report Date" = _t, #"Product Code" = _t, #"Production Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Report Date", type date}, {"Product Code", type text}, {"Production Date", type date}},"en-GB"),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Product Code"}, {{"Max Date", each List.Max([Report Date]), type nullable date}, {"Min Date", each List.Min([Report Date]), type nullable date}, {"Rows", each _, type table [Report Date=nullable date, Product Code=nullable text, Production Date=nullable date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Description", each if [Max Date]>[Min Date] then "Existing" else
if [Max Date]=List.Max(#"Grouped Rows"[Max Date]) then "New" else "Terminated"),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Added Custom", "Rows", {"Report Date", "Production Date"}, {"Report Date", "Production Date"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Rows",{"Product Code", "Report Date", "Production Date", "Description"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

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.

Top Solution Authors