I have a set of data that looks something like the attached, I would like to create a matrix that allows me to calculate the average size by the number of months selected. For example, if the filter is set on Jan and Feb, the average for city abc would be zone 1 (848) and zone 2 (306). TIA
Updated after your comment:
Given a table called Sheet1 such as this:
Create a New Measure to calculate the quantity of months between the first month and the last one. If you have one entry per months this should work:
Months =
var MonthStart = CALCULATE(Year(FIRSTDATE(Sheet1[month]))*12+Month(FIRSTDATE(Sheet1[month])),ALLEXCEPT(Sheet1,Sheet1[month]))
var MonthEnd = cALCULATE(Year(LASTDATE(Sheet1[month]))*12+Month(LASTDATE(Sheet1[month])),ALLEXCEPT(Sheet1,Sheet1[month]))
RETURN
MonthEnd-MonthStart+1
Create a New Measure to sum over all values and divide it by the [Months] measure.
TotalAmount = sum(Sheet1[size])/[Months]
This should yield the results:
Related
I have production figures which sum the quantity of parts produced by each workstation on a weekly basis. These are shown in a matrix with workstation number on the rows and week numbers across the columns.
I want to be able to select 2 weeks and then
Display all the weeks in the range selected by a slicer.
Show the quantity values for only the first and last selected weeks.
The first part is working using a simple Week Slicer.
The second part, however, always shows the totals for all the selected weeks rather than the 2 individual week quantities.
In this example I have selected weeks 4 and 9 and therefore the expected values for quantities are 11,505 and 49,425 as shown in the Good Qty data with red frames.
The measures to extract the fist and last selected week numbers are working:
SelWeek1 = FIRSTNONBLANK(VALUES(vLink[Week]),0)
SelWeek2 = LASTNONBLANK(VALUES(vLink[Week]),0)
My measures for the week quantities are:
IF([SelWeek1]>0,
CALCULATE([Sum vGood Qty],FILTER(vLink, vLink[Week] = [SelWeek1])),
0
)
and
SelWeek2 Qty =
IF([SelWeek2]>0,
CALCULATE([Sum vGood Qty],FILTER(vLink, vLink[Week] = [SelWeek2])),
0
)
What am I missing?
Try to use below measures:
SelWeek1 = MIN(vLink[week])
Measure =
VAR _selWeek = [SelWeek1]
VAR result =
CALCULATE(
[Sum vGood Qty],
vLink[Week] = _selWeek
)
RETURN
result
and for selected week 2 change min to max in the first measure and _selWeek variable in the second measure respectively.
In my fact table (fTable) the columns I have are dates, region and sales.
dates
region
sales
-----
------
-----
I am visualizing the data in a pivot table with regions as rows and months as columns (I have a date table (dDate) with a months column in my model)
I am looking for a way to dynamically change the denominator in an averaging measure if a certain region doesn't have sales in a given month. Right now my denominator is hard-coded as 6, because I am averaging 6 variables in my nominator, but any one of them could be 0 if I don't have any sales in a certain month, in which case my denominator needs to change to 5, 4 or less depending on how many months I don't have sales in. So I am looking to count how many of the past 6 months have sales and sum that as the denominator.
I have managed to count months with sales this way:
Denominator:=
var newTable = Summarize(fTable,fTable[date (month)], fTable[region],"Sales",[Sum of Sales])
var MonthsWithSales = Countrows(newTable)
RETURN
MonthsWithSales
I've tried to RETURN
Calculate(SUMX(newTable,MonthsWithSales), Dateadd(dDate[Date],-6,MONTH)
but it yields a wrong result.
Any suggestions?
Thanks
Based on my sample, we can use function VALUES & COUNTROWS inside CALCULATE to get what we need:
Measure = CALCULATE( COUNTROWS(VALUES('Table (2)'[Month])), ALL('Table (2)'[Month]) )
I'm trying to calculate a monthly average number of cases for each investigator. It might be over a quarter, year, or multiple years so it needs to respond to the visual or table context I drop it into. The base table has Case (individual case#), Investigator (person name), Date assigned (not shown), and from that date,month and year columns extracted and a YearMonth categorical column.
I create a caseCount measure as
caseCount = COUNT('Table'[Case])
I've tried several different ways to calculate the average over all months (in this case 4). Because Mary has cases in each month, her average is correct (1.75) but Sam's uses a denominator = 3, thus doesn't calculate correctly. (returns 1.3 instead of 1). How can I force the calculation to use the full number of months.
Additional notes:
There may be cases in the table that fall outside the date range I want so I've tried using a
Avg = CALCULATE(AVERAGE(caseCount), Table[Date] > #10/31/2019#)
I've also tried several variations using CALCULATE(DIVIDE(), [Date] > 10/31/2019. Everything seems to exclude those months when an investigator had no investigations assigned. I also tried connecting to a Date table and using the Distinct YearMonth value created there.
This is because the evaluation context.
I would define the measure as follow:
VAR _casecount = //count number of cases in the selected period, applied on the fact table
VAR _months = COUNTROWS(CALCULATETABLE(VALUES('Calendar'[Month]), ALLSELECTED('Calendar'))) //count number of months in the selected period
RETURN
_casecount/months
Update
I did not consider the scenario of multi-year periods involving May-2019 and May-2020. Then, let's reframe the solution using DATEDIFF:
VAR _casecount = //count number of cases in the selected period, applied on the fact table
VAR _firstCalendarDate = CALCULATE(MIN('Calendar'[Date], ALLSELECTED('Calendar'))
VAR _lastCalendarDate = CALCULATE(MAX('Calendar'[Date], ALLSELECTED('Calendar'))
VAR _months = DATEDIFF(_firstCalendarDate, _lastCalendarDate, MONTH)
RETURN
_casecount/months
I am trying to show the current months sales in a table that also looks at Median, average. The figure returned for each month is correct. However the total is not correct.
SalesMostRecentdaMonth = CALCULATE(Sum('Sales'[Sales]),DATESINPERIOD(Dates[Date],MAX(Dates[Date]),-1,MONTH))
So I figured out the answer it was to create a measure that calculated sales in the previous 12 months (trailing 12 months). The video linked below helped.
https://www.youtube.com/watch?v=duMSovyosXE
Last 12 Months =
VAR CurrentDate = MAX(Dates[Date])
VAR PreviousDate = DATE(YEAR(CurrentDate),MONTH(CurrentDate)-12,DAY(CurrentDate)+1)
VAR Results =
CALCULATE(SUM('Sales'[Sales]),
FILTER(Dates,Dates[Date] >= PreviousDate&&Dates[Date]<=CurrentDate))
Return
Results
I have a data with Dept name and its corresponding Amount for each Dept for each Month like below :
Table1 :
Dept name Amount Period
XXX 20 Jan,2018
XXX 30 Feb,2018
XXX 50 Mar,2018
XXX 70 April,2018
....
YYYY 20 Jan,2018
YYYY 30 Feb,2018
YYYY 50 Mar,2018
YYYY 70 April,2018
....
I need to calculate the Average of Last 3 months (Ex. For Dept XXXX, If I select April Month, It needs to calculate the average Amount of (Jan,Feb,Mar)(20+30+50)/3 =33.33) and Compare the same with current (April) month (70)
I've created a calculated column for Last 3month Average as below (I have also created a Calender Table in Power BI)
AVG3mth =
CALCULATE(SUM('Table1'[Amount]),DATESINPERIOD(Calender[Date],LASTDATE('Table1'[Period]),-3,MONTH))/3
(But it just dividing the current month by 3 and not the Last 3 Mnths.)
and when comparing If the Average of Last 3 months greater than current month I should highlight it as "YES" since the Amount is dropped when comparing to last 3 months. I have added another column as "Dropped?" for the same.
Dropped? = IF(VALUES('Table1'[Amount])<[AVG3mth], "Yes", "No")
And also If I choose the Particular month (Period) in slicer I need to get those Month, Amount, Last 3 months average and Dropped YES/NO alone in my Report.
Attached my current report screenshot (You will get clear idea if you look into this)
Report Screenshot
To do this, you will need 1 Calculated Column and 3 Measures.
First, I created a new column called as MonthDiff (Calculated Column)
MonthDiff = DATEDIFF(MIN(Table1[Period]),Table1[Period],MONTH)
So afterwards, I created the Average for last 3 months Measure
Average Last 3 Months =
Var selectedmonth = SELECTEDVALUE(Table1[MonthDiff])
Var startingMonth = (selectedmonth - 4)
Var selecteddepartment = SELECTEDVALUE(Table1[Dept name])
Return CALCULATE(AVERAGE(Table1[Amount]), FILTER(ALL(Table1), Table1[MonthDiff] > startingMonth && Table1[MonthDiff] < selectedmonth),FILTER(ALL(Table1),Table1[Dept name] = selecteddepartment))
So, then you can create the current selected value Measure
SelectedAmount = SELECTEDVALUE(Table1[Amount])
Then you can create the drop Measure
Drop = var currentvalue = SELECTEDVALUE(Table1[Amount])
Var selectedmonth = SELECTEDVALUE(Table1[MonthDiff])
Var startingMonth = (selectedmonth - 4)
Var selectedDepartment = SELECTEDVALUE(Table1[Dept name])
Var averagevalue = CALCULATE(AVERAGE(Table1[Amount]), FILTER(ALL(Table1), Table1[MonthDiff] > startingMonth && Table1[MonthDiff] < selectedmonth), FILTER(All(Table1),Table1[Dept name] = selectedDepartment))
Return if(averagevalue > currentvalue, "Yes", "No")
This is my final output,
Do let me know, if this helps or not.
My Best Practice
When you are dealing with Measures that involves multiple filters,
it's best to declare them using Var and test it by returning the
output on the card visual as you develop the measure.