Power Query - DATEADD fails once current year filter applies - dax

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.

Related

*SOLVED* How do I sort columns in a Matrix view?

I am attempting to create a gantt chart in Power Bi using the matrix view, and the end user wants it to have year, quarter, and month. The year and quarter columns are fine, but the month columns are out of order, and I cannot seem to figure out how to put them in order. My calendar table and everything in it are created using dax.
So far, I haven't really tried anything. I am assuming Power Bi is not recognizing them as months in a date table and that is why it is not sorting them accordingly, but alphabetically by quarter and year.
You need a sort by column.
You can read how to create one here.
https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-sort-by-column?tabs=powerbi-desktop

Microsoft Power BI, DAX - tricky ALLSELECTED with 2 values in slicer, but show only TOP 1 row in visual

This question is an extension of an already answered question, which I posted this week.
I have the below situation in Microsoft Power BI.
I have 2 simple tables:
1) CountryTable
2) YearTable
There is a 1-M relationship between YearTable and CountryTable.
The latter (YearTable) is used to feed values into a slicer.
(In my client database, Year has some alphabetical prefixes, such as Q1-2022, so I prefer to use YearOrder column to sort the Year column at the backend, while the slicer will display the Year column.)
The former (CountryTable) is the main table, with just a few sample rows.
These two tables are related via the Year column.
The Year slicer always has EXACTLY 2 values chosen in my Power BI report.
I need the Maximum of these two values of the year slicer as a measure, for each row of my visual.
At the same time, these two year values of the slicer must remove the unwanted rows in my report visual, based on the slicer selection of year values.
For example, when the slicer has 2019 and 2020 chosen, I need the value as in the DesiredOutput1 page.
Similarly, you can see DesiredOutput2 (Slicer values are 2020 and 2022); DesiredOutput3 (Slicer values are 2019 and 2022) pages.
I have indeed successfully obtained DesiredOutput1, DesiredOutput2, DesiredOutput3. Thanks to all the folks who helped me attain this.
Now, my main requirement in this posting, is this:
After obtaining the DesiredOutputs above, I need the following output:
Show only the TOP 1 row (ASC order of Year column, which is the minimum value of the slicer).
Essentially:
Year column of the visual: Minimum value of the slicer
MaxYear_Measure_SlicerSelection: Maximum value of the slicer (maximum of the two values chosen in the slicer)
You can see below:
Note: MaxYear_Measure_SlicerSelection measure can refer to any one of the two measures [MaxYear] or [MaxYearMeasure_Community] (see the .pbix file for the formulas of the measures).
Any idea ?
I prefer the Year column of the visual not to be converted to a new measure. Would RANKX help in this case ? Any thoughts?

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

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:

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

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]

Resources