Adding a filter on date with different relations in Power BI - filter

I have three tables based on year
Lecturer table - the year represents the time the lecturer was
recruited
Student table - the year represents the time the student
first registered
Course table - the year represents the time the
course was given
I added a year table with an updating column (based on the Today function) which calculates how many years have passed since that year.
I would like to create different visualizations, each using a slicer/filter on the updating column with a different context. For students I would like to slice according to their academic year, for lecturers I would like to slice according to seniority and for courses, I want to slice according to how recent they are.
Is there an option to define a slicer/filter that chooses the context of the relationship? I am particularly asking about filters and I prefer not to duplicate yearSinceToday column in all my tables.
sample data
studentId
studentName
registrationYear
s1
John
2022
s2
Jack
2023
s3
Jill
2022
lecturerId
lecturerName
lecturerRecruitementYear
l1
Luke
2019
l2
Leia
2018
l3
Lando
2022
courseId
courseName
coursYear
c1.2022
Python
2022
c1.2023
Python
2023
c2.2022
Java
2022
courseId
lecturerId
c1.2022
l1
c1.2023
l2
c2.2022
l3
c2.2022
l2
courseId
studentId
c1.2022
s1
c1.2022
s2
c1.2023
s2
c1.2023
s3
c2.2022
s3
year
yearSinceToday
2018
5
2019
4
2020
3
2021
2
2022
1
2023
0
I would like to be able to ask questions like:
How many courses from year X there are with a lecturer with seniority at least Y?
How many students of the academic year of at least X take each course from year Y?
and so on

Is there an option to define a slicer/filter that chooses the context
of the relationship?
You don't define the slicer this way - you define your measure. Your "yearSinceToday" table is known as a role-playing dimension and you would create active and inactive relationships between it and your other tables. You would then activate the appropriate relationship using USERELATIONSHIP() and have the measure return the appropriate data to filter on.

Related

How to deal with reporting slowly changing dimensions

For a client I am creating a data warehouse in which we have some slowly changing dimensions (or facts if that is even a thing?). For example we want to report the annually recurring revenue (ARR) for subscriptions and we want to have both the currently active and the expired subscriptions in there. So that we can see the ARR over a timeline.
The data we retrieve looks like this:
subscription_id
account_id
ARR
start_date
end_date
1
1
10
01-01-2022
31-03-2022
2
2
20
01-01-2022
31-12-2022
3
1
5
01-04-2022
31-11-2022
So in this case the same account (account_id 1) renewed a subscription at the 01-04-2022. In the report of 2022 we want to see the ARR for all months in 2022. I've looked into slowly changing dimensions, however something I can not really see in that concept is how to report both the currently active license and the history in a dashboard. If we for example want to visualize the ARR in all of 2022 per month in a dashboarding tool we want to see both subscriptions for account_id 1 over the course of the year, not just the currently active one. This seems to be very tricky to do in most dashboarding tools.
To overcome this I've done the following. I created a calendar table with an interval of 1 month and I cross join it with the table above to generate a fact table. The end result would look like:
timestamp
account_id
ARR
01-01-2022
1
10
01-01-2022
2
20
01-02-2022
1
10
...
...
...
01-11-2022
1
10
01-11-2022
2
20
01-11-2022
2
20
This makes it really easy for the user of the reporting tool to filter on a specific month and show the ARR between the dates and over multiple subscriptions. It does however generate a lot of extra data, but at the moment the storage space is not an issue. And it makes it more of a transactional style table, but the ARR is not really a transaction (i.e. it is not really a sold product on a specific date).
My question is: Are there better ways of generating a fact table where the source data contains a date range?

Microsoft Power BI, DAX - tricky ALLSELECTED with 2 values in slicer, but show only TOP 1 row in visual

This question is an extension of an already answered question, which I posted this week.
I have the below situation in Microsoft Power BI.
I have 2 simple tables:
1) CountryTable
2) YearTable
There is a 1-M relationship between YearTable and CountryTable.
The latter (YearTable) is used to feed values into a slicer.
(In my client database, Year has some alphabetical prefixes, such as Q1-2022, so I prefer to use YearOrder column to sort the Year column at the backend, while the slicer will display the Year column.)
The former (CountryTable) is the main table, with just a few sample rows.
These two tables are related via the Year column.
The Year slicer always has EXACTLY 2 values chosen in my Power BI report.
I need the Maximum of these two values of the year slicer as a measure, for each row of my visual.
At the same time, these two year values of the slicer must remove the unwanted rows in my report visual, based on the slicer selection of year values.
For example, when the slicer has 2019 and 2020 chosen, I need the value as in the DesiredOutput1 page.
Similarly, you can see DesiredOutput2 (Slicer values are 2020 and 2022); DesiredOutput3 (Slicer values are 2019 and 2022) pages.
I have indeed successfully obtained DesiredOutput1, DesiredOutput2, DesiredOutput3. Thanks to all the folks who helped me attain this.
Now, my main requirement in this posting, is this:
After obtaining the DesiredOutputs above, I need the following output:
Show only the TOP 1 row (ASC order of Year column, which is the minimum value of the slicer).
Essentially:
Year column of the visual: Minimum value of the slicer
MaxYear_Measure_SlicerSelection: Maximum value of the slicer (maximum of the two values chosen in the slicer)
You can see below:
Note: MaxYear_Measure_SlicerSelection measure can refer to any one of the two measures [MaxYear] or [MaxYearMeasure_Community] (see the .pbix file for the formulas of the measures).
Any idea ?
I prefer the Year column of the visual not to be converted to a new measure. Would RANKX help in this case ? Any thoughts?

How do I subtract two rows from a POWERBI custom table?

Project
Cost
January
323
Feb
323
I have a table as followed seen above which ROW is month (filtered by a certain project) and values are cost of the project. I want to calcuate the difference between two months, but I am having trouble.
How do I subtract two rows from each other.
In the code I wrote:
Variance = [Cost] - CALCULATE([Cost],PREVIOUSMONTH('Month'[Month))
I get the following error, A column specified in the call to function is not of type date.
Is there a way to manual subtract two months?
The best way to do it is to replace your month with an actual Date value. The first of the month for example. The you should be able to do something like this assuming your month dates are unique: If they are not unique you should create a Dates table (See Microsoft's Guidance on date table) and join.
variance = [Cost] - Calculate([COST],(PARALLELPERIOD(Month[Month],-1,Month)
You can use EARLIER function here but, only when the Months have an Id. Such as
1 Jan
2 Feb
3 March
...
Link for details
However, I would suggest creating a date table and then having a relationship from the date table to your table. By using date table you can easily achieve using in-buit date functions.

Month slicer and filter not working properly on rolling data Power BI

i All.
I have created below measure to reflects always 3 month figures when month slicer is used.
3R =
CALCULATE(COUNT('Order'[Order/ not ordered]),DATESINPERIOD('Date'[Date],LASTDATE('Date'[Date]),-3,MONTH)))
However, when I try to add salesman figures as filter with below formula, and click to month slicer it is directly show just choosen month figures not 3 months.
3R John =
CALCULATE(COUNT('Order'[Order/ notordered])
,DATESINPERIOD('Date'[Date],LASTDATE('Date'[Date]),-3,MONTH),FILTER('Order','Order'[Salesman]="John")))
on below link I have shared a sample for this. there are 2 different table and 1 matrix.
Matrix is named working and 1 of the table is named as "not working properly". not selecting any value on slicer. all data gives same data. however when clicked month slicer not named main table is changing and this is correct. also matrix is correct but table that I am trying to achive is not working.
What i am trying to achive is, 3 month roling data based on customer and salesman. when click for example 1 on month slicer table should give January 2020, December 2019 and November 2019 figures.
https://drive.google.com/file/d/1LoqSiKhHMFn_OioI2RnXOzjcIL9dPRjS/view?usp=sharing
below is the solution. worked for me.
3R John =CALCULATE(COUNT('Order'[Order/notordered]),DATESINPERIOD('Date'[Date],LASTDATE('Date'[Date]),-3,MONTH),'Order'[Salesman]="John"))
just remove filter('order', section and it is ok now.

DAX Year over Year Change in Cash for different Organizations

I am working in PowerBI and would like to accomplish this issue using DAX. I have a table structured as follows:
ID Year Cash
3001 1999 1,200
3001 2000 1,000
3001 2001 2,200
3001 2002 1,900
... ... ...
8500 2014 3,520
I am trying to identify the organizations (indicated by the 'ID' column) that experienced decreases in cash for two consecutive years. There are several thousand organizations, and I have data on every organization from year 1999-2014.
I would first calculate a column for the Prior year Cash values e.g. for [Cash Year-1] as:
=LOOKUPVALUE([Cash],[ID],[ID],[Year],[Year] - 1)
I would repeat for -2.
Then I would use an IF statement to calculate the final column e.g. [Cash Trend]:
=IF(AND([Cash]<[Cash Year-1],[Cash Year-1]<[Cash Year-2]),"Declining for 2 years","Other")

Resources