Getting workdays for the year in Power Pivot DAX - excel

I need to use the full year's work days in my DAX calculation, but I can't figure out how to get DAX to do that. I have two tables,
1) sales with their respective dates
2) work days table, with end of month and the month's respective work days
I created the relationship between the two, based on the date, and it works, however DAX captures workdays only for the month's where there were sales. How do I get the full year's sum?
As an example, these are the two tables I created:
and connected them as follows:
I then added a calculated field with the following DAX:
=CALCULATE(
SUM(tWorkdays[Days]),
FILTER(
tSales,
YEAR(tSales[Date]) = YEAR(MAX(tSales[Date]))
)
)
and built a pivot table like so:
As you can see, it just captures workdays for the months where there were sales, but I need the entire total, regardless if there were any sales or not.
What can I do to fix this?

I'm not positive about the details, but it has something to do with ALL(), EXCEPT(), and VALUES()
A matter of context, as they say in DAX
https://powerpivotpro.com/2011/06/precedence-part-3-allexcept-vs-all-w-values/
And another explanation:
https://www.sqlbi.com/articles/using-allexcept-versus-all-and-values/

Related

DAX Measure no longer calculates accurately if I sort an axis by another column?

I'm having some trouble creating a DAX Measure in PowerBI that will group sales by quarter, but still sort correctly. Our sales data is received cumulatively and once a quarter. I've created the following DAX Measure to calculate the quarterly sales:
Quarterly Sales = SWITCH(
SELECTEDVALUE(FactSales[Quarter]),
"Q1 2018/19",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q1 2018/19"),
"Q2 2018/19",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q2 2018/19")-CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q1 2018/19"),
"Q3 2018/19",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q3 2018/19")-CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q2 2018/19"),
"Q4 2019/20",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q4 2018/19")-CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q3 2018/19"),
"Q1 2019/20",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q1 2019/20")
)
The problem is, while this calculates accurate numbers, it chart sorts this alphabetically, so the two Q1s are beside each other. when I try to sort the Quarter column by a Sorting dummy column, the calculations then all become out of wack and are no longer accurate.
I feel like there must be an easy way to fix this, but I can't figure out how to do it. Thank you in advance for any assistance!

DAX for rolling seven day average to pivot chart by year

I'm trying to create a single pivot chart that will show separate years of data on the same date axis for a rolling 7-day average.
So, the x-axis will be text, 01-Jan to 31-Dec, and each year will be a separate series:
It has to be a text x-axis, as 01-Jan will be a category containing data for 01-Jan-2018, 01-Jan-2019, 01-Jan-2020...
In theory, the pivot table setup would have the column (series) as the Year, and the x-Axis (labels) as the date label (column Date).
The values are then from the DAX expression that creates the rolling 7-day average.
The source data (tblSource) has a single column of dates (Date2) that rolls over across years and has the column Year to break it down in the pivot.
The daily value is the one that is averaged (itself and the previous six days).
The 7-day average I normally use in DAX doesn't work here.
I need to have the Date column actually in the pivot rather than Date2, as the axis needs to be text to allow for multiple date years on the same x-axis point, but I can't get a DAX formula to work.
The other consideration is that the formula can't just consider a single year, as the rolling seven day average for 01-Jan-2018 includes the previous six days of 2017, for example.
This is the formula I usually use, but I can't manage to tweak it!
AVERAGEX (
DATESINPERIOD ( tblSource[Date2], LASTDATE ( tblSource[Date2] ), -7, DAY ),
[Sum of Daily Value]
)
But this is the output I get, and nothing has been averaged. I think it's because Date2 is being pivoted off Date, but I'm not sure how I get around that?
Can anybody offer me any help?
It's quite a frustrating problem as it would be trivial for me to do it using code, or doing it manually, but I really am trying to get better at DAX!
Thanks in advance!
Phil.
Update: Thanks to Joao for this.
=VAR d = MAX(tblSource[Date2])
RETURN CALCULATE(AVERAGE(tblSource[Daily Value]),
ALL(tblSource[Date]),
DATESINPERIOD(tblSource[Date2], d, -7, day),tblSource[Year]>0)
I had to use MAX rather than SELECTVALUE as Excel seems to lack that functionality.
I also had to unfilter the Year, so that the rolling average could be calculated from the previous year's date where neccessary.
Thanks.
When you run that measure, your table is being filtered by the date text at each point, so when you pass/create the DATESINPERIOD filter, it creates a table with the last 7 dates, but only one of them is actually available (the one relevant to your current data point).
You need to clear the filters on the table so that you have all the dates available, in order for you average to work. You can achieve this by changing the measure slightly:
VAR d = SELECTEDVALUE(tblSource[Date2])
RETURN CALCULATE(AVERAGE(tblSource[Daily Value]),
ALL(tblSource[Date]),
DATESINPERIOD(tblSource[Date2], d, -7, day))

Calculate average based on a value column (count) in a pivot table

I'm looking a way to add an extra column in a pivot table that that averages the sum of the count for the months ("Count of records" column) within a time period that is selected (currently 2016 - one month, 2017 - full year, 2018 - 5 month). Every month would have the same number based on the year average, needs to be dynamically changing when selecting different period: full year or for example 4 months. I need the column within the pivot table, so it could be used for a future pivot chart.
I can't simply use average as all my records appear only once and I use Count to aggregate those numbers ("Count of records" column).
My current data looks like this:
The final result should look like this:
I assume that it somehow can be done with the help of "calculated filed" option but I couldn't make it work now.
Greatly appreciate any help!
Using the DataModel (built in to Excel 2013 and later) you can write really cool formulas inside PivotTables called Measures that can do this kind of thing. Take the example below:
As you can see, the Cust Count & Average field gives a count of transactions by month but also gives the average of those monthly readings for the subtotal lines (i.e. the 2017 Total and 2018 Total lines) using the below DAX formula:
=AVERAGEX(SUMMARIZE(Table1,[Customer (Month)],"x",COUNTA(Table1[Customer])),[x])
That just says "Summarize this table by count of the customer field by month, call the resulting summarization field 'x', and then give me the average of that field x".
Because DAX measures are executed within the context of the PivotTable, you get the count that you want for months, and you get the average that you want for the yearly subtotals.
Hard to explain, but demonstrates that DAX can certainly do this for you.
See my answer at the following link for an example of how to add data to the DataModel and how to subsequently write measures:
Using the Excel SMALL function with filtering criteria AND ignoring zeros
I also recommend grabbing yourself a book called Supercharge Excel when you learn to write DAX by Matt Allington, and perhaps even taking his awesome online course, because it covers this kind of thing very well, and will save you significant head-scratching compared to going it alone.

SSAS compare 2 YTD values for same month in different years

I am developing a cube in SSAS, I need to be able to compare YTD values between current year and last year.
I have the below scenario:
Budget types by year : budget1-2016 , budget2-2016 ,budget1-2017
, budget2-2017.
The budget types should be placed in the columns
Units: should be placed in the rows
The measure is called "Billings"
I need to compare YTD value for "Billings" between budgets "budget1 - 2016" and "budget2-2017" (different budget types and year) for a specific month.
when the Calendar month is included in the rows or columns everything works fine,
but if it is included in the filter and I choose for example February 2016 and February 2017, the results are not as expected.
With the usage of the TAIL function, only the higher month is taken into consideration and the column from 2016 is removed.
Is there a way to put the calendar month in the filter, select same month from different years and be able to see their YTD values?
The problem is that you put two members in the filter. CurrentMember doesn't work with two tuples. Hence [Data Date].[Calendar].CurrentMember is [Data Date].[Calendar].DefaultMember is [Data Date].[Calendar].[All]. It runs the following code:
Sum(
PeriodsToDate(
[Data Date].[Calendar].[Calendar Year],
[Data Date].[Calendar].[All]
),
[Measures].[_Billings]
)
You are choosing two month from different year, thus it's unclear what do you want to return from the PeriodsToDate() function. The data since January 2016 or January 2017? Choosing one of the members will fix your problem and make it clear.
I don't guarantee that's the perfect code to work with, but it may does the magic as well, add yet another calculated members to sum months:
Sum(
existing [Data Date].[Month].[Month].Members,
[Measures].[Billings]
)

Calculating Weekly Average by user given daily data input in excel pivot table

So I started using Pivot Tables a few weeks ago, but I'm pretty decent at Excel otherwise. I hit an issue that should be an easy fix and I don't see it. I have a document that is tracking Key Performance Indicators for my warehouse packing department. I have 2 tabs in the document that matter (Input Log, and Analysis).
Input log is basically copied from a report generated from my warehouse system. Gives me a USER, DATE, HRS WORKED, ORDERS PACKED, ITEMS PACKED. Using a pivot table I want to see the average hours worked by week for each user.
Currently I can only see the Sum of the hours works and the daily average. How do I also see the weekly average?
[InputLogData][2]
I was able to get the workbook hosted on google drive Packing KPI Workbook
If you create the pivot table, but select to "add to data model",
you will see, in the "Value Fields Settings" selection, a Distinct Count item.
You can then do a Distinct Count of the WE column, and add a column to divide the total hours worked by User, by the total number of Weeks. (See the formula in D4: =B4/C4 )
EDIT: If you want to have the results within the Pivot Table itself, you can add a few calculated columns to your input log.
WE per User is the Unique number of weeks each user works. This formula is an array formula and must be entered by holding down ctrl + shift while hitting enter. Excel will place braces {...} around the formula seen in the formula bar:
=SUM(--(FREQUENCY(IF(User=D2,WE),WE)>0))
Hrs Worked This Wk is the hours worked in the week by the user. This is not really needed, but is in there for when I was troubleshooting the calculations.
=SUMIFS(Hrs_Worked,WE,A2,User,D2)
This will be used to calculate the average for the AvgHrs/Wk column:
=SUMPRODUCT((User='Input Log'!$D2)*Hrs_Worked/WE_Per_User)
This can then be used to create the Pivot Table:
In the Pivot Table, the
Total Hrs is the SUM of Hrs Worked
Hrs-Daily is the Average of Hrs Worked,
Weekly Hrs is the Average of AvgHrs/Wk

Resources