How to fetch column from another Dimension table without relationship - powerbi

I have 2 dimension tables with no relation between them.
1 is Product Dimension table as
ProdCode | ValidStartDate | ValidEndDate
XX | 2012-01-01| 2016-12-31
XX | 2017-01-01| 2017-12-31
XX | 2018-01-01| 2020-12-31
2nd is Time table
Year | IsCurrent
2012 | 0
2013 | 0
2014 | 0
2015 | 0
2016 | 0
2017 | 0
2018 | 0
2019 | 0
2020 | 1
I need to create a calculated column in Product table to show IsCurrent column from Time Table wrt the year selected.
I tried with CALCULATE but it expects one of the aggregate functions which i can not use because i want to show value in current row context.
for example:
IsCurrent =
CALCULATE(
MAXA('Time'[IsCurrent]),
FILTER(
'Time',
'Time'[Year] >= YEAR(Product[ValidStartDate])
&& 'Time'[Year] <= YEAR(Product[ValidEndDate])
)
)
This always gives me Max value from the range satisfied for example in case of 1st record (2012- 2016) shows 2016 always but I want to show respective current row year from Time table.
Please suggest.
Thank you.

Try this below Measure script-
Measure
IsCurrent =
CALCULATE(
MAXA('Time'[IsCurrent]),
FILTER(
'Time',
'Time'[Year] >= YEAR(MIN(Product[ValidStartDate]))
&& 'Time'[Year] <= YEAR(MIN(Product[ValidEndDate]))
)
)
Custom Column
IsCurrent_column =
var current_start_year = YEAR(Product[ValidStartDate])
var current_end_year = YEAR(Product[ValidEndDate])
RETURN
CALCULATE(
MAXA('time'[IsCurrent]),
FILTER(
'time',
'time'[Year] >= current_start_year
&& 'time'[Year] <= current_end_year
)
)
Here is the output-

Create this below measure-
in_range =
VAR selected_year = SELECTEDVALUE('time'[Year])
RETURN IF(
YEAR(MIN('product'[ValidStartDate])) <= selected_year
&& YEAR(MIN('product'[ValidEndDate])) >= selected_year
,
1,
0
)
This will give you a output as below-
Now you can add a Visual Level filter using this above measure so that the row wonly show when in_range = 1. This filter will only keep your expected rows in the table.

Related

DAX Formula to filter the last 28 days in the table data

I have a measure that totals the values for each date in the table. I want to filter this measure so that I can display only the last 28 days present in the table instead of displaying values for all the dates. Following is the code that works for getting totals for full table:
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) ) )
The 'relative date' filter in the Filters pane does not work because it only accepts the last 28 days based on today's date and not the dates in the table. Please suggest a DAX formula that can filter for the last 28 days present in the table.
Try this code
VAR endDay = LastDate(Daily_Reports[Case_Date])
VAR startDay= DATEADD(endDay,-28,DAY)
VAR setOfDates = DATESBETWEEN(Daily_Reports[Case_Date], StartDate, EndDate )
RETURN
CALCULATE(
SUM(Daily_Reports[Confirmed])
,setOfDates
)
You can try this one:
MS =
CALCULATE (
SUM ( Daily_Reports[Confirmed] ),
FILTER (
ALL ( Daily_Reports[Case_Date] ),
Daily_Reports[Case_Date]
>= SELECTEDVALUE ( Daily_Reports[Case_Date] ) - 28
)
)
This is what finally worked for me. I created a measure (not a column), that returns 1 for the last 28 days with an IF clause, leaving it blank if the date is not in the last 28 days as follows:
Last28 =
VAR MaxDate = LASTDATE( ALL(Daily_Reports[Case_Date]) )
VAR MinDate = DATEADD( MaxDate, -28, DAY )
RETURN
IF( SELECTEDVALUE(Daily_Reports[Case_Date]) >= MinDate && SELECTEDVALUE(Daily_Reports[Case_Date]) <= MaxDate, 1 )
Then I incorporated this measure into the Calculate function as follows:
Daily Cases =
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) && NOT(ISBLANK(Daily_Reports[Last28]))
)
)

Power BI - Get date when sum is 0

I have two tables - Customer and Transaction. The transaction holds invoices and payments. I need to get the date when the transaction sum is 0 or grater.
So the expected result should be John 02-20-2021. How can I achieve this?
customerID
Name
1
John
2
Ben
customerID
value
date
1
-150
02-13-2021
1
100
02-14-2021
1
200
02-20-2021
1
10
02-23-2021
You can try this out, by adding some new measures.
First measure to calculate the account balance over dates, returning a value only for positive balance:
Account Balance :=
VAR _date = MAX ( Transaction[date] )
VAR _balance =
CALCULATE (
SUM ( Transaction[value] ) ,
Transaction[date] <= _date
)
RETURN
IF ( _balance >= 0 , _balance )
The second filters the original table based on this measure and returns the earliest date:
Break Even Date :=
MINX (
FILTER (
VALUES ( Transaction[date] ),
[Account Balance]
),
[date]
)
Result:
Do note that I have not tested this beyond your (simple) example.

PowerBI join table and calculation

I have two tables:
Table 1
Year Name Score
2010 A 1000
2011 A 1200
2012 A 1200
2013 A 1300
2010 B 1100
2011 B 1500
2012 B 1300
2013 B 1600
Table 2
Year Factor
2010 3.57
2011 4.21
2012 6.11
2013 2.15
I would like to do this: match two tables, as long as Year in Table 1 match Year in Table 2, divide the score by the factor, e.g. for A in 2010, 1000/3.57; for B in 2010,1100/3.57
How to revise the following code?
adjusted score =
VAR
filter (all('Table 1'[Year]), 'Table 1'[Year]='Table 2'[Year])
RETURN
DIVIDE ('Table 1' [Score], 'Table 2'[Factor')
Thank you very much!
If you are looking for a measure, you can try this below code-
adjusted score =
VAR current_row_year = MIN('Table 1'[Year])
VAR current_row_Score = MIN('Table 1'[Score])
VAR factor =
CALCULATE(
MAX('Table 2'[Factor'),
filter (
all('Table 2'),
'Table 2'[Year]= current_row_year
)
)
RETURN DIVIDE (current_row_Score, factor)
Code for a Custom Column is as below-
adjusted score =
VAR current_row_year = 'Table 1'[Year]
VAR current_row_Score = 'Table 1'[Score]
VAR factor =
CALCULATE(
MAX('Table 2'[Factor'),
filter (
all('Table 2'),
'Table 2'[Year]= current_row_year
)
)
RETURN DIVIDE (current_row_Score, factor)
You can also do the same using Transformation using Power query. In that case you have to just merge your table 1 and 2 using Year and then after Expanding the table, you will have Factor in each row for the Year. You can then just add a new custom column with - Score/Factor

Power BI DAX summarize and filter by date

I have a Power BI report and I want to find the last date some action took place that had a non-0 value. In the following example:
|-------------|------------|
| ActionDate | ActionAmt|
|-------------|------------|
| 1-Feb | -100 |
|-------------|------------|
| 1-Feb | 100 |
|-------------|------------|
| 10-Jan | 150 |
|-------------|------------|
I want to return the 10-Jan date, not 1-Feb, since 10-Jan is the first non-0 value summed by ActionDay. My code returns 1-Feb:
Last Action Date =
VAR maxdatekey =
CALCULATE ( MAX ( 'Action'[ActionDateKey] ), 'Action'[ActionAmount] > 0 )
RETURN
IF (
maxdatekey,
FORMAT (
LOOKUPVALUE ( 'Date'[DateValue], 'Date'[DateKey], maxdatekey ),
"dd-MMM-yyyy"
)
)
How do I further group this to exclude the summarized 0 days?
I think you're looking for something like this where you summarize by date when calculating the amount:
LastActionDate =
VAR Summary =
SUMMARIZE (
'Date',
'Date'[DateValue],
"Amt", CALCULATE ( SUM ( 'Action'[ActionAmount] ) )
)
RETURN
FORMAT (
MAXX ( FILTER ( Summary, [Amt] > 0 ), 'Date'[DateValue] ),
"dd-MM-yyyy"
)

Generate date series using DAX

I have a closing date and delivery date column, if the date difference greater than 1 then, I need to generate date series with a closing date column. I have attached the example closing date and delivery date details below.
I want the output to be like the below one
Please help me in solving this issue.
Using this extended table and the following code I believe you would get the desired results:
Closing | Delivery | Difference | Order
---------------------------------------
190228 | 190321 | 21 | A
190228 | 190301 | 1 | B
190310 | 190317 | 7 | C
ExpandedDeliveryTable =
SELECTCOLUMNS(
FILTER(
ADDCOLUMNS(
CROSSJOIN(
CALCULATETABLE(
'Deliveries';
'Deliveries'[Difference] > 1
);
GENERATESERIES(
0;
MAX('Deliveries'[Difference]);
1
)
);
"Filter";
IF( [Delivery]-[Closing] < [Value]; 1; BLANK() )
);
[Filter] <> 1
);
"Closing_Date"; [Closing]+[Value];
"DeliveryDate"; [Delivery];
"Date Diff"; [Value];
"Order"; [Order]
)
This generates a new table looking like this:
]