Forum Discussion

jcastr02's avatar
jcastr02
Post Prodigy
2 years ago
Solved

range of dates when a column changes

I have list of stores, but with eff_date (effective date) and end_dt(end date).  How can I achieve to only get range of dates when the DE_Support column Changes.  See desired result table on right side.  

DATA in PQ   Desired Result  
str_nbreff_dtend_dtDE_Support Str_nbreff_dtend_dtDE_Support
238/24/2023null0.15 238/24/2023 0.15
2312/1/20228/23/20230.45 2312/1/20228/23/20230.45
2310/29/202211/30/20220.15 232/26/202111/30/20220.15
239/8/202210/28/20220.15 231/8/20212/25/20211
232/11/20229/7/20220.15 235/2/20191/7/20210.15
231/28/20222/10/20220.15 4810/4/20185/1/20190.6
2311/5/20211/27/20220.15 483/6/201710/3/20180.55
239/10/202111/4/20210.15     
239/9/20219/9/20210.15     
232/26/20219/8/20210.15     
231/8/20212/25/20211     
2312/3/20201/7/20210.15     
236/6/202012/2/20200.15     
236/4/20206/5/20200.15     
233/20/20206/3/20200.15     
2310/18/20193/19/20200.15     
239/5/201910/17/20190.15     
235/24/20199/4/20190.15     
235/2/20195/23/20190.15     
4810/4/20185/1/20190.6     
486/6/201810/3/20180.55     
485/29/20186/5/20180.55     
485/3/20185/28/20180.55     
481/4/20185/2/20180.55     
4811/24/20171/3/20180.55     
489/22/201711/23/20170.55     
489/7/20179/21/20170.55     
485/4/20179/6/20170.55     
485/4/20179/6/20170.55     
483/6/20175/3/20170.55     


                   

  • Read about GroupKind.Local and its awesome power.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ndJJrsMgDADQu2RdxZiZs1S9QfV3vX+NwZiksPmrOPLzIIvn87DueBwZrAdrOP77vN/0MSeG4/XoAC1gBbZhJ9icflYGbBGGCM7Iz7VbgTwUkbxWNHLMLJDWCKdyKtgMpEahZrBVbHqVXo+twkt8V0USU3hf3UZFeY1QE+R1v8vV+dKGdVq3iRCHoT4S35GXRGyn+DV1lCK3RnQhrGtj4Qosa1Z4CKNakeTnqkJ7dpwp4LdIEqG9vCvyuQ3h8swKFUU17VDYuZPYnGFqFNoLxjwutUROh/H7WyCcN7Ibg/0CiQs2O9FCdiDsN0gLliRBFbhGAbyi+H/jNNGvoeb1BQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [str_nbr = _t, eff_dt = _t, end_dt = _t, DE_Support = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"str_nbr", Int64.Type}, {"eff_dt", type date}, {"end_dt", type date}, {"DE_Support", type number}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"str_nbr", "DE_Support"}, {{"eff_dt", each List.Min([eff_dt]), type nullable date}, {"end_dt", each List.Max([end_dt]), type nullable date}},GroupKind.Local)
    in
        #"Grouped Rows"

    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". Once you examined the code, replace the Source step with your own source.

4 Replies

  • Read about GroupKind.Local and its awesome power.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ndJJrsMgDADQu2RdxZiZs1S9QfV3vX+NwZiksPmrOPLzIIvn87DueBwZrAdrOP77vN/0MSeG4/XoAC1gBbZhJ9icflYGbBGGCM7Iz7VbgTwUkbxWNHLMLJDWCKdyKtgMpEahZrBVbHqVXo+twkt8V0USU3hf3UZFeY1QE+R1v8vV+dKGdVq3iRCHoT4S35GXRGyn+DV1lCK3RnQhrGtj4Qosa1Z4CKNakeTnqkJ7dpwp4LdIEqG9vCvyuQ3h8swKFUU17VDYuZPYnGFqFNoLxjwutUROh/H7WyCcN7Ibg/0CiQs2O9FCdiDsN0gLliRBFbhGAbyi+H/jNNGvoeb1BQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [str_nbr = _t, eff_dt = _t, end_dt = _t, DE_Support = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"str_nbr", Int64.Type}, {"eff_dt", type date}, {"end_dt", type date}, {"DE_Support", type number}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"str_nbr", "DE_Support"}, {{"eff_dt", each List.Min([eff_dt]), type nullable date}, {"end_dt", each List.Max([end_dt]), type nullable date}},GroupKind.Local)
    in
        #"Grouped Rows"

    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". Once you examined the code, replace the Source step with your own source.

  • lbendlin Thanks so much for your help.  It seems it doesn't capture one of the rows because there is no end date (and that sometimes happens)  see below in yellow.

     

    • lbendlin's avatar
      lbendlin
      Super User

      Please provide sample data that fully covers your issue.
      Please show the expected outcome based on the sample data you provided.