Week Over Week change measure - powerbi

I have 2 tables, a date table that consists of the following fields:date, year, yearmonth, month, day, WeekNum
The second table Consists of the following fields: Name, Cost, Date.
The tables are linked by the date measure.
I want a dax measure that can give me week over week change in cost.
I already have a month over month change in cost measure but the same logic can't be applied here because "dateadd" does not give me "Week addition".
Week over Week calculation gives me wrong results.
Week Over Week = CALCULATE(
SUM ( UsageDetails[Cost] )
- CALCULATE (
SUM ( UsageDetails[Cost] ),
FILTER (
ALL ('Date' ),
'Date'[Year] = YEAR( NOW() )
&& 'Date'[Week Number]
= MAX ( 'Date'[Week Number] ) - 1
&& 'Date'[Week Day] = MAX ( 'Date'[Week Day] )
)
))

There is a WeekNum function available to get a week of the year and then you do the math for WOW calculation.
https://learn.microsoft.com/en-us/dax/weeknum-function-dax

Related

PowerBI / DAX - REMOVEFILTERS not working as expected

We are using a date table based on a fiscal calendar. I need to determine if a particular year has 53 weeks or 52 weeks.
The page has a slicer with a date hierarchy that is used to select a year, quarter, month, or week. I would like the Measure to return the maximum number of weeks in a particular year regardless of what is selected.
The measure below works fine, it returns 53 which is the maximum number of weeks in any year regardless of the selection in the date hierarchy slicer.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate))
However, when I try to add a filter condition to select the proper year it no longer works it returns the maximum week number of the period selected in the hierarchy. If I select January (a 5 week month) it returns 5 instead of 53 then number weeks in fiscal 2021.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate),filter(vwPBIDate,vwPBIDate[Fiscal Year Number]=2021))
You're missing an ALL:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
FILTER(
ALL( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)
)
which is equivalent to:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)

Dax Counts at end of each month

I have a measure that depending on a "before" date slicer shows how many accounts were active at any given point in the company's history. I'm being asked to show month over month growth (end of month 1 compared to end of month 2 totals) but that's difficult given my measure needs a date slicer with one date value to return a total.
Active_Accounts =
CALCULATE (
COUNTX (
FILTER (
VALUES ( 'TEST CHARGES'[BI_ACCT] ),
[total as of date] > 0
),
[BI_ACCT]
)
)
link to sample file
https://www.dropbox.com/s/pewpm85wogvq3xf/test%20active%20charges.pbix?dl=0
if you move the slider you'll see the active accounts total change to show at that time in history how many accounts had an active charge. What I'm hoping to add to the dashboard is a measure that can be placed on a table of month end values and show the active accounts at that time so I can do month to month comparisons.
Active Accounts =
var month_end =
ENDOFMONTH (
LASTNONBLANK (
'Test Charges Date Table'[Date],
CALCULATE ( DISTINCTCOUNT( ( 'TEST CHARGES'[BI_ACCT] ) )
)
)
)
var last_date =
CALCULATE(
LASTNONBLANK('TEST CHARGES'[CHG_DATE], ""),
'TEST CHARGES'[CHG_DATE] <= max('Test Charges Date Table'[Date])
)
var num_of_actives =
CALCULATE(
Countx(
Filter(
Values('TEST CHARGES'[BI_ACCT]),
[total as of date] > 0
) , [BI_ACCT]
),
last_date <= month_end
)
return num_of_actives
As Peter advices you do not need Calculate() to show total in the card and using of Calculate() reduces speed of calculation. In your case speed is reduced with no reason.
There are no need to have a special column for month - use date hierarchy for row and just exclude day and quater levels.
Then add the measure to the visual table/matrix
Cummulative Count =
Calculate(
[Active_Accounts]
,'Test Charges Date Table'[Date]<=MAX('Test Charges Date Table'[Date])
)
Cummulative Count prevMonth =
Calculate(
[Cummulative Count]
,PARALLELPERIOD('Test Charges Date Table'[Date],-1,MONTH)
)

Moving Annual Total with DAX Power BI

I'm trying to calculate MAT (Moving Annual Total) using DAX, but can't realize exactly what I want.
I have the data as below.
What I'm trying to is calculate the total of treatment for each 12 month, like "Jan 21 - Dec 21" which has 'date' from 2020/1/1 to 2020/12/1. i.e. 'A' appears 4 times in that period, so the result will be like below.
Then I'd like to continue this for each latest 12 months, and finally visualize it like below.
I've read that I should use CALCULATE and DATESPERIOD in DAX, can't calculate exactly though. The following code is the one I tried and failed.
Moving Annual Total = CALCULATE(
COUNTA('2017-2022Q1'[idnum]),
DATESINPERIOD('2017-2022Q1'[mat].[Date], LASTDATE('2017-2022Q1'[mat].[Date]), 12, MONTH))
Could someone kindly give me an advise to realize this?
Thanks.
*This is the sample file.
https://drive.google.com/file/d/1gDNeBe5KiKBqx3cZ7G0SMiSQ23w96NF4/view?usp=sharing
In your dax measure, I recommend you first to create a date table and create a one-to-many relationship with the fact table(your table: '2017-2022Q1') ; then filter the date table in your calculate measure, not the fact table('2017-2022Q1') directly as you did here. You should follow the best practice.
For Example: How to create a date table using dax:
Date =
VAR MinYear = YEAR ( MIN ( '2017-2022Q1'[date] ) )
VAR MaxYear = YEAR ( MAX ( '2017-2022Q1'[date] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO( ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Month Name", FORMAT ( [Date], "mmmm" ),
"Month Number", MONTH ( [Date] )
)
Your final code could be like this:
Moving Annual Total = CALCULATE(
COUNTA('2017-2022Q1'[idnum]),
DATESINPERIOD('Date'[Date], LASTDATE('Date'[Date]), -12, MONTH)
)
if you define the start date using the lastdate() function, then It returns the last date in your filter context. you should also specify the interval as (-12) month to extract all dates within 1 year period.
Hope It helps.

How to add a year in a calculated column?

DateTable=CALENDARAUTO()
Calculated columns:
NextYearDate = DATEADD(DateTable[Date],1,YEAR)
The above calculated column shows blanks instead of the next years date. Why is that and how to fix it?
DATEADD is a Time Intelligence function, which returns dates from your date column shifted by the specified interval. This means that for the latest year in your date table, there are no "next year" values to return.
An alternative would be:
NextYearDate =
DATE (
YEAR ( DateTable[Date] ) + 1,
MONTH ( DateTable[Date] ),
DAY ( DateTable[Date] )
)

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.
I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):
SalesBlank = CALCULATE(
COUNT(Vehicles[MVANumber]),
FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))
I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.
How can I set up a measure to look at the date table and filter the vehicles table with this logic:
purchasedate <= date[EOMONTH] && ISBLANK(salesdate)
Any help would be greatly appreciated!!
Thanks,
Matt
Sample Data and Desired Results
Relationships
If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.
You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].
You could use a measure that is something like this:
Inventory Count =
VAR MaxDate =
MAX ( 'Date'[Date] )
VAR MinDate =
CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR Sales =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR MaxPurDt =
CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
IF (
MIN ( 'Date'[Date] ) <= MaxPurDt
|| MIN ( 'Date'[Date] ) <= MaxSlDt,
Purch - Sales
)
This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.
I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).
Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.