Hard coded filter in PowerBi measure - powerbi

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
)

Related

DATEADD throwing an error when adding MAX

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.

Cumulative measure with a conditional reset

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.

count distinct value filtered based on date diff measure

I have the following dataset
user_id, login_date
111, 01/02/2021
222, 02/15/2021
444, 02/20/2021
555, 01/15/2021
222, 03/10/2021
444, 03/11/2021
I want to count of the number of unique active user_id in the last 90 days based on the max date of my date slicer. I'd like to solve this without using filters. This also needs to be dynamic as max date can be change from the date slicer.
From what I have understand so far I will need to evaluate for each row if the date difference between the current date of the row and the max date of the slicer is less than 90 days. Then for all the rows where the date diff is less than 90 days I will want to count the distinct number of users.
so basically I will have three layer in my final formula
evaluate the date diff.
filter out rows where the date diff is superior to 90 days.
count the distinct users in the remaining rows.
I've tried many approach and formula. I think that this one is closed to something that could work:
Measure test = CALCULATE(SUMX(DISTINCT(mytable[user_id]),filter(mytable,DATEDIFF(SELECTEDVALUE(mytable[login_date]),[Max range date],DAY)>90)))
this formula return me the following error :
The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
I've also tried applying a if statement to output the date diff as 0 and 1 and hopefully being able to sum this for each unique id with something like this
SUMX( VALUES(my_table[user_id]), IF(DATEDIFF(SELECTEDVALUE(mytable[login_date]),[Max range date],DAY)>90,0,1)
anyway I'm kind of stuck. hopefully my question is clear enough.
You will have to create a disconnected table from which you will use the date column in the slicer, I have prepared a power BI file which included 2 very common scenarios, I hope that helps you.
File - Simon.pbix
Screenshot of the report - https://ibb.co/xjrjVv5
Screenshot of the model - https://ibb.co/Ws1z8D7
DAX Code -
for simon =
IF (
ISINSCOPE ( simon[Login Date] )
|| ISINSCOPE ( simon[User ID] ),
VAR LastVisibleDate =
CALCULATE (
MAX ( 'Simon Date Table'[Login Date] ),
ALLSELECTED ( 'Simon Date Table' )
)
VAR CurrentDate =
MAX ( simon[Login Date] )
VAR TimeJump = 90
VAR Result =
CALCULATE (
DISTINCTCOUNT ( simon[User ID] ),
simon[Login Date] <= LastVisibleDate,
simon[Login Date] > LastVisibleDate - TimeJump,
ALLSELECTED ( simon )
)
RETURN
Result
)
second version:
for simon 2 =
IF (
ISINSCOPE ( simon[Login Date] )
|| ISINSCOPE ( simon[User ID] ),
VAR LastVisibleDate =
CALCULATE (
MAX ( 'Simon Date Table'[Login Date] ),
ALLSELECTED ( 'Simon Date Table' )
)
VAR CurrentDate =
MAX ( simon[Login Date] )
VAR TimeJump = 90
VAR Result =
IF (
CurrentDate <= LastVisibleDate,
CALCULATE (
DISTINCTCOUNT ( simon[User ID] ),
simon[Login Date] <= CurrentDate,
simon[Login Date] > CurrentDate - TimeJump,
ALLSELECTED ( simon )
)
)
RETURN
Result
)

PowerBI - DAX - Cumulative Measures - Multiple Series on same line chart

How can I write a DAX cumulative measure that - not only - works over time - but also allows me to draw a line for different categories?
As soon as I drag a category into the 'Legend' of the line chart it does not work - all lines represent the cumulative of all categories.
this is what i expect based on my data ...:
Here is my DAX: The resulting 'total' cumulative is correct - but it does not break it down by legend / category?
CALCULATE(
SUM( [VALUE] ),
FILTER(
ALLSELECTED( Fact ),
Fact[Date] <= MAX( Fact[Date] )
)
)
Simply add a Category to the filter:
CALCULATE(
SUM( Fact[Value] ),
FILTER(
ALLSELECTED( Fact ),
Fact[Date] <= MAX( Fact[Date] )
&& Fact[Category] = MAX( Fact[Category] ) // <- This line
)
)

YTD Average of Measure

Can anyone help me in creating a YTD Average % Calculation?
I have created 'A','B' by using the following DAX
A = DIVIDE([Current Month W/Allowance Over 90 $],[Aging],0)
B = DIVIDE([UnderBill],[OverBill],0)
Now I need to create a YTD Average % calculation based on the above two calculations.
This is what I am looking for
YTD A = Average of 'A' ( This average should be YTD)
YTD B = Average of 'B' ( This average should be YTD)
enter image description here
So if we look at YTD A for 08/01/2019, in excel I did the Average =AVERAGE(C2:C9) and the result is 65% and for next month it should be =AVERAGE(C2:C10) and the result is 61%
Assuming there is a Calendar table which has Calendar[Year] and Calendar[YearMonth] columns, here is a possible solution.
YTD A =
-- Calculated table which has start and end dates of each YTD months
VAR YearMonthsYTD =
ADDCOLUMNS(
SUMMARIZE(
FILTER(
ALL( 'Calendar' ),
'Calendar'[Year] = MAX( 'Calendar'[Year] )
&& 'Calendar'[Date] <= MAX( 'Calendar'[Date] )
),
'Calendar'[YearMonth]
),
"#StartDate", CALCULATE( MIN( 'Calendar'[Date] ) ),
"#EndDate", CALCULATE( MAX( [Date] ) )
)
-- Calculate the value for each month, and get the average of them
RETURN
AVERAGEX(
YearMonthsYTD,
CALCULATE(
[A],
FILTER(
ALL( 'Calendar' ),
'Calendar'[Date] >= [#StartDate] && 'Calendar'[Date] <= [#EndDate]
)
)
)
BTW, although I'm ignorant in accounting, I'm doubting if your calculation logic is correct, because average of percentages will not be an appropriate measure in general.
In the presented scenario, wouldn't this definition be more appropriate?
YTD A = DIVIDE( <YTD value of numerator>, <YTD value of denominator> )
This is not only to make it correct, but also to make it much easier.
YTD A = CALCULATE( [A], DATESYTD( 'Calendar'[Date] ) )
Forgive and forget if this is not a relevant comment for the specific case of OP.