Wednesday, November 21, 2018

How to use SAMEPERIODLASTYEAR, PARALLELPERIOD and DATEADD Functions in Power BI

What is difference between SAMEPERIODLASTYEAR, PARALLELPERIOD and DATEADD Functions in Power BI DAX
1. SAMEPERIODLASTYEAR:
This function returns a table that contains a column of dates shifted one year back in time from the dates in the specified dates column, in the current context.
In simple words, it returns the same PERIOD but last year! same period, which means if you are looking at data on the day level, it would return same day last year. 
If you are slicing and dicing in a month or quarter level; this function would give you the same month or quarter last year dynamically. 
Syntax :
SamePeriodLastYear(<date field>)

Example :
Suppose we have a Data Model in our Power BI Report as follows..


Now based on this model, we will create the reports, where we discuss about the SAMEPERIODLASTYEAR Function and other related functions.

We use the following DAX Expression to calculated "Net_Sales" of Same Period Last Year.
This expression will work for even our current context of data viewing at Year, Quarter, Month or at a Day level, the SAMEPERIODLASTYEAR function will provide the same period of Last Year dynamically.

LYSP_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),SAMEPERIODLASTYEAR(DimCalendar[Date_Id]))

Note :
here the FactSales and DimCalendar are the related Tables. The Field [Date_Id] represents the Date column in this Data Model.

Output :
Current Year Vs. Last Year :

Current Year Quarters Vs. Last Year Same Quarters :


Current Year Months Vs. Last Year Same Months:


Similarly we can view the data at the Day level as well.

Please Note :
In Case of YTD Comparisons, when the Current Year is not fully completed, then we should restrict the SAMEPERIODLASTYEAR to consider only those specific dates from Last Year based on the Current Year dates with Sales. Otherwise, the YTD Comparisons may not accurate.

Example:
CY YTD Sales = CALCULATE([Net Sales] , DATESYTD(DimCalendar[Date_Id]))

LY YTD Sales =
VAR vMaxSaleDT = MAX(tbl_FactSales[Order_Date])
VAR vLYTD_Sales =
CALCULATE([CY YTD Sales],
    SAMEPERIODLASTYEAR(
        FILTER(VALUES(DimCalendar[Date_Id]),DimCalendar[Date_Id]<= vMaxSaleDT)))
RETURN
vLYTD_Sales

2. PARALLELPERIOD:
This function returns a table that contains a column of dates that represents a period parallel to the dates in the specified dates column in the current context, with the dates shifted a number of intervals either forward in time or back in time.

You can choose the interval to be Month, Quarter, or Year. and the number of intervals can be negative (to go to past), or positive (to go to the future).You cannot choose Day as interval in this function.
Syntax:
ParllelPeriod(<date field>, <number of intervals>, <interval>)

Example:
We will look the "Net_Sales" at various ParallelPeriods like Last Year, Last Quarter, Last Month..etc in the following examples.

Current Year Vs. Last Year :
PP_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),PARALLELPERIOD(DimCalendar[Date_Id],-1,YEAR))
Output :
Current Quarter Vs. Previous Quarter :
PP_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),PARALLELPERIOD(DimCalendar[Date_Id],-1,QUARTER))
Output :
Current Month Vs. Previous Month:
PP_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),PARALLELPERIOD(DimCalendar[Date_Id],-1,MONTH))

Output :
3. DATEADD:
The DATEADD Function returns a table that contains a column of dates, shifted either forward or backward in time by the specified number of intervals from the dates in the current context.
The DateAdd is a function that adds or subtracts a number of days/months/quarters/years from or to a date field.
Syntax:
DateAdd(<date field>, <number of intervals>, <interval>)
Example:
We will look the "Net_Sales" at various Date ranges like Last Year, Last Quarter, Last Month and Last day in the following examples.
Current Year Vs. Last Year :
DA_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),
DATEADD(DimCalendar[Date_Id],-1,YEAR))
Output :
Current Quarter Vs. Previous Quarter :
DA_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),
DATEADD(DimCalendar[Fact_Date],-1,QUARTER))
Output :
Current Month Vs. Previous Month:
DA_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),
DATEADD(DimCalendar[Date_Id],-1,MONTH))
Output :
Current Day Vs. 7 Days Back:
DA_NetSales = CALCULATE(SUM(FactSales[Net_Sales]),
DATEADD(DimCalendar[Date_Id],-7,DAY))

Note :
This expression will look 7 days back at a Transactional level and return the value on that day. If no Transaction value found on that day, it will return blank.
Output :
Notes :
The DATEADD works on the period dynamically (like SamePeriodLastYear), but the ParallelPeriod works statically on the interval mentioned as the parameter.
The DateAdd Function can work on an interval of Day, Month, Quarter, or Year. The ParallelPeriod function only works on intervales of Month, Quarter, and Year.

-------------------------------------------------------------------------------------------------------- 
Thanks, TAMATAM ; Business Intelligence & Analytics Professional 
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog