I have the need of creating a cumulative measure that resets when a certain condition happens.
The target is to create the 'Longest Stock Out Period' measure. That will be inserted in a matrix visual like this.
Date
No. of Negative Days
Longest Stock Out Period
08-03-2022
0
0
09-03-2022
1
1
10-03-2022
1
2
11-03-2022
0
0
The logic is that 'Longest stock out period' should cumulative sum 'No. of negative days' until 'No. of negative days' is 0, then it should reset.
This is what I have currently tried, which just computes a 0. I believe there should also be some logic in the measure regarding no. of negative days should not be 0 or alike.
Longest Stock Out =
VAR _date =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
CALCULATE (
[No of Days],
FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] <= _date )
)
Try this Measure:
Longest Stock Out Period :=
VAR ThisDate =
MIN( 'Table'[Date] )
VAR DateOfLatestZero =
CALCULATE(
MAX( 'Table'[Date] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[No. of Negative Days] = 0
)
)
RETURN
CALCULATE(
SUM( 'Table'[No. of Negative Days] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[Date] >= DateOfLatestZero
)
)
Adapt so as to use your Calendar dates as required.
Related
I am looking to get the sum of sales for the last 3 months. not including the unfinished month which is this month.
I have this Dax but its not accepting the DATEADD with the MAX. Is there any workaround?
Measure1 = CALCULATE (
SUM ( table1[Sales] ),
DATESINPERIOD (
table1[date],
DATEADD( MAX ( table1[date] ), -1,MONTH),
-3,
MONTH
)
)
DATEADD function requires a set of dates to computation. That is why it not works with MAX, which returns single value. Find more here.
You need to find the maximum date in the dataset, then filter out the current month from the date table. Try the measure as follows:
Measure1 =
VAR maxDate =
CALCULATE(
MAX( 'Calendar'[Date] ),
ALL('Calendar' )
)
VAR firstDay = DATE( YEAR( maxDate ), MONTH( maxDate ), 1 )
VAR maxK =
CALCULATE(
MAX('Calendar'[Date] ),
'Calendar'[Date] < firstDay
)
VAR result =
CALCULATE(
SUM( AmountPaid[PaymentAmt] ),
DATESINPERIOD('Calendar'[Date], maxK, -3, MONTH )
)
return result
The maxK part calculates maximum date excluding the latest month in my dataset. You have to adjust the measure a bit for your needs (e.g. use TODAY() instead maxDate).
Hope it helps.
I have a measure (Users_1) that calculates number of rows between specific dates that have the parameter is_sql = 0.
This measure is used in a table alongside with other measures.
I have 5 more filters on the page that that should affect this specific measure and so I can not use All(users).
One of the filters on this page is "is_sql". And when is_sql = 1 every measure except for measure (Users_1) should change to correspondig value. Measure (Users_1) shold stay the same.
Now when I chose is_sql = 1 the measure (Users_1) is blank.
Users_1 =
CALCULATE(
COUNTROWS( 'users' ),
FILTER(
KEEPFILTERS('users'),
'users'[date (days)] <= MAX( 'Calendar'[Date] )
&&'users'[date (days)] >= MIN( 'Calendar'[Date] )
&&'users'[is_SQL] = 0
)
)
You will want to avoid using KEEPFILTERS on any filters you want to allow to look outside of the filter context:
Users_1 =
CALCULATE(
COUNTROWS( 'users' ),
'users'[date (days)] <= MAX( 'Calendar'[Date] ),
'users'[date (days)] >= MIN( 'Calendar'[Date] ),
'users'[is_SQL] = 0
)
I am having some difficulties in creating a dax formula for calculating prev yr YTD sales.
I have written a formula but the same is not working.
I need to calculate the performance % yr over yr by comparing YTD sales of current year to YTD of prev yr sales.
any help would be appreciated
Sales sameperiod =
VAR first_date =
FIRSTDATE ( DATEADD ( 'Date'[Date], -12, MONTH ) )
VAR last_date =
LASTDATE ( DATEADD ( 'COGS Data'[Invoice Date], -12, MONTH ) )
RETURN
IF (
ISBLANK ( first_date ) || ISBLANK ( last_date ),
BLANK (),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
DATESBETWEEN ( 'Date'[Date], first_date, last_date )
)
)
there are multiple ways, but my go-to is creating a Date table, I assume you already have it.
Then you would create relationship to Fact table from DateKey, and a new matrix visual with rows from Date Table, for example Date and Month. And Measure would be like -
Revenue last year = IF(
HASONEVALUE ('Date'[Month]),
IF (
SUM ('COGS Data'[Final Unit Cost] ) <> BLANK(),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
SAMEPERIODLASTYEAR ('Date'[Date])
)
),
CALCULATE (
SUM ( 'COGS Data'[Final Unit Cost] ),
DATESBETWEEN (
'Date'[Date],
EDATE ( MIN ('Date'[Date]), -12 ),
EDATE ( MAX ('COGS Data'[Invoice Date]), -12 )
)
I want the values in between the years mentioned in the image and get their equivalent sales qty.
The sample data is:
Sample Data
For ex: If I select 2005 in the slicer, I should get the qty ordered as 4+5+6 =15 in the new column or as a measure.
Try with this code:
BetweenRange =
CALCULATE (
SUM ( 'Table'[Sales Qty] ),
FILTER (
ALL ( 'Table'[Start Year], 'Table'[End Year] ),
SELECTEDVALUE ( 'YourFilterTable'[Year] ) >= 'Table'[Start Year]
&& SELECTEDVALUE ( 'YourFilterTable'[Year] ) <= 'Table'[End Year]
)
)
I'd suggest using a variable to read in the slicer value:
SumQuantity =
VAR SelectedYear = SELECTEDVALUE ( Slicer[Year] )
RETURN
CALCULATE (
SUM ( Sales[Sales qty] ),
Sales[Start year] <= SelectedYear,
Sales[end Year] >= SelectedYear
)
After going through several posts on StackOverflow and the PowerBI forums, I still can't figure out how to calculate a rolling average based on a given period- in my case, a 30-day rolling average.
Most of the posts I've seen advocate something either identical or really similar to this:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
(code taken from this post)
...and yet, I can't seem to get the proper values.
In my case, I have the following:
"closing date" for a given loan (column)
loan count (measure)
closing length (column)- length of time (in days) to close a loan
What I'd like to calculate is the rolling 30 day average for any given day. I coded the following:
Rolling Average =
CALCULATE (
SUM(Query1[Closing_Length])/[Loan Count],
FILTER (
ALL ( Query1 ),
[Closing Date].[Date]
>= MAX ( Query1[Closing Date] ) - 30
&& [Closing Date] <= MAX ( Query1[Closing Date] )
)
)
To check the results, I used a visual filter to examine one month's worth of data and these are the results:
Note the totals row at the bottom; for this given period, there are 102 loans and it took an aggregate of 3922 days for them to close. The average I'd like to calculate is 3922/102, which should equal approximately 38.45 days. Instead, we see 42.
How can I fix this?
Measure based solution:
Rolling Average Measure =
VAR A =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Closing Length], 2 )
)
VAR B =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Loan Count], 2 )
)
RETURN
A / B
Calculated column based solution:
Rolling Average =
VAR CurrentDate = 'Query'[Closing Date]
VAR T =
FILTER ( 'Query', [Closing Date] <= CurrentDate )
RETURN
ROUND ( SUMX ( T, 'Query'[Closing Length] ) / SUMX ( T, [Loan Count] ), 2 )
Print Screen: