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$
It looks like your desired output is simply $x-6$. Your example doesn't show anything about how you want $y$ to influence the output, because all rows in the example have $y=0$.
In any case, saying that you want to know the distance from some Sunday doesn't tell anything about which Sunday that is. What you need as an input is the complete date of the reference day (ideally expressed on your "indexPath" scale) -- not merely which day of the week it is.