Many to Many Relationship BY DATE in PowerBI - powerbi

I need to be able to calculate a measure in a table of many to many relationship. Here are my two tables:
Contracts Table
Serial# ContractTyp StartDate EndDate
A MP 1/1/2017 1/6/2018
B ML 10/24/2017 6/30/2020
A ML 1/6/2018 12/30/2019
C MU 5/15/2018 1/1/2021
Performance Table
Serial# Diff Good Bad Date
A 15 1 0 1/30/2017
B -24 1 0 12/17/2017
A 57 0 1 4/22/2017
A 18 1 0 2/1/2018
C 123 0 1 9/12/2018
So, my measure is simple. It just calculates the percentage of Good by Serial#.
NUM_GOOD = CALCULATE(COUNTA('Performance'[Good]),'Performance[Good] IN {"1"})
NUM_BAD = CALCULATE(COUNTA('Performance'[Bad]),'Performance[Bad] IN {"1"})
PERFORMANCE_METRIC = NUM_GOOD/(NUM_BAD + NUM_GOOD)
I need to be able to run that performance metric for every Serial# but only for when the machines were under a MP or MU ContractTyp. So I need it to be able to look at the Date in the Performance Table and make sure that date falls in the range between StartDate and EndDate on the Contracts Table where Serial# is MP or MU.
So I for example if I want to look at the Performance of all Serial#'s with a ContractTyp of MP or MU in the last 2 years I would want the result to look as follows:
Serial# PERFORMANCE_METRIC
A 50%
B 100%
C 0%
Thanks in advance!

Below is a formula for a Calculated Column that you could add to the Performance table to return the True / False values (ref your comments).
To explain the logic:
Store the Serial and Date values (from the current record) in variables.
If we find at least 1 matching Contract with the required ContractTyp and Date range, return True, otherwise False.
Has Contract =
VAR vSerial = [Serial#]
VAR vDate = [Date]
RETURN
IF (
CALCULATE (
COUNTROWS ( Contracts ),
Contracts[Serial#] = vSerial,
Contracts[ContractTyp] IN { "MP", "MU" },
Contracts[StartDate] <= vDate,
Contracts[EndDate] >= vDate
) > 0,
TRUE (),
FALSE ()
)

One way to do it would be to create a new table in Power BI by converting the Contracts table which is at a Serial #, Start date and End date level to a Serial # and Date level. For example if the data has the following records:
ID Type Start End
1 MP 1/1/2019 1/2/2019
You should convert it into something like this:
ID Type Date
1 MP 1/1/2019
1 MP 1/2/2019
The following link should help you out regarding this:
https://natechamberlain.com/2018/08/08/how-to-add-rows-for-dates-between-start-and-end-dates-in-power-bi-date-range-data/
Once the table is converted, you should be able to do a simple join based on Serial# and date and it will be a one to one relationship. Then you can apply whatever filter you are looking for.
One caveat here is that, the size of the data would matter a lot. If the Contracts table has a reasonable number of records, this would work, otherwise the data might blow up.

Adding another possible answer using merge queries. Instead of creating a relationship you can directly merge the two tables. If you have not done this before, you can access this is in the edit query window. Once you merge both the tables using serial ID as the key, you can use a formula like below to use as a filter:
Filter Field = IF(Date2[Date]>=Date2[Date1.Start]&&Date2[Date]<=Date2[Date1.End],1,0)
Then you can simply set Filter Field =1 in the filter pane and you should be good to go. Hope this helps.
Here is a tutorial on merge queries. The link is specifically for excel, but it should work in power bi as well:
https://www.powerquery.training/merge-tables/

Related

IF 'AND-OR' ISFILTERED combination in DAX giving problems

Below is the sample dataset
The data has two slicers ( date and category ) shown below
I am writing a DAX Statement to multiply the sum(values) * 10 only if the date range is in the current year 2023.
The StartYear gives the start of the current year, firstD gives the lowest date from the date slicer.
Formula =
var new = sum(Test[Value]) * 10
var startyear = DATE(YEAR(TODAY()),1,1)
var firstD = CALCULATE( MIN( Test[Date]), ALLSELECTED(Test[Date]) )
return if( ISFILTERED(Test[Categories]) && firstD >= startyear, new, 0 )
Now when I filter dates to 2023, the total value should be 2300 but it shows as 0
However the DAX works when I select A or B
If we remove the ISFILTERED function then, it gives wrong value, the expected value is 0 because the start date is in 2022, but it shows 650
let me know if that is the right syntax
It looks like you are not using a separate calendar table to handle this, which you need!
In your very last example you have set your slicer to some time late 2022, but the minimum value of 'Test'[Date] for your selected category is in year 2023. Hint: set the slicer to e.g. 2022-12-14, this will include a 2022-date for Category A in your data.
Your measure behaves exactly how it is supposed to, in other words!
To fix this, you need to do the following:
Create a calendar table in your model, this should contain contiguous dates, which is necessary for the filtering method you want
Establish a relationship between the calendar table and existing Test table.
Use the date column from your new calendar table in your slicer and as date reference in your measure
Exactly how to create a calendar table is thoroughly documented on Google, I recommend you search and find an article or video you understand for implementing this.
Lastly: Your use of ISFILTERED in this measure seems strange, since you mention nowhere the requirement of only showing a number if the column you are testing filtering on is filtered, if that makes sense.. :-) The way you describe your calculation, you only need to check whether the selected date range starts in current year.

Power BI - Filtering model on latest version of all attributes of all dimensions through DAX

I have a model that's comprised of multiple tables containing, for every ID, multiple rows with a valid_from and valid_to dates.
This model has one table in that is linked to every other table (a table working as both a fact and a dimension).
This fact has bi-directional cross filtering with the other tables.
I also have a date dimension that is not linked to any other table.
I want to be able to calculate the sum of a column in this table in the following way:
If a date range is selected, I want to get the sum of the latest value per ID from the fact able that is before the max selected date from the date dimension.
If no date is selected, I want to get the sum of the current version of the value per ID.
This comes down to selecting the latest value per ID filtered on the dates.
Because of the nature of the model (bi-directional with the fact/dimension table), I want to have the latest version of any attribute from any dimension selected in the visual.
Here's an data example and the desired outcome:
fact/dimension table:
ID
Valid_from
Valid_to
Amount
SK_DIM1
SK_DIM2
1
01-01-2020
05-12-2021
50
1234
6787
1
05-13-2021
07-31-2021
100
1235
6787
1
08-01-2021
12-25-2021
100
1236
6787
1
12-26-2021
12-31-2021
200
1236
6787
1
01-01-2022
12-31-9999
200
1236
6788
Dimension 1:
ID
SK
Valid_from
Valid_to
Name
1
1234
10-20-2019
06-01-2021
Name 1
1
1235
06-02-2021
07-31-2021
Name 2
1
1236
08-01-2021
12-31-9999
Name 3
Dimension 2:
ID
SK
Valid_from
Valid_to
Name
1
6787
10-20-2019
12-31-2021
Name 1
1
6788
01-01-2022
12-31-9999
Name 2
My measure is supposed to do the following:
If no date is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 3
Name 2
200
If July 2021 is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 2
Name 1
100
So the idea here is that the measure would filter the fact table on the latest valid value in the selected date range, and then the bi-directional relationships will filter the dimensions to get the corresponding version to that row with the max validity (last valid row) in the selected range date.
I have tried to do the following two DAX codes but it's not working:
Solution 1: With this solution, filtering on other dimensions work and I get the last version in the selected date range for all attributes of all used dimensions. But the problem here is that the max valid from is not calculated per ID, so I only get the max valid from overall.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFrom =
CALCULATE(
MAX(fact[valid_from]),
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
)
RETURN
CALCULATE(
SUM(fact[Amount]),
fact[valid_from] = _maxValidFrom
)
Solution 2: With this solution, I do get the right max valid from per ID and the resulting number is correct, but for some reason, when I use other attributes from the dimensions, it duplicates the amount for every version of that attribute. The bi-directional filtering does not work anymore with Solution 2.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFromPerID =
SUMMARIZE(
FILTER(
fact,
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
),
fact[ID],
"maxValidFrom",
MAX(fact[valid_from])
)
RETURN
CALCULATE(
SUM(fact[Amount]),
TREATAS(
_maxValidFromPerID,
fact[ID],
fact[valid_from]
)
)
So if somebody can explain why the bi-directional filtering doesn't work anymore that will be great, and also, more importantly, if you have any solution to have both the latest value per ID and still keep filtering on other attributes, that would be great!
Sorry for the long post, but I thought it's best to give all the details for a complete understanding of my issue, this has been picking my brain since few days now and I'm sure I'm missing something stupid but I turned to this community for help because I cannot seem to be able to find a solution!
Thank you very much in advance for any help!
Seems to be workable with a dummy model. I didn't got the point how filter ID, so if it creates a problem let me know how you handle ID. Then I changed fact to facts as fact is a function. Also, I'm not sure about the workability of the measure at your real model. Hope you will give some feedback.
Amount Measure =
VAR ValidDate=
calculate(
max(facts[Valid_to])
,ALLEXCEPT(facts,facts[ID])
,facts[Valid_to]<=MAX(Dates[Date])
)
Return
CALCULATE(
SUM(facts[Amount])
,TREATAS({ValidDate},facts[Valid_to])
)

Power BI - Show zero/null value on line chart with dates

I'm using a simple table like this to make a report in Power BI:
Order number
Order date
Turnover
001
30/1/2022
10 €
002
30/1/2022
20 €
003
2/2/2022
15 €
I need to create a line chart showing all the dates, even where I have no data (no orders for that day). This is currently how is shown:
You can notice that the 1/2/22 and 3/2/22 are missing due to no order, but I want them to be visible and the value should be 0. This is also affecting the average line because it's calculated based on the days with data, but I need to put into account also the 0-turnover days.
I tried to use the "Show items with no data" on the date dimension and switch the X axis from Continuous to Catergorical and the other way around. I also tried to create a new metric like this:
Total Turnover = IF(ISBLANK(SUM(Orders[Turnover (EUR)])), 1, SUM(Orders[Turnover (EUR)]))
but it's not working.
If I understand your business requirement correctly, you are going to need to do three things:
Make sure you have a date-dimension table in your model. Build the relationship based on your [Order date] column.
Refactor your [Total Turnover] measure as such:
Total Turnover =
VAR TotalTurnover = SUM( Orders[Turnover] )
RETURN
IF(
ISBLANK( TotalTurnover ),
0,
TotalTurnover
)
Build your line chart using the [Date] column from your date table.

Power BI - Select Slicer Date Between 2 Columns

Hopefully a quick explanation of what I am hoping to accomplish followed by the approach we've been working on for over a year.
Desired Result
I have a table of SCD values with two columns, SCD_Valid_From and SCD_Valid_To. Is there a way to join a date table in my model (or simply use a slicer without a join) in order to be able to choose a specific date that is in between the two SCD columns and have that row of data returned?
Original Table
ID | SCD_Valid_From | SCR_Valid_To | Cost
1 2020-08-01 2020-08-03 5.00
Slicer date chosen is 2020-08-02. I would like this ID=1 record to be returned.
What We've Attempted So Far
We had a consultant come in and help us get Power BI launched last year. His solution was to create an expansion table that would contain a row for every ID/Date combination.
Expanded Original Table
ID | SCD_Valid_Date | Cost
1 2020-08-01 5.00
1 2020-08-02 5.00
1 2020-08-03 5.00
This was happening originally on the Power BI side, and we would use incremental refresh to control how much of this table was getting pushed each day. Long story short, this was extremely inefficient and made the refresh too slow to be effective - for 5 years' worth of data, we would need over 2000 rows per ID just to be able to select a dimensional record.
Is there a way to use a slicer where Power BI can select the records where that selected date falls between dates in two columns of a table?
Let me explain a workaround and I hope this will help you to solve your issue. Let me guess you have below 2 tables-
"Dates" table with column "Date" from where you are generating the date slicer.
"your_main_table" with with column "scd_valid_from" and "scd_valid_to".
Step-1: If you do not have relation between table "Dates" and "your_main_table", this is fine as other wise you have to create a new table like "Dates2". For this work around, you can not have relation between those tables.
In case you have already relation established between those tables, create a new custom table with this below code-
Dates2 =
SELECTCOLUMNS(
Dates,
"Date", Dates[Date]
)
From here, I will consider "Dates2" as source of your Date slicer. But if you have "Date" table with no relation with table "your_main_table", just consider "Dates" in place of "Dates2" in below measures creation. Now, Create these following 4 measures in your table "your_main_table"
1.
date_from_current_row = max(join_using_date_range[SCD_Valid_From])
2.
date_to_current_row = max(join_using_date_range[SCD_Valid_to])
3.
date_selected_in_slicer = SELECTEDVALUE(Dates2[Date])
4.
show_hide_row =
if(
[date_selected_in_slicer] >= [date_from_current_row]
&& [date_selected_in_slicer] <= [date_to_current_row]
,
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "your_main_table"
Final Step: Now just add a visual level filter with the measure "show_hide_row" and set value will show only when "show_hide_row = 1".
The final output will be something like below image-

Create a Measure in Power Pivot / DAX that calculates available equipment based on date ranges

I am looking for unit counts when the equipment is not in the date range (start and end dates).
How do I make a measure "AvailUnitCount" to give me unit counts by category in the timeline dimension (say November 11, 2018)?
I think it can be achieved via a measure in Power Pivot and date table, but I am just quite new to DAX and time dimension concept overall.
My measure reads:
AvailUnitCount := CALCULATE( DISTINCTCOUNT( EquipUsage[EquipmentNo] ) )
How do I incorporate time dimension into the measure above, so I can report on available equipment for a specific date by moving a timeline in Excel?
Please see the data set and the desired outcome below. I immensely appreciate your advice on this.
Table 1: EquipUsage
EquipNo CategoryNo UsageStartDate UsageEndDate
----------------------------------------------------
10005164 A020004004 5-Nov-18 5-Dec-18
10005167 A020004004 24-Oct-18 10-Nov-18
10005176 A020004005 9-Oct-18 5-Dec-18
10015982 A020004006 18-Feb-18 5-Sep-18
10019170 A020004006 16-Aug-18 30-Mar-19
10019551 A020004006 2-May-17 10-Nov-18
10005178 A020004007 20-Sep-18 15-Jan-19
Table 2: EquipCategories (Example of Desired Outcome for November 11, 2018)
CategoryNo AllUnits AvailableUnits
--------------------------------------
A020004004 2 1
A020004005 1 0
A020004006 3 2
A020004007 1 0
The AllUnits measure is simple:
AllUnits = COUNTROWS(EquipUsage)
For availability, read in your desired date and sum up the rows where that date does not fall in the given date range:
AvailableUnits =
VAR CheckDate = SELECTEDVALUE(DateTable[Date])
RETURN SUMX(EquipUsage,
IF(
EquipUsage[UsageStartDate] <= CheckDate &&
EquipUsage[UsageEndDate] >= CheckDate,
0,
1
)
)