Power BI (DAX): Calculating MoM Variance of a Measure Field - dax

I have measure formula that takes a table and converts it to monthly count of distinct customers:
Active Publishers =
CALCULATE(
DISTINCTCOUNT( 'Net Revenue Data'[Publisher Name] ),
'Net Revenue Data'[Active Month] = 1)
Now, I would like to create a new formula that takes the Month-Over-Month (MoM) variance of this trend, like this:
This is the formula I attempted to get the net monthly change:
Net Change = 'Net Revenue Data'[Active Publishers] -
CALCULATE('Net Revenue Data'[Active Publishers],
PREVIOUSMONTH('Net Revenue Data'[Date (Month)]))
How can I create a measure that takes the monthly variance of the 'DistinctCountActiveMonths' measure?
I created an additional date table to relate to the revenue table's date column:
Date Table =
ADDCOLUMNS(CALENDAR("1/1/2000","12/31/2025"),"DateAsInteger",FORMAT([Date],"YYYYMDD"), "Year",YEAR([Date]), "MonthNumber",FORMAT([Date],"MM"),"YearMonthNumber",FORMAT([Date],"YYYY/MM"),"Ye arMonthShort",FORMAT([Date],"YYYY/mmm"),"MonthNameShort",FORMAT([Date],"mmm"),"MonthNameLong",FORMAT([Date],"mmmm"),"DayOfWeekNumber",WEEKDAY([Date]),"DayOfWeek",FORMAT([Date],"dddd"),"DayOfWeekShort",FORMAT([Date],"dddd"),"Quarter","Q"&FORMAT([Date],"Q"),"YearQuarter", FORMAT ( [Date], "YYYY" ) & "/Q" & FORMAT ( [Date], "Q" ))
Now I should be able to relate the two tables to create Month over Month variance.

PREVIOUSMONTH(), like all built-in time intelligence functions in DAX requires a proper date dimension with consecutive, non-repeating dates that span from January 1 of the first year you have data to December 31 of the last year you have data.
I am unsure, looking at the tags on your post whether you are using add-ins to Excel or the Power BI desktop program. If Excel, there is an option in the Power Pivot menu to 'Mark as date table', which you should always do in a Tabular model. If Power BI desktop, this functionality is not yet available, so you must create the relationship between your date dimension and fact table on the date field directly, and not on some surrogate key.

One solution to this problem requires 2 steps:
Create an intermediate calculated column that takes the last month's active publishers:
LM Active Pubs = CALCULATE([Active Pubs],DATEADD('Net Rev 09-14'[Date],-1,MONTH))
Subtract the current months "Active Pubs" from the last month's active publishers:
Change in Active Pubs = [Active Pubs]-[LM Active Pubs]

Related

I do not get my revenue filtered date wise in Power BI

I am a newbie in Power BI. I have created my data model with a Calendar Lookup table which consists of all dates since 2015 to current date and also a Sales table. I have sales entries for 2017 in Sales table. Now I want my Total Revenue gets filtered for a particular date. Suppose the date is 1/1/2017. And I have minimum 6 entries on that Order Date in Sales Table. The calculation I have done to get Total Revenue is -
Total Revenue =SUMX (Sales, Sales [Order Quantity] * Sales [Retail Price])
And now I want to filter this Total Revenue for 1st January 2017. I have chosen CALCULATE function to do that. I have tried this way -
Revenue on 1/1/2017 = CALCULATE ([Total Revenue],
Calendar [Date] = 1/1/2017)
But I do not get any answer. I have perfect relationship status between Sales and Calendar table. I have made a one-to-many relationships between Date in Calendar table and Order Date in Sales table. I can't understand what I am missing out.
You have to provide your date value as below (YYYY,MM,DD):
Revenue on 1/1/2017 = CALCULATE ([Total Revenue], Calendar [Date] = DATE(2017,01,01))
Hope this helps!!

Power Query - DATEADD fails once current year filter applies

Please help. I don't understand why I have 2 similar data models and one works but the other does not.
I have a fact table with a Calendar Key and data as granular by day by location. I have a calendar dimension table with a Calendar key, each individual date, and month/fiscal year definitions.
Measures are Rev = SUM(FactTable[RevColumn]) then Rev PY = CALCULATE([Rev], DATEADD(CalendarTable[Day_Date], -1, YEAR))
Model 1 in Power BI, when I apply date filters, Rev calculates the total revenue & Rev PY calculates last year's rev for same range. Even if I select all dates that are YTD (which is tedious)
Model 2 in Excel 2016 Power Query, when I apply a year filter, Rev calculates total revenue for YTD in Fiscal Year & Rev PY is blank.
I don't understand what is different and why it would even fail in the first place. Calculate is supposed to override the filter context of the current filters right?
Also: the only "Date" type columns are located in the calendar table so it's not possible for me to select the wrong table.
I ultimately had to ask a professor on Udemy.com. The solution is to mark you calendar dimension table as a "Date Table." Since I'm combining the 2 tables on a whole number field, the time intelligence functions only work when filtering with date columns.
By marking the Calendar table as the Date table, the time intelligence functions know to use any attributes from that table to filter down the date ranges and get and display the appropriate answer.
In Power Pivot view, click the "Design" ribbon, then click the "Date Table" button and the "Set Default" button after that.
This resolved all issues I had.

How to create a measure in Power Bi dax which is to be related to Date Column in matrix

I want to make the following table with such data:
Columns: Dates from the selected week(based on calendar table) in WeekMonth slicer
Subcon: Subcontractor distinct list, created with power query from the Subcontractor values in the data table
Plan and Actual: count of set dates, which were set in the data table which looks like:
siteid, subcontractor, milestone, plan date, actual date
Milestone slicer allows to filter data table for a specific milestone and is linked to datatable[milestone]
WeekMonth slicer allows to filter data table for a specific week, only one week can be displayed in the table, by default the "current week is selected".
Filtering data table using the Relatationships between the Calendar[WeekMonth] and the data table is not possible because it contains two types of dates.
As I understand the DAX measures for Plan and Actual must be designed in a such way so it would refer to the date from the matrix Column. How this can be implemented?
How the table must look like(values are not correct yet!):
"Filtering data table using the Relatationships between the
Calendar[WeekMonth] and the data table is not possible because it
contains two types of dates."
It is actually possible to filter multiple date fields by one calendar table in Power BI.
The way it works:
Connect your Calendar table to plan date field;
Connect the same Calendar table to actual date field. Power BI will add this second relationship as "inactive", as indicated by the dotted line;
Normally, when you use your week slicer, it will filter DAX measures by the active connection only (plan date). However, you can tell DAX to filter by the inactive connection instead, using USERELASHIONSHIP function.
Code typically looks like this:
Metric by Plan Date = SUM(TableName[Field])
Metric by Active Date =
CALCULATE( [Metric by Plan Date], USERELASHIONSHIP(Calendar[Date], TableName[Actual Date])
Here, you first define your DAX measure normally, and it will calculate using Plan Date. Then, you recalculate the same measure using different relations (Active Date).
This article might help you further:
https://radacad.com/userelationship-or-role-playing-dimension-dealing-with-inactive-relationships-in-power-bi

Calculate Full Period Basing Dates Selected by User on the Date Slicer

We need to create a dashboard with a date slicer and a TimeGranularity slicer.
The date slicer allows the user to choose which every date range they want.
The TimeGranularity slicer has the values like Day, Week, Fiscal Period, and etc.
To make it easier to explain. I reduce the model to only 4 tables: a fact table "FactTable", a date table "DateTable", a dim table "TimeGranularity" with flattened dates for different time granularities like Day, Week, Fiscal Period, a dim table "Location"
The TimeGranularity table connects to DateTable as many to one relationship.
What we need is, when the user select on the date slicer with a date range like 01-Jan-2017 to 05-May-2017 and the Fiscal Period level on the TimeGranularity slicer, the metric [Sales] should be able to pull/calculate the full fiscal periods that 01-Jan-2017 and 05-May-2017 fall in plus the fiscal periods in between.
Our DateTable has all the related columns that might be needed for this, like FiscalPeriodID, FiscalPeriodDays (how many days in each FP), FiscalPeriodDayNumber(day numbering in each FP), FiscalPeriodIndex (numbering of all FPs)
Currently the dax for [Sales} is:
[Sales]=Calculate(sum(FactTable[Sales]), TimeGranularity)
I would love to share the pbix file but don't know how to load it up here....
Thanks a lot in advance!

Rank only for latest month

I've a calculated field "Total Revenue" which blends revenue from multiple data sources in Tableau.
Based on this field (aggregate field) on monthly basis, I would like to show only TOP 5 items with highest revenue as of latest month.
As shown in above table, items have to be filtered out (as they are TOP 5 in March) based on revenue data as of the latest month (March). How can this be achieved using RANK()? I'm not able to rank only for latest month as formulated below because it shows error as I cannot mix aggregate and non-aggregate functions.
IF DATETRUNC('month'),ReportDate)=//March 1st date given//
THEN RANK(Total Revenue)
END
My solution is a kind of workaround but seems to be working:
Create a calculated field with below formula:
IF DATEPART('month', {MAX([date])}) = DATEPART('month', [date])
and DATEPART('year', {MAX([date])}) = DATEPART('year', [date])
THEN
[revenue]
ELSE
0
END
The {MAX([date])} part on the code gets the maximum date in your data, it is fixed with { and } characters so that the value is not effected by the filters, date partitions etc. If you want the sorting month to be the one we are in then you should change that part with NOW()
Now we have a value containing only the sum of the latest month and we can sort our visual with this Measure.
You can drag your new measure (I called my measure: 'last month revenue') to the details and right click your item pill on the Rows, sort it by your new field.
And finally drag your item to the filters and go to Top tab, make filter your data according to sum of "last month revenue" measure.
Below screenshot shows the excel data and the final Tableau table:

Resources