# Neat way to make Dataset from RecurrenceTable

I just learned that I can create a Dataset by combining Dataset and Table functions. For example:

d = RandomVariate[DemandProcess, nperiods];
dat = Table[<|
"d" -> d[[i]],
"underQ" -> Boole[d[[i]] > Q] (d[[i]] - Q),
"overQ" -> Boole[d[[i]] <= Q] (-d[[i]] + Q),
"under$" -> Boole[d[[i]] > Q] (d[[i]] - Q) cu, "over$" -> Boole[d[[i]] <= Q] (-d[[i]] + Q) co,
"iQ" -> If[i == 1, Q, 0]
|>, {i, 1, nperiods}];
dat = Dataset[dat]


For anyone interested in the background of this problem, this is a newsvendor model where Q is quantity of inventory to order in every period, d is a list of demand quantities per period, underQ is shortage of inventory, under\$is cost of the shortage, cu is the cost of unit shortage; overQ, over\$, co have similar meanings; rows are periods and inventory doesn't carry over.

Now, where I'm struggling is the iQ column: I want its consecutive values to depend on preceding values. I could achieve that, for example, by doing this:

RecurrenceTable[{iQ[t + 1] ==
iQ[t] + Boole[iQ[t] < d[[t]]] cu - Boole[iQ[t] > d[[t]]] co,
iQ[1] == iQstart}, iQ, {t, 1, nperiods}]


I don't think that this is achievable by using Table function and I don't know how to combine Dataset function with the above RecurrenceTable code.

• For such a simple recurrence you gave you can directly do the same with Table so you can write "iQ"->(q=q+1) where of course q=Q is set before the table starts. But for more complicated recurrence you'll probably have to run the recurrence table first q=RecurrenceTable[...] then only create the dataset the same way you did but "iQ"->q[[i]]. Sep 30 '17 at 9:12
• Thanks @SquareOne but my RecurrenceTable code shows a code that I made up to save me some typing, not the actual code. I should have realized that. I'll edit my question with the actual code. Sep 30 '17 at 12:28
• Your actual code for the RecurrenceTable can be easily translated to Table. Run this for example:q=iQstart ; Table[q=q+Boole[q<d[[t]]] cu - Boole[q>d[[t]]] co, {t,1,nperiods}]. Then, you can directly use it inside your dat command to build the dataset. Sep 30 '17 at 20:47

dat[Query[<|