DAX Dynamic Date Averages - powerbi

I am trying to create a single measure that will calculate a date average, depending on the drill down on a time barchart.
The bar chart has 5 time pieces in the hierarchy. Year - Quarter - Month - Week -Day
As I drill through the bar chart, I want a measure that will dynamically calculate the Yearly Avg - Quarterly Avg - Monthly Avg, Weekly Avg, Daily Avg.
For example, lets say the barchart is on the Year Level and displaying 4 bars representing 2016-2019. The vale of the measure would be COUNT(UnitID) / 4 because there are 4 bars currently displayed on the X axis.
Lets drill into 2018 to the month level. There are 12 bars representing Jan-Dec. The value of the measure would be COUNT(2018 UnitIDs) / 12 because there are 12 bars currently displayed on the X axis.
Lets drill into 2019 to the month level. There are only 10 bars representing Jan-Oct. The value of the measure would be COUNT(2019 UnitIDs) / 10 because there are only 10 bars currently displayed on the X axis.
Finally, lets not drill down, but just expand the hierarchy from year to month. We go from the yearly view showing 4 bars to the monthly view showing 12. But the Jan total is the sum of 2016Jan + 2017Jan + 2018Jan + 2019Jan. The measure needs to interpret this as COUNT(All UnitIDs) / 12 because there are 12 months currently displayed on the X Axis.
I'm basically trying to figure out how to create a measure that counts whatever number of values are on the X axis at any given time.
Thank you all for your advice and feedback. I'm really looking forward to testing your responses and seeing if they work!

It sounds like you are looking for a way to show the same value for each year, quarter, month, etc.
I wonder how this would make sense as a visual, but if that is the case, your measure would be something like this.
Average Count UnitIDs in Drilldown Periods =
-- At which level the report is drilled down?
VAR CalendarDrilldownLevel =
IF(ISFILTERED('Calendar'[Day]), "Day",
IF(ISFILTERED('Calendar'[Week]), "Week",
IF(ISFILTERED('Calendar'[Month]), "Month",
IF(ISFILTERED('Calendar'[Quarter]), "Quarter",
IF(ISFILTERED('Calendar'[Year]), "Year", "None")))))
-- Total count of UnitIDs in the entire period
VAR TotalCount = CALCULATE([Count of Unit IDs], ALLSELECTED('Calendar'))
-- Number of years, quarters, months, etc. based on the drilldown level
VAR CountOfPeriods = CALCULATE(
SWITCH(
CalendarDrilldownLevel,
"Year", DISTINCTCOUNT('Calendar'[Year]),
"Quarter", COUNTROWS(
DISTINCT(
SELECTCOLUMNS(
'Calendar',
"Year", 'Calendar'[Year],
"Quarter", 'Calendar'[Quarter]
)
)
),
"Month", COUNTROWS(
DISTINCT(
SELECTCOLUMNS(
'Calendar',
"Year", 'Calendar'[Year],
"Quarter", 'Calendar'[Quarter],
"Month", 'Calendar'[Month]
)
)
),
-- Similar lines follow for weeks, days, and in case of no drill down.
),
ALLSELECTED('Calendar')
)
RETURN DIVIDE(TotalCount, CountOfPeriods)
Maybe you are trying this to show average line in the visual? In that case, you can just define a simple measure like COUNT('Your Table'[UnitID]) and add an average line in column chart visual setting. (You can find that in Analytics section)

Related

what kind of measure or column will use here

[Please refer to the below image. I want to create a stacked column chart but I don't have a column that includes 0-30, 30-60, 60-90, etc. How did I put this range on X-axis?
And I didn't understand what kind of measure is used for "LAST 3 MONTH average" & "LAST 12 MONTH average ".
Can you please assist me with the same? ]
https://i.stack.imgur.com/AEnoa.png
Here is Microsoft documentation on using grouping and binning features in Power BI: https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-grouping-and-binning
You can create a measure for last 3 month average like this:
3 Month Average = CALCULATE(
AVERAGE(Table[value]),
FILTER(Table,
Table[date] >= DATE(YEAR(TODAY()),MONTH(TODAY()-3),DAY(TODAY()))
)
)

Calculate Rolling Day Total in PowerBI/Dax

I have a table in PowerBI that has the column "Date" and "Sales". I want to create a measure and display it in a table that computes a rolling 7 day total of the "Sales" column. To be clear, I want to see this overtime, I do not want it for a single day, I want to create a table exactly like I am showing below, thanks!
Rolling total can be make using the quick measure feature underneath the New Measure & New Column buttons in the Home Tab.
Select Calculation -> Rolling Total in the Totals Section
If not then you can make a formula (Both for Measures):
Rolling Total =
CALCULATE(
SUM('Sheet1'[Sales]),
FILTER(
ALLSELECTED('Sheet1'[Date]),
ISONORAFTER('Sheet1'[Date], MAX('Sheet1'[Date]), DESC)
)
)
Rolling Total 2 =
CALCULATE(
SUM(Sheet1[Sales]),
DATESMTD(
Sheet1[Date])
)

DAX: Monthly AVG

I am looking for the "best practice" when it comes to calculating Monthly Averages in DAX. Ideally, I would like to use some of the DAX built in Time Intelligence Functions.
I have a measure called "Total Units". Total Units = COUNT(Table[UnitId])
Each row in my table represents when a single unit was sold.
When I put my Total Units and Sales Date into a bar chart, I can see how many units were sold every month.
How do I now calculate the monthly average?
Month Total Units (Sold)
Jan 2019 10
Feb 2019 30
I want a measure that will tell me that the Monthly AVG is (10+30)/2 = 20
There are a couple of ways to solve this. Assuming you have a [Month] column on your table, you could simply do:
MonthlyAvg = DIVIDE ( [Total Units] , DISTINCTCOUNT( 'Table'[Month] ) )
In other words, simply dividing the total with the number of months considered.
If you have a more "proper" data model, with a separate calendar dimension, you should do something like:
MonthlyAvg = AVERAGEX( VALUES( 'Calendar'[Month] ) , [Total Units] )
All you need is a simple average measure as below,
Total Avg = AVERAGE(Total Units (Sold))
When you select any period for date, measure will adjust and provide the average for the selection.
Let's say you select for year 2019 and Months are Jan, Feb, March
(Jan + Feb + March)/3
Hope it helps!!

Monthly sales trend in Power BI based on working days

I have daily sales data and I would like to create a measure or a column that calculates the monthly sales trend based on the average daily units sold in the month.
For example, as of 08/17/2017, we have had 12 working days this month and 360 units sold. This is an average of 30 units per day. So to calculate the trend manually, I would take 30 units/day and multiply it by the 23 total working days in the month for a trend of 690 units sold.
Here is some sample data.
EDIT: Adjusted the explanation and measure definitions based on the new info that was added to the post
You can create three measures to help you reaching your final result. In this example I'm merely splitting the measure into three parts to increase the readability. You can put it all together in a single measure if you like.
The first measure gives you the number of units sold. This is just a simple sum of units sold, since you have already added a month filter to your measures in your sample:
Sales:=SUM('SalesTable'[Items Sold])
The second measure gives you the number of working days for the filtered month up until the latest entry in your Sales Table:
WorkingDaysToDate:=
CALCULATE(
COUNTROWS('DateTable'),
FILTER(
'DateTable',
'DateTable'[Date] > CALCULATE(MAX('SalesTable'[Date]))
&& [Type] = 'Work Day'
)
)
The third measure gives you the total number of working days for the filtered month:
WorkingDays:=
CALCULATE(
COUNTROWS('DateTable'),
FILTER(
'DateTable',
[Type] = 'Work Day'
)
)
In the end you can combine the three measures to get your month trend:
MonthTrend:=DIVIDE([Sales], [WorkingDaysToDate]) * [WorkingDays]
Total Month To Date Sales
TotalMTDSales = SUM('Sales Table'[Items Sold])
Total Month To Date Work Days
TotalMTDWorkDays = CALCULATE(
COUNT('DateTable'[Date]),
FILTER(ALLSELECTED('DateTable'), 'DateTable'[Date]<=MAX('SalesTable'[Date]) &&
'DateTable'[Type] = "Work Day"))
Total Work Days
TotalWorkDays = CALCULATE(
COUNTROWS('DateTable'),
FILTER(
'DateTable',
'DateTable'[Type]= "Work Day"))
Trend = ([TotalMTDSales] / [TotalMTDWorkDays] ) * [TotalWorkDays]
Filter by Month

PowerBI Adjusted Cumulative Total Measure

I've got a measure called _CumulativeBudget that has the following formula:
_CumulativeBudget = CALCULATE(SUM('Sheet1'[2017 Budget]), FILTER(ALLSELECTED('Sheet1'), 'Sheet1'[Imp Month]<=MAX('Sheet1'[Imp Month])))
Which you can see as the line in the following chart:
The problem is that each of those bars represent the END of a cash flow. For example, there is a project that was allocated $0.2M in budget whose "Imp Month" (Impact Month) is March. But really, the team was given all $0.2M at the beginning of the year and probably spent about 1/3 of that money in Jan, 1/3 in Feb, and 1/3 in Mar.
So if a project has an Imp Month of X, I'd like the cumulative measure to sum 1/X of it's budget in Jan, 1/X in Feb, etc.
Is this something that's fairly doable in a measure? If not, I may just go with a straight line approximation--e.g. if the year-end number is $1.9M then the budget is approximately 1/12th of that each month.
Thanks in advance for any assistance, guys!
I started with some mock data that looks like this.
1) I created a table of months.
Months = SELECTCOLUMNS(
FILTER(
CALENDAR(DATE(2017, 1, 1), DATE(2017, 12, 31)),
DAY([Date]) = 1
),
"Month", [Date]
)
2) I created a calculated table that crossjoined the two tables and filtered it down based on comparing Imp Month to the Month from the month table. At the same time, I limited down the columns and created a new column for the monthly budget.
BudgetExpanded = SELECTCOLUMNS(
FILTER(
CROSSJOIN(Sheet1, Months),
YEAR(Sheet1[Imp Month]) = Months[Month].[Year] &&
MONTH(Sheet1[Imp Month]) >= Months[Month].[MonthNo]
),
"Project", Sheet1[Project],
"Imp Month", Months[Month].[Date],
"Monthly Budget", CALCULATE(
MAX(Sheet1[2017 Budget]) / MONTH(MAX(Sheet1[Imp Month])),
FILTER(Sheet1,
Sheet1[Project] = EARLIER(Sheet1[Project])
)
)
)
3) I created relationships between the tables.
4) I added your cumulative measure to the new table and updated it for the Monthly Budget column.
CumulativeBudget = CALCULATE(
SUM(BudgetExpanded[Monthly Budget]),
FILTER(
ALLSELECTED(BudgetExpanded),
BudgetExpanded[Imp Month] <= MAX(BudgetExpanded[Imp Month])
)
)
5) I created a combo chart similar to yours with the original 2017 Budget and the new CumulativeBudget (black line is the new measure; red line is your original measure).