Add Column Power BI from two column of different tables - powerbi

I wanna add a column calculated from two column from different tables :
table 1 :
Date ; target;
19/10/2018; 52
table 2 :
Product; Duration;
P1; 1;
P2; 3;
P3; 4;
And i wanna have something like that
Product; Duration; New Column
P1; 1; (52/(1+3+4)*1)
P2; 3; (52/(1+3+4)*3)
P3; 4; (52/(1+3+4)*4)

With DAX try this as a new column for table2:
New Column = VALUES('table1'[target])/SUM(table2[ Duration])*'table2'[ Duration]
The VALUES function will work here because there is only one value in 'table1'[target]
When you expand table1 with more dates and targets like this:
You can use the LOOKUPVALUE function to retrieve the target for a specific date:
New Column =
LOOKUPVALUE ( Table1[target], Table1[Date], DATE ( 2018, 10, 19 ) )
/ SUM ( table2[ Duration] )
* 'table2'[ Duration]
Or the target form the latest date:
New Column =
LOOKUPVALUE ( Table1[target], Table1[Date], MAX ( 'Table1'[Date] ) )
/ SUM ( table2[ Duration] )
* 'table2'[ Duration]

Related

Count of active employees between two date columns, by department

I'm trying to imitate this report (page 3) where it slices active headcount and all the other metrics (1) by date and (2) by department.
My data looks like this (with relationships, of course):
ID
Name
DEPID
Hired Date
Terminated Date
Terminated (Y/N)
1
John
2
1/1/2019
2020/12/31
Y
2
Jane
2
1/3/2018
2019/07/26
Y
3
Jack
1
1/5/2022
null
N
Using the following measure, I was able to extract total number of employees by date, but I wasn't able to filter by department:
CountOfActive =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('Table'); filter(ALL('Table'); Table[HIREDDATE] <= VALUE(_selectedDate) && (Table[TERMINATEDDATE] >= VALUE(_selectedDate) || ISBLANK(Table[TERMINATEDDATE]))))
My ideal output is something like the following (where I'll create a table for each department and list the number of active employees, then join them to my department key table afterwards so I can slice them):
Date
Count of Active Employees
Department
2019/1/1
3
Retail
2019/1/2
3
Retail
2019/1/3
4
Retail
...
...
...
The "Date" column would be a calendar table built with CALENDAR().
What should I do to achieve the last table based on the data I have?
My relationship schema looks like this.
try this : 'Table 2' is your Calendar Table which is also a slicer on the visual.
Make sure that your Calendar Table's Date has a relation with the Hired Date and also the relation between the Department Table
Count of Emp =
VAR _latest =
MAX ( 'Table 2'[Date] )
VAR _from =
MIN ( 'Table 2'[Date] )
VAR _dept =
SELECTEDVALUE ( Department[Department] )
RETURN
CALCULATE (
COUNTX ( 'Table', 'Table'[ID ] ),
FILTER (
ALL ( 'Table' ),
'Table'[Terminated Date ] >= _from
&& 'Table'[Hired Date ] <= _from
&& 'Table'[Terminated (Y/N)] = "Y"
&& RELATED ( Department[Department] ) = _dept
)
)
+ CALCULATE (
COUNTX ( 'Table', 'Table'[ID ] ),
FILTER (
ALL ( 'Table' ),
'Table'[Terminated (Y/N)] = "N"
&& 'Table'[Hired Date ] <= _from
&& RELATED ( Department[Department] ) = _dept
)
)

Filter on LOOKUPVALUE (Power BI)

I would like to create a new column in TABLE B to sum up the total weight from Table A. But I don't want the sum value are copying to every single row in TABLE B (Based on ID). I just want to VLOOKUP the value to every first of the ID (Num = 1)
Expectation
Thank for your attention. Any help will greatly appreciated.
Column =
VAR _1 =
CALCULATE (
SUMX (
FILTER ( t1, t1[id] = MAXX ( FILTER ( t2, t2[num] = 1 ), t2[id] ) ),
t1[val]
)
)
RETURN
_1

Power BI - How to create new column that summarize data by week and by Online or Store Channel?

I have WTDdata table that contains ThisYearRevenue and LastYearRevenue summarized by week:
I need to create 4 more columns LastYearOnlineRevenue, LastYearStoreRevenue, ThisYearOnlineRevenue, and ThisYearStoreRevenue from another table (RevenueByDate) that looks like this:
W column in this table means Fiscal Week.
I tried using this aproach:
LastYearOnlineRevenue =
SUMMARIZE(FILTER(ALL(FiscalCalendar),FiscalCalendar[FiscalWeek]),FiscalCalendar[FiscalWeek]),
"LastYearOnlineRevenue",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] =
YEAR(TODAY())-1 && RevenueByDate[Channel] = "Online")
If you can help me with at least one column, I am assuming the logic will be the same for the other 3.
Thank you in advance.
Here is the location for sample data and .pbix file:
https://1drv.ms/u/s!AhhZq1add5YwjYIvuASi76lCL3R1eA?e=C7ObDZ
Try this:
Since there is no way of telling the current year from the WTDdata table, I have assumed that the current year is always 2021, you can also replace that with:
VALUE ( MAX ( RevenueByDate[FiscalYear] ) )
LastYearOnlineRevenue =
VAR CurrentFiscalWeek = WTDdata[FiscalWeek]
VAR CurrentYear = 2021
VAR ImmediatePreviousYear =
CALCULATE (
MAX ( FiscalCalendar[FiscalYear] ),
FiscalCalendar[FiscalYear] < CurrentYear,
REMOVEFILTERS ( WTDdata )
)
VAR Result =
CALCULATE (
SUM ( RevenueByDate[Revenue] ),
FiscalCalendar[FiscalWeek] = CurrentFiscalWeek,
FiscalCalendar[FiscalYear] = ImmediatePreviousYear,
RevenueByDate[Channel] = "Online",
REMOVEFILTERS ( WTDdata )
)
RETURN
Result

DAX issue counting records with Start- and EndDates for a given time period and filter

I have the following table ("Services") in PBI:
The table holds all services offered to a customer. A service has a Start- and EndDate.
What I'm trying to do is to create a measure that, for a given date or date range, returns the number of distinct CustomerID:s receiving a service during this period.
Some examples:
Given the table above and a date range between 2019-01-01 and 2019-04-01 the measure would return the distinct value 3 (a match for rows #2, #4 and #5).
Giving a single date of 2019-07-01 the measure would return a distinct value of 3 (because rows #1, #2, #3 and #4 has a period given by Start- and EndDate matching this date).
In my report I also need to be able to filter by ServiceTypeID.
The table is defined like this:
Services =
DATATABLE (
"CustomerID"; INTEGER;
"ServiceTypeID"; INTEGER;
"ServiceStartDate"; DATETIME;
"ServiceEndDate"; DATETIME;
{
{ 1; 10; "2019-06-03"; "2019-09-01" };
{ 2; 12; "2019-01-01"; "2019-12-31" };
{ 2; 10; "2019-05-01"; "2019-09-01" };
{ 3; 8; "2019-02-01"; "2019-08-01" };
{ 4; 10; "2019-03-30"; "2019-06-01" }
}
)
I have tried defining the measure like the code below, but I have difficulties filtering by ServiceTypeID (the measure just shows a value as if I didn't apply the filter for ServiceTypeID).
Number of active services =
CALCULATE (
DISTINCTCOUNT ( 'Services'[CustomerID] );
FILTER (
ALLSELECTED ( 'Services' );
(
MIN ( 'DateTable'[Date] ) >= 'Services'[ServiceStartDate]
&& MIN ( DateTable[Date] ) <= 'Services'[ServiceEndDate]
)
|| (
MAX ( DateTable[Date] ) >= 'Services'[ServiceStartDate]
&& MAX ( DateTable[Date] ) <= 'Services'[ServiceEndDate]
)
)
)
Does anybody know what I'm doing wrong here? Any help is much appreciated.
Kind regards,
Peter
I propose this solution :
First here is the model :
Then you can write this new measure :
NumActservices =
// Retrieve the current date
Var VrCurrentDate = MAX(DateTable[Date])
// Retrieve active Acts filter per ServiceID Within the current date
Var VrServicesInPeriode =
CALCULATETABLE(
Services;
FILTER(
ALLEXCEPT(Services;Service[ServiceTypeID]);
Services[ServiceStartDate]<VrCurrentDate
&&
Services[ServiceEndDate]>=VrCurrentDate
)
)
// Count Disctinct customer belonging to the previous table
Var VrResult = CALCULATE(
DISTINCTCOUNT(Services[CustomerID]);
VrServicesInPeriode
)
RETURN
VrResult
The result is visible here :

Filter a column dynamically using a measure in Power BI

There is a column with FY i.e. "FY19","FY18","FY17" etc.
I Created a measure which calculates the current FY:
CurrFY =
CONCATENATE (
"FY",
IF (
MONTH ( TODAY () ) <= 3,
VALUE ( FORMAT ( TODAY (), "YY" ) ),
VALUE ( FORMAT ( TODAY (), "YY" ) ) + 1
)
)
e.g. output: "FY19"
Now i need to filter the report based on the FY column using the current FY i get from the CurrFY measure.
How do i do it?
Create a calculated column on your table using that measure
FYFilter = IF(Table1[FY] = [CurrFY], 1, 0)
Then add that column as a report level filter where FYFilter is 1.