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!!
Related
When i drag in a tabel
and just have
-YEAR
-Kategory
-Summa
I will get the medium value of 895,50 (815 + 976 / 2) for february
I don't want it to separeate the Subcategory
I want it to show
1791
when i just mark february
and if i use February and March
It sould say
2022 Inköp 2435,5
Becouse that the Medium of February and Marsh on the category
It's difficult to understand what you are asking (likely the reason for your downvotes). I think you are after a monthly average measure. Since your sample data is extremely limited I assume that each month will have exactly one date. Ideally this type of calculation should be handled by using a calendar table as a basis for month values, but for your sample data, try this:
Monthly average =
AVERAGEX (
VALUES ( 'Table'[Datum] ) ,
CALCULATE ( SUM ( 'Table'[Summa] ) )
)
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)
Create a measure to compare forecast vs sales in power BI.
The logic is that at the beginning of Jan. 2018 it is forecasted the sales for the whole year and the amount of pcs sold in Jan. 2018 is only known in Feb. 2018 (next month's file). As an example, we are in Nov. 2018 and it is known how many pcs were sold in the previous months. I want to create a table to compare forecast vs sales.
My dataset looks like the figure below. It is marked in red the historical data.
An example of a power BI matrix that I am trying to create by dividing forecast by sales
It looks like you need to normalize your data source. This may look something like:
Now you can create some measures. Create a Forecast Qty measure:
Forecast Qty:=CALCULATE (
SUM ( SourceTable[Qty] ),
SourceTable[Type] = "Forecast"
)
A Sold Qty measure:
Sold Qty:=CALCULATE (
SUM ( SourceTable[Qty] ),
SourceTable[Type] = "Sold"
)
And a Sales % Forecast Measure:
Sales % Forecast:=DIVIDE (
[Sold Qty],
[Forecast Qty],
BLANK()
)
Now you can lay out your visualisation as you choose. For example:
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
I want to calculate annual revenue for each month from monthly revenue data. Sample data is shown as below:
e.g. For annual revenue of 2015 May = sum of monthly revenue from 2015 Jan to 2015 May, and so on.
The problem is, the Monthly Revenue is a measure. I want to create a measure for Annual Revenue as well, so that it can interact with other filters. However, I only know how to write the expression using Calculated Column:
Annual Revenue =
CALCULATE(
[Monthly Revenue],
FILTER(
'Month',
'Month'[Year] = EARLIER('Month'[Year]) &&
'Month'[MonthKey] <= EARLIER('Month'[MonthKey])
)
)
How can I translate the above expression so that it will work with Measure?
It sounds like what you want is a YTD measure for any given date (i.e. in May 2015, YTD is January-April 2015). I typically wouldn't do this using a [Monthly Revenue] measure and a Month table. I'd do this using a regular date table, a base Revenue measure, and DATESYTD.
However, using the MONTH table as you've outlined, this is what I'd do for a measure:
Annual Revenue Measure =
CALCULATE (
[Monthly Revenue],
FILTER (
ALL ( 'Month' ),
'Month'[Year] = MAX ( 'Month'[Year] )
&& 'Month'[MonthKey] <= MAX ( 'Month'[MonthKey] )
)
)
You'll note it's almost the same as you have for your calculated column, except using MAX rather than EARLIER (since EARLIER only applies to calculated columns).