This question is related to a programming question. Because I think it's pure mathematic I post this question here.
I want to calculate the difference in days to a reference point. Therefore I have two inputs:
indexPath.Item: natural number between zero and infiniteordinality: natural number between zero and six, which represents a day in a week
ordinality:
0 - Sunday
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday
I made a sketch to demonstrate my task:

As you can see I have a reference date - the 1st of Feb. 2015. This day is a Sunday and the ordinality for Sunday is 0. I want to calculate the difference in days to this date in dependency of the ordinality and the indexPath.Item.
I can't figure out how a equation would look like under these conditions. I could set up an input/output table as follows:
x = indexPath.Item
y = ordinality
z = difference in daysy=0 x=10 z=4
y=0 x=9 z=3
y=0 x=8 z=2
y=0 x=7 z=1
y=0 x=6 z=0
y=0 x=5 z=-1
y=0 x=4 z=-2
y=0 x=3 z=-3
y=0 x=2 z=-4
y=0 x=1 z=-5
y=0 x=0 z=-6
This is only an example, where y is always zero.
The reference date in my case is always the first of a month. The 1st of a month falls into a week - namely the first week of a month (the 1st week of a month doesn't always include a full week in my case!). The 1st of a month defines the ordinality (y) and this is the start (or the shift if you want) of my calculations. Here the difference in days is zero. If y = 0, Sunday (last column) is the start. If y = 6, Saturday (next-to-last column) is the start. In x direction the difference in days increases and in -x direction the difference in days decreases (negative values).
Can anyone give me a hint in the right direction?
Edit:
I found out that 1 - y + x would work, but here another ordinality (y) is used: Monday: 1 - Sunday: 7. So this formula doesn't work for y = 0. Can this be expressed in an adapted formula?
Solution:
Thanks to Henning Makholm!
$R = \begin{cases} 6 & \text{if }y=0 \\ y-1 & \text{otherwise}\end{cases}$
$x−referenceDate$
x-y+1fails because y can have a zero. – testing Mar 17 '15 at 13:12