Trying to figure out how ParallelPeriod works in DAX - powerbi

I have a simple Power BI table that looks as follows:
I have two tables. A Date table and an Invoice table with a field representing invoice amounts. This is a 1-M relationship on Invoice.InvoiceDate.
The second column is simply a measure for the sum of invoices. The third and fourth columns are measures using ParallelPeriod to sum invoices for 12 months prior and 24 months prior. Even though these numbers are correct, I'm not entirely certain I know what's actually going on.
The measure for the 12-month parallel period looks like this:
Sum Invoice Amount 12 Months Ago =
CALCULATE (
SUM ( FactCustomerTransaction[InvoiceAmountDollars] ),
PARALLELPERIOD ( 'Date'[Date], -12, MONTH )
)
Here is what I think is happening. When the sum is calculated for say 2015-Feb, all values for that month are retrieved in the invoice (many side) table and summed to generate the "Sum Invoice Amount". The sames dates, minus 12 months, in the Date table, are retrieved and the same sum is generated for those range of dates for 'Sum Invoice Amount 12 Months Ago'. And then the same process for 24 months ago.
This works because of the 1-M relationship between Date and Invoice. Is this correct?

For 2015-Feb row, assuming the Year and Month Name column is on your 'Date' table, your filter context is 'Date'[Year and Month Name] = "2015-Feb". This filter corresponds to the dates 2015-02-01 through 2015-02-28 in your 'Date'[Date] column and that filtering propagates across the relationship to return only the rows in FactCustomerTransaction table where InvoiceDate is one of those dates and then sums the amounts corresponding to only those rows.
When you add PARALLELPERIOD, it works the same way except that after matching the dates 2015-02-01 through 2015-02-28 corresponding to 'Date'[Year and Month Name] = "2015-Feb", it shifts those dates back by 12 months and then propagates those shifted dates across the relationship.

Related

calculate an average on a calculated measure in DirectQuery

I'm using Power BI, in directQuery mode for all my tables (other than my date table).
I can't change that.
To simplify, I have a fact table that has an id, a date field and a sales figure (not even using the sales for this question.. just the fact that there is a sale on this date).
The date field relates to my date dimension as you'd expect.
I have created a measure, which is a rolling 2 year count of sales (not sum, just count).
Rolling_year_sales_count = CALCULATE(
DISTINCTCOUNT( fact[sale_id]),
DATESINPERIOD(
dim_date[yyyy_mm_dd],
LASTDATE(dim_date[yyyy_mm_dd]),
-2,
Year
))
this looks good when I put it on a simple table - its properly counting the rolling year counts.
Now, I want to create a rolling 2 year AVERAGE count.
So the result should show 2 for 1925, 5 for 1926, 8 for 1927... (Add the 2 previous year's rolling counts, and divide by 2)
I tried just including this measure in a new measure that uses SUMMARIZE - but because I am using DirectQuery, I cannot refer to calculated measures in my SUMMARIZE.. Same goes for CALCULATE.
So how can I calculate this average, knowing that I'm in directQuery mode?
Thanks!

DAX: How to count how many months have sales in a period

In my fact table (fTable) the columns I have are dates, region and sales.
dates
region
sales
-----
------
-----
I am visualizing the data in a pivot table with regions as rows and months as columns (I have a date table (dDate) with a months column in my model)
I am looking for a way to dynamically change the denominator in an averaging measure if a certain region doesn't have sales in a given month. Right now my denominator is hard-coded as 6, because I am averaging 6 variables in my nominator, but any one of them could be 0 if I don't have any sales in a certain month, in which case my denominator needs to change to 5, 4 or less depending on how many months I don't have sales in. So I am looking to count how many of the past 6 months have sales and sum that as the denominator.
I have managed to count months with sales this way:
Denominator:=
var newTable = Summarize(fTable,fTable[date (month)], fTable[region],"Sales",[Sum of Sales])
var MonthsWithSales = Countrows(newTable)
RETURN
MonthsWithSales
I've tried to RETURN
Calculate(SUMX(newTable,MonthsWithSales), Dateadd(dDate[Date],-6,MONTH)
but it yields a wrong result.
Any suggestions?
Thanks
Based on my sample, we can use function VALUES & COUNTROWS inside CALCULATE to get what we need:
Measure = CALCULATE( COUNTROWS(VALUES('Table (2)'[Month])), ALL('Table (2)'[Month]) )

Finding Previous year Sales in Power BI with week number

I am trying to find the Previous year sales for the same week in Power BI.I dont have any date column.
I have two table one is FACT Indicators table as shown below:
and one sales table( Fact Sales table):
I want to create one calculated field namely(Sales Previous Year) to show the previous year sales for the same week .
In 'Fact Indicators' table 'PY 52 week flag' filed shows if this week id is Previous year or not.
Week column shows the week number from 1 to 52 weeks .
Week Id shows the unique number per Market key.
'Market_Week Id Key' is the common joining key between FACT Indicators table and Fact Sales table
Please help me to find the formula for calculated field.I dont have the date field in my raw data
Every time you deal with anything related to dates, you will need to add what we call a date dimension. It will save you tons of headaches. Once you have it in you will be able to hook it into the creation of the calculated filed.
you can google power bi or ssas date dimension and find tons of information on it.
Yeah! I guess SQL Technical team can be a tough crowd.... Well! In this case, I would recommend bringing the Year into FactSales Table from Fact Indicator . You have two options here with physical relationship set up between Market Week Id Key in both tables you can build a calc column with
Year = CALCULATE(VALUES(FactIndicators[Year]))
or without relationship use LOOKUPVALUE on WeekId
Year = LOOKUPVALUE(FactIndicators[Year], FactIndicators[WeekId], FactSales[WeekId])
Sales Last Year calc colum :
SalesLastYear =
CALCULATE (
SUM(FactSales[SalesThisYear] ),
TOPN(1,
FILTER(
FactSales,
FactSales[Year] < EARLIER(FactSales[Year])
&& FactSales[Key] < EARLIER(FactSales[Key])
)
)
)

Filter table based on a specific date plus 7 days

I have a table containing a date field (from 1 March 2020 to now) that I need to filter to a specific date and the previous 6 days to give complete week's data. So if I chose 30 March I'd get a table of 24 March to 30 March. If I then chose 31 March the table would show 25 March to 31 March.
I can use a date slicer to choose a range of dates but I want to be able to pick a single date, with Power BI automatically selecting the earlier date.
Any pointers much appreciated.
Mark.
You can create two measure - one for Slicer selected date and Another one with 7 day minus from the selected date as below-
Considering your date table name is- Dates
selected_date = SELECTEDVALUE(Dates[Date])
seven_day_starts_from = DATEADD(Dates[Date],-7,DAY)
Now create your calculated measure first like-
total_sales = SUM(Sales[sale])
Here comes how you will always calculate last 7 days sales considering the selected date in the slicer-
7_day_sales =
(
CALCULATE(
[total_sales],
DATESBETWEEN(
'Dates'[Date],
[seven_day_starts_from],
[selected_date]
)
) + 0
)
Remember, this is just a sample flow showing how it should work. You should try to follow the steps with your data and table structure. Dates table is a calendar table and Sales table is connected to the Dates table using the Date column.

Dax Finding date based on Criteria calculated Column

I couldn't find an answer for my issue elsewhere, so trying my luck here.
I have sales table and my final result should determine if there were sales made for same person in specific period of time, for example within 7 business days. for example:
For ID 123 I have to flag it that sale for products A,B,C where within specified period.
For ID 1234 only sales of products A and B meet the criteria product C was sold in irrelevant time frame.
I've created a date table with indicators that determine for each date if the date is a working day, but i am struggling to calculate the relevant last working day
For example: I need that for 01/01/2019 i will get 10/01/2019 date, based on NUMOFDAYS and FinalWorkday = TRUE, which basically means that i have to count NUMOFDAYS times TRUE statement for each date and return corresponding date.
After that step done I think that it would be pretty easy for me to determine if the sale for a person was made in specific time frame.
If someone can give me direction for that much appreciated
Thank you in advance
You could use a DateTable like this one:
I used the following DAX-expressions for the calculated columns:
nrDays = 7
isWorkDay = WEEKDAY('DateTable'[Date],2) < 6
rankWorkingDays = RANKX ( FILTER ( DateTable, DateTable[isWorkDay] = TRUE () ),
DateTable[Date] , , ASC )
LastWorkDay = LOOKUPVALUE ( DateTable[Date],
DateTable[isWorkDay], TRUE (),
DateTable[rankWorkingDays], DateTable[rankWorkingDays] + DateTable[nrDays])
This issue can be solved by the following, since three are non-working days/holidays we can filter them out via POWERQUERY, than add an Index Column and Another column Which is basically Index column + Number of days wanted, then simply merge duplicate of dates query on Index+number of days wanted column on Index column. And we get the correct date