Filter across tables - powerbi

I want to add a calculated column where I have a filter on. This is in the sales targets table. How can I add a filter which filters on city with the ordval from the sales table?

You should be able to do something like this:
SumOrdval =
VAR RelatedCity = LOOKUPVALUE(Customer[Citycode], Customer[Cno], Sales[cno])
VAR RelatedCnos = CALCULATETABLE(VALUES(Customer[cno]), Customer[Citycode] = RelatedCity)
RETURN
CALCULATE(
SUM(Sales[ordval]),
ALL(Sales),
Sales[cno] IN RelatedCnos
)

Related

How can I concatinate multiple columns of a filtered calculated table for a mesure in DAX?

I am trying to filter a calculated table by a parameter value (created with new parameter) then concatinate several columns and display the result in a card.
Measure =
VAR FTab = FILTER('Toy','Toy'[ID] = 'ID Slider Parameter'[ID Slider Parameter Value])
RETURN
DISTINCT( CONCATENATE(FTab[gender] , FTab[region]))
The error that I am getting is Cannot "find table 'FTab'"
The 'Table' I am working with is a calculated table. I know how to do it in Power Query but not with DAX.
I want to filter the table with the slider and add the measure to a card. The output would be something like "fEast".
You can find my .pbix file here http://filedropper.com/Rz9fLpyz
Use CONCATENATEX() instead:
Measure =
VAR FTab =
FILTER(
'Toy',
'Toy'[ID] = [ID Slider Parameter Value]
)
RETURN
CONCATENATEX(
FTab,
[gender] & [region]
)

Why can't I filter a column based on a parameters table's value?

I wanna filter a Date using a table parameters.
First, I created a Parameters table like this :
Here is the table's values :
And then I created a Slicer with the table parameters values as a list :
So what I want is to filter my Date table based on which value is selected on the slicer.
The Date table looks like this :
v_DATE
Date
Date is a "date" type.
Here are some sample values :
Date
13.10.21
14.10.21
15.10.21
The Date table is used on a bar chart with values that came from an article table.
So what I want is when I click on the Last30Days it filters me all the values from the current date to 30 days ago.
I tried this :
On my Date column, I right clicked and clicked on "New measure".
Then I wrote a DAX code like this :
Measure = CALCULATE(IF(SELECTEDVALUE(Parametres[Selection_date]) = "Last30Days", filter(v_Date, v_Date[Date] = "15.10.21")))`
I used 15.10.21 as an example value to test my query. But It didn't work, the date isn't filtered.
So what I am doing wrong ? And how can I write my 3 parameters?
EDIT
How can I add the values that are not in the last 30 days ?
You haven't mentioned the measure/column name which you are applying the filter, so I'm assuming it as a Measure([Measure_Value]).
If your user is only selecting only single option at a time, you can use a switch case to filter the expression based on slicer value.
You can calculate the current date using TODAY() and on the basis of that you can calculate other time values like this:
var _month = MONTH(_today)
var _year = YEAR(_today)
var _last_30_days = _today - 30
On the basis of these values and switch case, you can create the measure:
Measure =
var _today = TODAY()
var _month = MONTH(_today)
var _year = YEAR(_today)
var _last_30_days = _today - 30
return SWITCH(SELECTEDVALUE(Parametres[Selection_date]),
"MonthToDate",CALCULATE([Measure_Value],FILTER(ALL(v_Date),MONTH('v_Date'[Date])=_month && YEAR('v_Date'[Date])=_year)),
"Last30Days",CALCULATE([Measure_Value],FILTER(ALL(v_Date),'v_Date'[Date]>_last_30_days)),
"YearToDate",CALCULATE([Measure_Value],FILTER(ALL(v_Date),YEAR('v_Date'[Date])=_year)),
BLANK())
In your measure formula you need to add the expression that you want to calculate, so if it's a sum or a count of records that's what you pick.
One way of doing it could be:
Measure = Count(Data[Records])
Measure with Param =
var selectedParam = SELECTEDVALUE(Parametres[Selection_date])
var dateFilter = FILTER(Dates,
(selectedParam = "Last30Days" && Dates[Date] = DATE(2021,01,19)) ||
(selectedParam = "MonthToDate" && Dates[Date] > DATE(2021,01,19)) ||
(selectedParam = "YearToDate" && Dates[Date] > DATE(2021,01,19)) ||
(ISBLANK(selectedParam)))
//note: the date filters are just an example, not how you would actually slice these.
return CALCULATE([Measure], dateFilter)
But you should look into Calculation Groups, as it is the nicest way to do what you're after. https://www.sqlbi.com/blog/marco/2020/07/15/creating-calculation-groups-in-power-bi-desktop/

Fetch specific record value from a table in PowerBI

I'm trying to dynamically calculate the % Change of the prices in a table, where each price will be compared with the first price (Price in the first record, ordered by date). There is a date filter, so the first record being displayed will change accordingly. Date column values are unique.
Assume the user applies a filter for date BETWEEN '15-APR-2021' AND '30-APR-2021'. Then the expected '% Change' column would look like:
For this example, I had to hardcode the starting price in the calculation:
% Change = (('table1'[price]/3218.95) -1)*100
I tried the below, but it doesn't return a static value of 3218.95. Instead, it re-calculates it at record level rather filtered table level as we see in the above screenshot :
first_date = (MIN(table1[Date]))
first_price = LOOKUPVALUE('table1'[price],table1[Date],'table1'[first_date])
% Change = (('table1'[price]/first_price) -1)*100
I'm new to PowerBI DAX. Logically the SQL would look like so:
SELECT
date,
price,
((
price /
( -- Gets the first price
SELECT price FROM table1
WHERE date IN (SELECT MIN(date) FROM table1 WHERE date BETWEEN '15-APR-2021' AND '30-APR-2021')
)
)-1) * 100 as '% change'
FROM table1
WHERE date BETWEEN '15-APR-2021' AND '30-APR-2021'
IF you want to get the first price you can use the following DAX:
first_price = CALCULATE(MIN('table1'[price]), FILTER( 'table1', MIN(table1[Date])))
As for the % Change:
% Change =
var curPrice = 'table1'[price]
var first_price = CALCULATE(MIN('table1'[price]), FILTER( 'table1', MIN(table1[Date])))
return ((curPrice/first_price) - 1) * 100

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

Power BI Sum cells grouped by same ID

How can I make a formula which sums all the cells which have the same ID?
Edit: Both of these columns are in the same table. I need a new column which calculates the sum of all the hours with the same ID.
In that case, I would create a new summery table:
SummeryTable =
SUMMARIZE(
'TableName';
'TableName'[ID];
"Time_summed"; SUM('TableName'[TimeColumn])
)
Then you will end up with a table with distinct [ID] on each row and the sum of all hours corresponding to this ID from the original data table. Which you can use to create a relationship with the original table.
EDIT_1:
Yes you can if you use a calculated column like this:
SumTime =
VAR thisID = [ID]
RETURN
CALCULATE(
SUM('TableName'[TimeColumn]);
ALL('Tablename');
'TableName'[ID] = thisID
)