DAX Counting Values in previous period(s) - powerpivot

I have a Month Column with the Month Field populated for each line for the 100K of lines of data I have.
I need to count the amount of times the Month Field is populated in the Previous Month (Period).
I also need to count the total amount of times the Month Field is populated in the Previous 11 months as well.
This is a rolling count for each months reporting that I do..
table name: 'ws pds' and field name [Month Tagged]

You can utilize the powerful time intelligence functions in DAX such as PARRALLELPERIOD to look at values from previous months. But in order to make use of these functions you need to create a calendar/date entity. Mark that entity as a Date table. And join to it by date from your "ws pds" table. The Date dimension should span the timeframe of your date with a continuous list of dates, one row per day.
Then your measure could look like this:
PreviousMonthCount=
CALCULATE (
COUNTROWS ( 'ws pds' ),
'ws pds'[Month Tagged] <> BLANK (),
PARALLELPERIOD ( Calendar[Date], -1, MONTH )
)

Related

Sum values on date table where one column equals a selected value from another

I have a DimDate table that has a Billable Day Portion field that can be between 0 and 1. For each day that's in the current Bonus Period I want to multiple that Day Portion by 10, and then return the total sum.
To find out what Bonus Period we're in, I return ContinuousBonusPeriod where the date equals today:
Current Continuous Bonus Period:= CALCULATE(MAX(DimDate[ContinuousBonusPeriod]), FILTER(DimDate, DimDate[DateKey] = TODAY()))
I can see in the measure display this is correctly coming back as Bonus Period 1. However, when I then use ContinuousBonusPeriod in the measure to determine the number of days in the current period, it only returns 10, 1 day multiplied by the static 10.
Billable Hours This Period:= CALCULATE(SUMX(DimDate, DimDate[Billable Day Portion] * 10), FILTER(DimDate, DimDate[ContinuousBonusPeriod] = [Current Continuous Bonus Period]))
It appears to be only counting today's DimDate record instead of all the records whereContinuousBonusPeriod = 'Bonus Period 1' as I'd expect.
I needed to make sure no existing filter was applied to the DimDate table when calculating the Current Continuous Bonus Period:
Current Continuous Bonus Period:= CALCULATE(MAX(DimDate[ContinuousBonusPeriod]), FILTER(ALL(DimDate), DimDate[DateKey] = TODAY()))
(Notice the ALL() statement)

Ms ACCESS: calculating past annual averages over varying date ranges

In a form on Ms ACCESS, a user can select a commodity (such as copper, nickel, etc.) from a list and a commodity price date from a list. A trailing 12 month average commodity price should then be calculated.
For example: the user selects Copper as commodity and February 1st 2010, 02/01/2010. I then want the average price to be calculated over the time period: [02/01/2009 - 02/01/2010].
I'm not sure how to write this in query form. This is the current incomplete code;
SELECT Avg(CommPrices.Price) AS Expr1,
FROM CommPrices
WHERE (((CommPrices.Commodity)=[Forms]![Tool Should Cost]![List243]))
AND CommPrices.DateComm = [Forms]![Tool Should Cost]![List55];
List243 is the list of commodities the user can select from, list55 is the list of dates the user can select. All data is obtained from the table CommPrices.
Note: the earliest dates in the column DateComm is 01/01/2008. So if the user selects a date for example 02/01/2008, then calculating the average over the past 12 months before 02/01/2008 won't be possible. I do want the code to still calculate the average using the dates available. (in the example it would just be the average over the past month)
Second Note: the column DateComm only has monthly dates for the first day of every month (e.g 01/01/2008, 02/01/2008, 03/01/2008). The dates listed in list55 can refer to different days in the month (e.g 03/16/2009), in that case I want the code to still calculate the past 12 month average using the closest commodity dates possible. So if the user selects date 03/16/2009, I want the code to calculate the 12 month average for 03/01/2008 - 03/01/2009.
For "integer" months it would be:
SELECT
Avg(CommPrices.Price) AS AveragePrice,
FROM
CommPrices
WHERE
CommPrices.Commodity=[Forms]![Tool Should Cost]![List243]
AND
CommPrices.DateComm = BETWEEN
DateSerial(Year([Forms]![Tool Should Cost]![List55]) - 1, Month([Forms]![Tool Should Cost]![List55]), 1)
AND
DateSerial(Year([Forms]![Tool Should Cost]![List55]), Month([Forms]![Tool Should Cost]![List55]), 1)

Selecting sets of data and creating a new column in SQL Server

In SQL Server can you select the first set of values (i.e. week numbers 1 - 52) give them another value in a new column, then select the next lot of values.
The problem I am having is the data table I am working on has week numbers for each financial year, which starts the first Sunday after 1 October. So it simply iterates 1 - 52 for each financial year.
I am trying to make a column in a view that grabs the first 52 gives them the a financial year value of 1, then grabs the next 52 and gives them a financial year value of 2 etc (obviously with year 1 starting at the first record). I do have the Week Ending Date column to work with also.
Here is a snippet of the table:
Is this possible?
Leave the Sundays and Octobers. If I understand correctly, you only need to assign a rank to each occurrence of week number in order of the ending dates.
Please try this (but use copy of the table or transaction to check first; of course T is name of your table):
update T
set fiscal_year = YearNumbers.FiscalYear
from T
inner join
(
select WeekEndingDate, WeekNumber, DENSE_RANK() over (partition by WeekNumber order by WeekEndingDate) as FiscalYear
from T
) as YearNumbers
on T.WeekEndingDate = YearNumbers.WeekEndingDate and T.WeekNumber = YearNumbers.WeekNumber

Effecient way to compare values with different date range

I need to build a report which has week's sales data by department by date (which I have done using Matrix) and compare it to weeks sales last year. Report would be run weekly. I am wondering which approach would be most efficient:
1) generate separate data sets - 1) for 1 weeks data and 2) for 1
week a year ago and then compare these values;
2) create 1 data set for a period of 1 year + 1 week and insert
calculated fields in the data set;
3) create 1 data set for a period of 1 year + 1 week and insert
calculated fields with expressions inside the report
4) any other.
Thanks
If you tables are well indexed, then I'd suggest you go with the first approach - It'd reduce the data to be compared. I feel that pulling data from one year in past would be a less expensive than comparing 2 week's data out of one year's data.
You could use the week number of any given week to efficiently pull data for any week based on the date range.

How to filter DATESBETWEEN based on column value (to calculate number of business days in a month)

Working on an SSAS Tabular project in Visual Studio 2010;
I'm trying to create a measure that calculates the total number of business days in a month:
I have Month Start Date and Month End Date measures, and Date and Is Business Day columns.
I can create a Total Business Days measure with COUNTROWS(FILTER(Dates,Dates[Is Business Day]=TRUE())). That gives me the number of business days in the context, but I want the number of business days for the current month.
I've tried various combinations of FILTER, COUNT, COUNTX, COUNTROWS, DATESBETWEEN, and CALCULATE without success.
What I want is a count of days between two dates, where the column [Is Business Day] is true, but I can't seem to get the right combination of filtering.
I would guess I filter the Dates table the way I do for the Total Business Days measure, but FILTER returns a table and COUNTROWS expects a single column - is there a way to get single column from a FILTER result?
Here's one thought...
First, create a calculated column called MonthKey (if you don't have it already):
=YEAR([Date]) * 100 + MONTH([Date])
Then create another calculated column called IsCurrentMonth
=IF(YEAR(TODAY()) * 100 + MONTH(TODAY()) = [MonthKey], 1, 0)
Then you can create your calculated measure as
COUNTROWS(FILTER(Dates,Dates[IsCurrentMonth] = 1))
Would that do what you need?