MDX Get Sales starting from 2 years ago to today - date

I'm trying to make a dynamic report that pull sales starting from the first day of the fiscal year 2 years ago up to today and rolls forward with each new fiscal year. Our fiscal years don't line up with calendar years.
I have little MDX experience and am still learning.
So it should look at todays date, get the current fiscal year, subtract 2 years from it and then pull sales starting from that year up to today.
I had some difficulty just trying to get the date working correctly as I was getting errors however the below query now pulls yesterdays sales for me. I assume I need to reference [Date].[Year] as well, but I don't know how to use it to get my desired results.
SELECT
NON EMPTY
{ [Measures].[Gross Margin Percentage],
[Measures].[Gross Margin Value],
[Measures].[Sales Value],
[Measures].[Sales Units] }
ON COLUMNS
FROM IMR
Where
{StrToMember("[Date].[Date].&" + Format(CDate(now()-1), "[yyyy-MM-ddT00:00:00]"))}

If you'd like the results to be across a range of dates then try StrToSet in your WHERE clause and construct in a similar way to what you are doing.
You currently have this:
Where
{StrToMember("[Date].[Date].&" + Format(CDate(now()-1), "[yyyy-MM-ddT00:00:00]"))}
Here is an example for 2 days which you can adapt to your needs:
Where
{StrToSet(
"[Date].[Date].&" + Format(CDate(now()-3), "[yyyy-MM-ddT00:00:00]")
+ ":"
"[Date].[Date].&" + Format(CDate(now()-1), "[yyyy-MM-ddT00:00:00]")
)}

Related

Previous Month Calculation without Date Filters or Attributes in Report

I am trying to create a report that shows total sales for the previous financial year (March-April), the current financial ytd, and the previous month in powerbi. I do not want to include any date attributes in the report or place any date filters on the report.
The 2 measures below are working as expected, but I am running into issues when trying to calculate for the previous month.
This Year = CALCULATE('Fact InvoiceLine'[Total Fare Currency],
DATESYTD(ENDOFYEAR(dateadd('Date'[Date], -2,Year),"3/31"),"3/31"))
Previous Year = CALCULATE('Fact InvoiceLine'[Total Fare Currency],
DATESYTD(ENDOFYEAR(dateadd('Date'[Date], -3,Year),"3/31"),"3/31"))
The closest I have been able to get, though it's still far from what I need.... is this,
Last Month = CALCULATE('Fact InvoiceLine'[Total Fare Currency],
DATESMTD(ENDOFMONTH(dateadd('Date'[Date], -2,YEAR))))
which goes back 11 calendar months, but what I need is to see the total for 1 calendar month. Using PREVIOUSMONTH does not work either, as that requires either a date filter or date value in the report.
Last Month =
CALCULATE([Total Fare Currency],
FILTER(ALL('Date'),'Date'[Date] >= EOMONTH(TODAY(),-2)+1),
FILTER(ALL('Date'),'Date'[Date] <= EOMONTH(TODAY(),-1))
)

Total today, total yesterday ... total 7 days ago, total last week, average this month -data awareness in Power BI

I want an easy way to have a sum of yesterday, the day before and the day before that total 7 columns (past 7 days)
Then i would like average for L7D, Average LM.
I have made a column in my date that indicate what "today" is and then my idea was to have it sum if today, sum if today-1, sum if today-2 but this does not seem to work
)
Yesterday= CALCULATE(SUM('DanvægtLines'[NAV_Qty]);FILTER('DanvægtLines';'DanvægtLines'[Dato_Anden]>=TODAY()-1)
(It take the sum of quanto, then filters if the 2.date on the order is today)
It is only working with ">=" not if i only use "=" ... witch is ok for yesterday, but the if i want to have only 4 days ago i have to filter yesterday, the day before, and 3 days ago... witch make a very very long code line i dont get why
=(today()-4 wont work)
Ok so after about 15 hours of hair pulling it dawned on me that the table I was referring to was in the date+time format, so fixing that I could use =today()-1

Difference between today and 7 days ago

I spent some hours googling now but I can't find a solution that fits, so you guys are my last hope.
I have sales data that is not aggregated (every single item sold listed seperately). I aggregate them in Tableau by Date so I can see on what day we sold which amount if items. What I'm trying to do now is compare the value of today (Saturday) with the value of last Saturday. My problem is that I dont know how to aggregate the sales of 7 days ago and today in the same table so I can compare them.
Is there a proper way in tableau to do this? Really grateful for every answer.
Create a calculation as below:
IF [Order Date]= TODAY() THEN 'Today'
ELSEIF [Order Date]=DATEADD('day',-7,TODAY()) THEN '7 Days Ago' END
Now build the view and filter out nulls from label field.

Filtering for dates less than or or equal to 9 months in the future in an Access query

Once a week I need to run a report where I query an Access database for any product that will expire in 9 months or less. The way they want it calculated is to take the date 9 months into the future and return anything that expires at the end of that month or sooner. If it were simply 270 days or less, I'd have no problem. (I'd also have no problem if I could do it in Excel, but that's not an option for now).
I came up with a solution that works every month of the year, unless it happens to be March (more specifically between March 6th and April 5th).
< DateValue(Month(Date()+270)+1 & "/1/" & Year(Date()+270))
So basically I'm:
adding 270 days to today's date
extracting the resulting month
adding 1 to the month
putting it back together as a text string so I can use < the 1st of the following month
for the year, I'm using the year from the date +270 days so I don't end up using the current year by accident
The trouble is that for the date range above (which I unhappily discovered today), I land in December when I add 270 days, so the following month is in a different year. As a result, my report only produced items that already expired.
In other words, on March 5th, I would have needed a list of everything expiring prior to December 1, but on March 6th, I need everything before January 1 of the next year.
Is there a more effective way to do this that avoids this issue? I thought of using
You may have had DateDiff in mind, and it can be used:
Where DateDiff("m", Date(), [YourDateField]) Between 0 And 9
However, that will ignore an index you might have on [YourDateField].
This, however, will include products that expired previously in the current month.
The alternative is DateSerial as Hans showed but he forgot that in SQL Date() must be used and that only those products that will expire should be listed:
Where [YourDateField] Between Date() And DateSerial(Year(Date()), Month(Date()) + 10, 0)
Use the DateSerial Function to compute the future date you need.
Here is a demonstration in the Access Immediate window which computes the date 9 months from today:
? Date
3/6/2015
? DateSerial(Year(Date), Month(Date) + 9, Day(Date))
12/6/2015
However, as I understand your requirement, you actually want dates from that entire month. In that case you can compute the first of the month which is 10 months from today and ask for everything less than that date.
? DateSerial(Year(Date), Month(Date) + 10, 1)
1/1/2016
You can include that expression in your query like this ...
WHERE expire_date < DateSerial(Year(Date()), Month(Date()) + 10, 1)

Total Average Week using a Parameter

I have a crystal report that shows sales volumes called week to date volume. It shows current week, previous week, and average week. The report prompts for a date parameter and I extract the week number to get current week and previous week volumes. Did it this way because Mngmt wants to be able to run report whenever. My problem is for Average Week I cant figure out how to get the number of weeks to divide by for my average. Report originates from June 1st, 2010. Right now I have:
DATEPART("ww", {?date}) - DATEPART("ww", DATE(2010, 6, 1))
This returns 2 right now which is perfect, so i divide my total by 2. This code will work until the end of the year then I'm hooped. Any idea how I can make this a little more dynamic. I was thinking a counter somehow, just can't get the logic down because the date parameter will keep changing, meaning I cant increase my counter by 1 after each week???
Cheers.
Look into the Crystal Reports method: DateDiff.