server-client-python
server-client-python copied to clipboard
View Filters with date ranges?
We have several Views containing a date range filter. How do we construct a CSVRequestOptions object that properly specifies the start and end timestamps for the range with calls to vf? What string format should the dates be in?
We've tried separating the start and end times in ISO format by a comma (as we seem to be able to do in other view filters with their simpler values), e.g. vf("created", "2019-03-20T12:00:00Z,2019-03-20T18:00:00Z"), to no avail. Even specifying a single date seems to have no affect on the returned results.
Are there plans to add this functionality?
I have the same question, I'm trying to get some views with a date range but I don't if it's possible.
We ended up setting up Parameters in the View, which can be set to date values with vf(). Then you can create a separate boolean Calculated Field that uses one or more Parameters in its expression. Finally, you can change your filter to use that Calculated Field. That way, your call to vf() affects the view filter in the way you want.
@knightcode ok so if Tableau sees the Parameter as the following:
globalFieldName: [Parameters].[End Date (copy)_5667428292300549]
valueString: 2/16/2021
useUsLocale: false
In the Web UI.. how do you suppose that might translate. Unfortunately my users only have read only access to views output (no edit)
I have tried End Date, 'End Date (copy), End Date (copy)_5667428292300549, [Parameters].[End Date (copy)_5667428292300549]` as the following options being used by a view csv populate request:
csv_options = tsc.CSVRequestOptions()
csv_options.vf("[Parameters].[End Date (copy)_5667428292518678529]", "2/16/2021")
I fear, based on the JSON seen using the Tableau UI, that the format being passed here is ultimately incorrect for the date.
Resolved on my end. The set parameters JSON response clarified things (from the Web UI) and showed that there is a value alias of 'YYYY-MM-DD' instead. I am able to filter on date now!
Now how the heck would I do a range filter update? That seems to be handled by a completely different codepath @ Tableau
For filtering date with ranges, I learnt that you could pass the dates separated as commas as a hack get the desired results. Another way to put it is: if you want to filter data from today and yesterday, you pass today's and yesterday's date separated by a comma.
So, here's a small function I wrote which will create the desired date range as a string which you pass as a filter.
def generate_dates(from_date, to_date):
date_format = "%Y-%m-%d"
from_date = datetime.strptime(from_date, date_format)
to_date = datetime.strptime(to_date, date_format)
dates = []
current_date = from_date
while current_date <= to_date:
dates.append(current_date.strftime(date_format))
current_date += timedelta(days=1)
return ",".join(dates)
from_date = "2022-08-10"
to_date = "2023-10-15"
dates = generate_dates(from_date, to_date)
csv_req_option = TSC.CSVRequestOptions()
csv_req_option.vf('Date Submitted', dates)
The only caveat is that the server has a request length limit gets exceeded on bigger ranges.