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]) )
Related
I'm using Power BI, in directQuery mode for all my tables (other than my date table).
I can't change that.
To simplify, I have a fact table that has an id, a date field and a sales figure (not even using the sales for this question.. just the fact that there is a sale on this date).
The date field relates to my date dimension as you'd expect.
I have created a measure, which is a rolling 2 year count of sales (not sum, just count).
Rolling_year_sales_count = CALCULATE(
DISTINCTCOUNT( fact[sale_id]),
DATESINPERIOD(
dim_date[yyyy_mm_dd],
LASTDATE(dim_date[yyyy_mm_dd]),
-2,
Year
))
this looks good when I put it on a simple table - its properly counting the rolling year counts.
Now, I want to create a rolling 2 year AVERAGE count.
So the result should show 2 for 1925, 5 for 1926, 8 for 1927... (Add the 2 previous year's rolling counts, and divide by 2)
I tried just including this measure in a new measure that uses SUMMARIZE - but because I am using DirectQuery, I cannot refer to calculated measures in my SUMMARIZE.. Same goes for CALCULATE.
So how can I calculate this average, knowing that I'm in directQuery mode?
Thanks!
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.
I am trying to calculate the Cumulative Purchases by YTD. The first step is to rank the items by Cost Amount, but when I try to rank by the [_YTD Cost] measure, the numbers I get do not make sense (skipped numbers, duplicated).
[]
I had 3 slicers: Month, Year and to select Month/YTD measures. Since with the Month calculation I have no problems, I removed the interaction with the Month/YTD slicer and I placed only YTD measures on the table:
Total Purchase Cost = SUM ( Purchases[Amount] )
_YTD Cost = TOTALYTD([Total Purchase Cost], 'dim-calendar'[Date])
_RANK YTD = RANKX(ALLSELECTED(Purchases), [_YTD Cost])
Notes:
I pulled the item from the Item table
The Purchase table is linked to the Date table by Purchase Date
Not 100 % sure on your data model, but try changing your RANKX(ALLSELECTED(***) to reference the Item-column. Like this:
RANKX(ALLSELECTED('Item'[Item]), [_YTD Cost])
I have put on the dashboard of Power Bi a matrix with years on columns, district areas on rows and total actual revenue, absolute growth and percentage growth on the last year as values. Why don't I see the absolute growth for each district? I'm using also years as parameter
M-CY_Sales = CALCULATE(sum('Detail Sales Report'[Total Actual Revenue - Base]);filter('Detail Sales Report';'Detail Sales Report'[fiscal Year]=CurrentYear[CurrentYearValue]))
M-PY_Sales = CALCULATE(sum('Detail Sales Report'[Total Actual Revenue - Base]);filter('Detail Sales Report';'Detail Sales Report'[fiscal Year]=CurrentYear[CurrentYearValue]-1))
M-PY2_Sales = CALCULATE(sum('Detail Sales Report'[Total Actual Revenue - Base]);filter('Detail Sales Report';'Detail Sales Report'[fiscal Year]=CurrentYear[CurrentYearValue]-2))
M-PY_Growth = calculate([M-PY_Sales]-[M-PY2_Sales];'Detail Sales Report')
M-PY_Growth% = DIVIDE([M-PY_Growth];ABS([M-PY2_Sales]))
M-CY_Growth = [M-CY_Sales]-[M-PY_Sales]
M-CY_Growth% = DIVIDE([M-CY_Growth];ABS([M-PY_Sales]))
I believe the issue is here:
M-PY_Growth = calculate([M-PY_Sales]-[M-PY2_Sales];'Detail Sales Report')
Using calculate in this form has an implicit inclusion of the ALL() function around the filter term which will strip out the context of the rows the measure sits within.
To override this try:
M-PY_Growth = calculate([M-PY_Sales]-[M-PY2_Sales];KEEPFILTERS('Detail Sales Report'))
If that doesn't work you may need to experiment with ALLEXCEPT() to selectively keep district as a filter but allow the year context to be adjusted.
I have a simple Power BI table that looks as follows:
I have two tables. A Date table and an Invoice table with a field representing invoice amounts. This is a 1-M relationship on Invoice.InvoiceDate.
The second column is simply a measure for the sum of invoices. The third and fourth columns are measures using ParallelPeriod to sum invoices for 12 months prior and 24 months prior. Even though these numbers are correct, I'm not entirely certain I know what's actually going on.
The measure for the 12-month parallel period looks like this:
Sum Invoice Amount 12 Months Ago =
CALCULATE (
SUM ( FactCustomerTransaction[InvoiceAmountDollars] ),
PARALLELPERIOD ( 'Date'[Date], -12, MONTH )
)
Here is what I think is happening. When the sum is calculated for say 2015-Feb, all values for that month are retrieved in the invoice (many side) table and summed to generate the "Sum Invoice Amount". The sames dates, minus 12 months, in the Date table, are retrieved and the same sum is generated for those range of dates for 'Sum Invoice Amount 12 Months Ago'. And then the same process for 24 months ago.
This works because of the 1-M relationship between Date and Invoice. Is this correct?
For 2015-Feb row, assuming the Year and Month Name column is on your 'Date' table, your filter context is 'Date'[Year and Month Name] = "2015-Feb". This filter corresponds to the dates 2015-02-01 through 2015-02-28 in your 'Date'[Date] column and that filtering propagates across the relationship to return only the rows in FactCustomerTransaction table where InvoiceDate is one of those dates and then sums the amounts corresponding to only those rows.
When you add PARALLELPERIOD, it works the same way except that after matching the dates 2015-02-01 through 2015-02-28 corresponding to 'Date'[Year and Month Name] = "2015-Feb", it shifts those dates back by 12 months and then propagates those shifted dates across the relationship.