DAX - YTD with year on columns - dax

Can anybody suggest how to write YTD Sales formula in DAX that can be used (e.g. in Excel Pivot Table) with Year on column, to compare YTD sales over the last few years.
Specifically, my difficulty is in determining "as of date". If I refer to the Date table without removing the filter, then something like LASTNONBLANK('Date'[Date],[SalesAmount]) will give me 12/31 of the corresponding year for prior year's columns. And if I remove the Date filter via ALL() I will get the last available date, thus ignoring filters/slicers a user may have added.
I am fairly new to DAX, and was hoping that this was a common scenario that someone has already solved. But all I am finding are the solutions for YTD comparison with prior year, NOT YTD "trend" over multiple years.
Any feedback is really appreciated!
Vlad

You need to provide more details for me to be specific, but I can guess at your dataset.
Assuming:
table1 with Date, Period, Year and SalesAmount
AND
table2 with Period and Year
AND
No relationships.
Your measures might go like this:
MAX_PERIOD = CALCULATE(MAX(table2[Period]))
MAX_YEAR = CALCULATE(MAX(table2[Year]))
YTD Sales = Calculate(Sum([SalesAmount]), FILTER(table1, table1[Period] <= [MAX_PERIOD] && table2[Year] = [MAX_YEAR]))
And your Pivot Table/Matrix might look like this:
_____________________________
|Filters: |Columns: |
| |table2[Period]|
| | |
|____________|______________|
|Rows: |Values: |
|table2[Year]|[YTD Sales] |
_____________________________

Related

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.

DAX Select a date range in the calendar table

I am looking to select the size of the calendar table, filtering everything before or after some date, much like a slicer does.
I am trying to compute the percentage change between prices of equity indexes between two dates: now and a year ago. The problem is that the same date last year sometimes falls on a non-trading day. In that case I want the code to select the earliest value available.
In a similar situation, where I use a slicer, I implemented this by using FIRSTNONBLANK and LASTNONBLANK functions. This way, if the slicer is set to a non-trading date the system still works.
I am very much a DAX/Power BI novice, interested in the methodology in DAX as much as addressing the current problem.
Thanks in advance.

SSRS Report Expression to display Weekly, Monthly and YTD totals

Have a report that displays daily counts and dollars. I'm trying to additional columns to display weekly, month to date and year to date totals.
In the past I've done this by creating separate weekly, monthly and ytd data sets and then used lookup expressions to populate the fields. Is it possible to use expressions that reference my primary data set, which includes a date column, to get weekly, mthly and ytd totals?
If this is possible, I'm not sure how to write the expressions to:
look back at the past 7 days and get totals for those days
Get totals for the current month
Get YTD totals.
Any help that anyone can provide would be appreciated. I've attached an example of the report I'm trying to create.
Thank you.
You would just need to use an expression for each as a filter your Dollar Amt column. It will determine whether to add the dollar amt or a 0 for each row of data.
I assume that you have a date field - I'll call it DATE.
YTD
=SUM(IIF(YEAR(FIELDS!DATE.VALUE) = TODAY.Year, FIELDS!DOLLAR_AMT.VALUE, CDEC(0)))
Monthly
=SUM(IIF(FORMAT(FIELDS!DATE.VALUE, "yyyyMM") = FORMAT(TODAY, "yyyyMM"), FIELDS!DOLLAR_AMT.VALUE, CDEC(0)))
The weekly would be a little different and depend on your week (sun - sat, mon - sun...) and your first day of the week setting but you get the general idea. If your using Monday - Sunday maybe something like:
WTD
=SUM(IIF(FIELDS!DATE.VALUE >= TODAY.AddDays(1 - TODAY.DayOfWeek), FIELDS!DOLLAR_AMT.VALUE, CDEC(0)))
The CDEC is used to add the 0 to the dollar amounts - otherwise you'll get an error when it tries to add 1.25 + 0. It's to Convert to DECimal.
The same logic will work for the counts - just use the Unit_Count field and remove the CDEC conversion for the zeroes.

DAX - Sum Workdays By Month within Range

I've been at this one a while.
Objective: Pivot a monthly summary of days worked by Resource within the range for that Resource line where those dates (from StartDate to EndDate) correspond to the Dates table IsWorkday field.
Thanks for your help!
It depends on how you define what a workday is. I suspect this is Monday thru Friday? If so, try the weekday function.
=WEEKDAY(serial_number,2)
Where serial_number is the cell with a date you want to identify. This would not exclude holidays, I would recommend an index/match on some holiday reference table for that.
https://support.office.com/en-us/article/WEEKDAY-function-60e44483-2ed1-439f-8bd0-e404c190949a

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