Saya pikir Anda dapat menggunakan kueri seperti ini:
SELECT *,
CASE
WHEN Active <> 0 AND
ISNULL(LAG(Active) OVER (PARTITION BY LocationCode ORDER BY YearMonth), 0) = 0 THEN 1
ELSE 0
END As New
FROM yourTable;