Bring your best ideas to the AI Use Case Contest! Enter to win 40 hours of expert engineering support and bring your vision to life using the powerful combination of Alteryx + AI. Learn more now, or go straight to the submission form.
Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

How to create a while loop equivalent in Alteryx iterating through a range of dates?

hermanhmcheung
5 - Atom

Hi All,

 

I have a SQL table that looks like this:

Member IDGym Plan TypePlan Start DatePlan End Date
A1234Monthly Plan2022-07-112022-08-10
B0057Annual Plan2023-01-012023-12-31

 

I have been running the below SQL script to calculate the number of gym members with expiring subscription on each day for the past 30 days. I would like to automate this through Alteryx, but I don't know how. Well noted that Alteryx SQL editor does not allow the creation of temp table, and for my personal reason, I cannot create a stored procedure in SQL to solve this. Is there any other ways I can automate this daily report in Alteryx? Thanks!

 

 

 

  DECLARE @AsOfDate DATE = DATEADD(DAY, -1, GETDATE())
  DECLARE @ReportEndDate DATE =  DATEADD(DAY, -29, DATEADD(DAY, -1, GETDATE()))

  
  CREATE TABLE #temp (
	[Gym Plan Type] VARCHAR(255),
	[As of Date] DATE,
	[Expired] INT,
  )

  WHILE (@AsOfDate >= @ReportEndDate)
  BEGIN
	INSERT INTO #temp
	SELECT
	 [Gym Plan Type]
	,@AsOfDate AS [As of Date]
	,count(DISTINCT(CASE WHEN [Plan End Date] = @AsOfDate THEN [Member ID] END)) AS [Expired]
  FROM [DM_USER].[dbo].[Gym Subscription List]
  GROUP BY [Gym Plan Type]

  SET @AsOfDate = DATEADD(DAY, -1, @AsOfDate)
  END

  SELECT
	*
  FROM #temp

  DROP TABLE #temp

 

 

 

5 REPLIES 5
apathetichell
20 - Arcturus

you're using in-db tools? this is moderately straight forward with dynamic input in-db in a batch macro. You would have:

 

 

	SELECT
	 [Gym Plan Type]
	,@AsOfDate AS [As of Date]
	,count(DISTINCT(CASE WHEN [Plan End Date] = @AsOfDate THEN [Member ID] END)) AS [Expired]
  FROM [DM_USER].[dbo].[Gym Subscription List]
  GROUP BY [Gym Plan Type]

You would have the Query as a plane text field called query. You would use a control parameter/action tool to connect in and change the value of Gym Plan Type (I assume you take the output for each gym plan type or something). You would use a formula with datetimenow() (or some variation of it) to edit the date in your text query. You then feed the query and the name of your connection into Dynamic Input In-DB.

 

This all occurs in your batch macro.

 

Note - I'd use something other than [] in my dummy query to denote my variables. Using a specific plan type/membership type and a specific day would allow for easier testing - and the action tool/formula tool can update those just as easily.

 

 

 

 

hermanhmcheung
5 - Atom

Thanks for your prompt reply!

 

I followed your instructions to feed the query, date variable, and connection name into the Dynamic Input In-DB tool. However, I still don't understand how I can achieve the "looping" part and get the same result for the not only today, but also for the past 30 days.

apathetichell
20 - Arcturus

you are looking for every day in thepast 30 to have a 30 day export? You do this outside the macro and pass those dates in via batch macro... ie:

 

gymplan 1... day 1.... gymplan 1... day 30.

 

so you use a second action tool/second control parameter to update that.

Christina_H
14 - Magnetar

Unless I'm misunderstanding, there's no need for macros here.  Use a generate rows tool to get the dates you're interested in, use a summarize tool to count the distinct contracts ending each day, and join them together.

Christina_H_0-1675866311412.png

 

apathetichell
20 - Arcturus

@Christina_H- For the outer data compilation (dates/gym plans) - yes - 100% but the macro is needed because once you get the data you have to feed it into a SQL query. Dynamic Input In-DB excepts one row (query/connection) so you need to iterate through your data via batch macro to feed in your queries.

Labels
Top Solution Authors