PowerBI: Find all records valid on specific date - powerbi

I have a classic datawarehouse with records. Each record has a valid_from and a valid_to date.
Now I want to set a filter in PowerBI. The idea is that the user somehow sets a date and all records for which this date falls between valid_from and a valid_to should be available in PowerBI. Thus granting the user the possibility of timetravelling through the data.
This seems like a very standard task but I can't find how to do it.... Suggestions?

Given the vague question without explicit details, I'll have to make some assumptions. I'll assume that you have a date slicer that populated from a parameter table unrelated to your data table and that you have a set of measures you use in your visual(s) to display the records you're interested in.
Given those assumptions, you can write a measure to filter your an existing measure along these lines:
FilteredMeasure =
VAR SelectedDate = SELECTEDVALUE ( DateSlicer[Date] )
RETURN
CALCULATE (
[ExistingMeasure],
FILTER (
DataTable,
DataTable[valid_from] <= SelectedDate
&& SelectedDate < DataTable[valid_to]
)
)
Here's another similar but not completely equivalent formulation:
FilteredMeasure =
VAR SelectedDate = SELECTEDVALUE ( DateSlicer[Date] )
RETURN
CALCULATE (
[ExistingMeasure],
DataTable[valid_from] <= SelectedDate,
DataTable[valid_to] > SelectedDate
)

Related

Allowing a user to choose a variable in a formula with an OR conditional

I have a formula for a column: it reports true if the record was completed this month or is still uncompleted, false if it was completed earlier than this month.
CreatedCompletedFilter =
IF (
OR (
MONTH ( [Completed date] ) = MONTH ( TODAY () ),
[Completed date] = BLANK ()
),
TRUE,
FALSE
)
How do I create a report so that, instead of using MONTH(today()), a user can choose a month to apply to this formula via the report itself? I haven't been able to get a slicer to incorporate the OR aspect of formula.
In general you would use a separate, unrelated Date table and then use
MONTH(SELECTEDVALUE('Date'[Date]))
instead of
MONTH(today())
However, your DAX expression doesn't look like a calculated column formula. Would be good to share some sample data.

PowerBI CALCULATETABLE, FILTER with SELECTEDVALUE

Am creating a table from original with filter condition, where my filter value is from SELECTEDVALUE
Table is not getting filtered on SELECTEDVALUE, if I replace with a real value then it works.
Code (doesn't work)
TransGt5 =
var seletectedQuanity = SELECTEDVALUE(QuantityFilter[Quantity])
return CALCULATETABLE(
Transactions,
FILTER(
ALL(Transactions),
Transactions[Quantity] >= seletectedQuanity
))
Code works file with hard coded value:
TransGt5 =
var seletectedQuanity = SELECTEDVALUE(QuantityFilter[Quantity])
return CALCULATETABLE(
Transactions,
FILTER(
ALL(Transactions),
Transactions[Quantity] >= 3
))
what am doing wrong?
That's not how Power BI works conceptually.
DAX can be used in 2 ways: to query data, and to define data (similar to SQL).
For queries, you can create DAX measures. They are executed in the run time and can respond to slicers and other user actions.
For calculated tables and columns, you can also write DAX code, but it's executed only in design time, when you create the code or refresh the data. It does not run as a query, and can not respond to user actions. The fact that you used DAX to create a table is irrelevant - the result is a static table, identical to the imported tables.
The only way to make it work is to build a measure. Inside measures, you can calculate tables, store them in variables, use them to calculate whatever you need and then publish a result. The result will be responsive to slicers.
For example, it could be something like:
Example =
VAR seletectedQuanity = SELECTEDVALUE ( QuantityFilter[Quantity] )
VAR FilteredTable =
CALCULATETABLE (
Transactions,
Transactions[Quantity] >= seletectedQuanity )
VAR Result = SUMX ( FilteredTable, Transactions[Quantity] )
RETURN Result
(although for this example, there are easier ways to achieve the same results, without the calculated table)

Count managers without sales

I have 3 tables:
Sales (date, manager_id)
Manager (manager_id, employment_date)
Calendar (date)
I need to calculate distinct count of managers without sales on a certain date. I tried this approach:
CALCULATE(
DISTINCTCOUNT(Manager[manager_id]),
FILTER(
Manager,
EXCEPT(
VALUES(Manager[manager_id]),
VALUES(Sales[manager_id])
)
)
)
but it didn't work.
Question 1: What do I need to fix to make it work or maybe there is a better logic?
Question 2: How to add a condition to count only managers who's been already employed on that date?
I'm assuming that the Calendar table has a relationship with the Sales table on date.
Q1
You can create 3 measures:
Managers= COUNT(Manager[manager_id])
Managers with sales = DISTINCTCOUNT(Sales[manager_id])
Managers without sales = [Managers] - [Managers with sales]
Or put it all into one measure:
Managers without sales=
VAR managers = COUNT(Manager[manager_id])
VAR managersWithSales = DISTINCTCOUNT(Sales[manager_id])
RETURN managers - managersWithSales
Q2:
To do that, you need to get the selected date and then pass it as a filter:
EmployedManagersWithoutSales =
VAR selectedDate = SELECTEDVALUE(Calendar[date])
VAR managers = CALCULATE(COUNT(Manager[manager_id]), Manager[employment_date] <= selectedDate )
VAR managersWithSales = DISTINCTCOUNT(Sales[manager_id])
RETURN managers - managersWithSales

Establish a Habit Score based on number of months with an action per unique user

I'm trying to establish a habit score which indicates +1 for every calendar month with an action and -1 for every calendar month with no action by user. The tricky part to this is I would like to do this per user starting after an indicated date (different for every user). If an action is done twice in the same month, I only want it counted as +1. The below image will help illustrate the logic and desired output.
I need like the result to appear as a calculated column in a separate table with distinct users (this table is already created, I just need the habit score added).
This is probably not the smoothest solution but it seems to work on the testdata you've provided.
N.B: I've assumed that there is a calendar dimension table, in my case called 'Calendar'.
First add a calculated column to the 'Activity' table:
Year-Month =
FORMAT([Activity_Date]; "YYYY-MM")
Then add the desired calculated column to the 'User' table:
Habit =
var minDate =
CALCULATE(
MIN('Activity'[Activity_Date]);
'Activity'[Indicator] <> BLANK()
)
var maxDate =
CALCULATE(
MAX('Activity'[Activity_Date])
)
var calendarActivity = DATEDIFF(minDate; maxDate; MONTH)
var sumAddOne =
CALCULATE(
DISTINCTCOUNT('Activity'[Year-Month]);
'Calendar'[Date] >= minDate;
'Calendar'[Date] <= maxDate
)-1
var sumSubtractOne = calendarActivity-sumAddOne
return
IF(
minDate <> BLANK();
sumAddOne-sumSubtractOne;
BLANK()
)
Then I end up with a 'User' table lilke this:

Power bi - User retention rate calculation

I'm fairly new to Power bi and have tried and searched for this almost all the forums but couldn't find anything similar to mine.
So.. I have a table like the following (Something similar)
I would like to calculate the retention rate of the users (who actually came back).
What I have done so far:
RetentionRate = (ReturningUsers / PreviousDayDistinctUsers)*100%
ReturningUsers = DistinctUsers - NewUsers
PreviousDayDistinctUsers = CALCULATE(DISTINCTCOUNT(table[User], PREVIOUSDAY(table[Date])
NewUsers = CALCULATE(DISTINCTCOUNT(table[User] ), table[MonthlyNewUsers] = BLANK () )
The above looks to be working, but the only drawback was with the PreviousDayDistinctUsers as it is only considering the previous day (Not all the days from the starting to that day).
So how do I write a measure to calculate the DistinctUsers for all the days until today?
PreviousDayDistinctUsers =
VAR Current_Day = LASTDATE ( table[Date] )
RETURN
CALCULATE ( DISTINCTCOUNT ( table[User] ), table[Date] < Current_Day )
How it works:
First, save last date in a filter context into a variable (instead of LASTDATE, you can also use MAX function).
Second, filter table User by all dates that are less than the saved date, and count distinct users in the filtered table.