https://dagster.io/ logo
#ask-community
Title
# ask-community
n

Nicholas Pezolano

02/16/2023, 6:48 PM
Is there a way to get the previous partition_key from context in an asset?
👀 1
o

owen

02/16/2023, 7:12 PM
hi @Nicholas Pezolano -- if you're using a time-based partitions definition (e.g. DailyPartitionsDefinition) then you can do something like
Copy code
@asset(partitions_def=my_partitions)
def my_asset(context):
    current_partition_window = context.partition_time_window
    previous_partition_window = my_partitions.get_prev_partition_window(current_partition_window)
this will give you a TimeWindow object which will have start and end datetime properties indicating the time range of that partition window. If you want to turn that into a partition key, you can do
my_partitions.get_partition_key_range_for_time_window(previous_partition_window).start
D 1
n

Nicholas Pezolano

02/16/2023, 8:25 PM
Copy code
previous_partition_window = edi_partitions_def.get_prev_partition_window(current_partition_window)
  File "C:\Users\n\anaconda3\lib\site-packages\dagster\_core\definitions\time_window_partitions.py", line 346, in get_prev_partition_window
    prev_window = next(windows_iter)
  File "C:\Users\n\anaconda3\lib\site-packages\dagster\_core\definitions\time_window_partitions.py", line 576, in _reverse_iterate_time_windows
    end_timestamp = pendulum.instance(end, tz=self.timezone).timestamp()
  File "C:\Users\n\anaconda3\lib\site-packages\pendulum\__init__.py", line 174, in instance
    raise ValueError("instance() only accepts datetime objects.")
I got the above error when I tried that,
current_partition_window
is indeed returning a
TimeWindow
object
TimeWindow(start=DateTime(2023, 2, 14, 18, 15, 0, tzinfo=Timezone('America/New_York')), end=DateTime(2023, 2, 15, 18, 15, 0, tzinfo=Timezone('America/New_York')))
o

owen

02/16/2023, 9:21 PM
ah that's pretty odd -- what version of
pendulum
are you on? this should theoretically work regardless of version but just want to narrow down the possibilities
n

Nicholas Pezolano

02/16/2023, 9:34 PM
I'm on 2.1.2 with pendulum, is it because I'm formatting my partition?
Copy code
DailyPartitionsDefinition(start_date="2023-01-12",  
                          fmt='%Y-%m-%d',
                          timezone='America/New_York', 
                          minute_offset=30,
                          hour_offset=16)
o

owen

02/16/2023, 9:59 PM
ah my bad, I had a typo in the original code -- it should be
previous_partition_window = my_partitions.get_prev_partition_window(current_partition_window.start)
D 1
n

Nicholas Pezolano

02/16/2023, 10:03 PM
Thanks
63 Views