Count managers without sales - powerbi

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

Related

DAX PowerBI SUM from the beginning up to current date (input by user)

I have 4 tables Calendar, Products, Region, and Sales. Then I create the relationship between 4 tables through
Region.Region_code -> Sales.Region_Code
Product.Product_wid -> Sales.Product_wid
Calendar.Cal_Row_wid -> Sales.Date_wid
Then I create 2 slicers Date and Month:
I would like to write a measure to calculate the Total quantity_rcs (which belongs to table Sales) for all order from the beginning up to current date (which characterized by Date and Month; both of them belong to Calendars).
How this should work if you have multivalue slicer?
belove example for onevalue slicer where we are pointing to specific date (consider that you have a unique date column In calendar maybe "issue_date"):
Measure =
var __date = calculate(max(Calendar[issue_date]), FILTER(ALL(Calendar[Date]
,Calendar[Month],Calendar[Year]),
Calendar[Date] = SELECTEDVALUE(Calendar[Date]) &&
Calendar[Month] = SELECTEDVALUE(Calendar[Month]) &&
Calendar[Year] = YEAR(TODAY())
)
)
return
calculate( sum(Sales[quantity_rcs]),
FILTER(ALL(Calendar[issue_date]),
Calendar[issue_date] <= __date )
)

DAX Grouping Evaluation

In this measure - I'm creating a temp table to group by customer ID and return the year of the min order_date for each customer. I then want to count the # of customers that show up for a given year (basically just a row count).
What I'm struggling to understand is - this formula doesn't seem to look at the SUMMARIZE table within it. If I put year and this measure in a matrix, it counts based on the raw table, not the grouped version built by SUMMARIZE in the formula. Any ideas why it's evaluating this way?
COUNTROWS(
SUMMARIZE(
Orders,
Orders[Customer ID],
"min_order_date_year",
MIN(Orders[Order Date].[Year])))
The formula is OK as per logic you provided to it. You have to clear about both your requirement and what your are writing in your code. Your formula is returning exactly what you are instructing it.
If I understand correct, you simply need the Customer count for the Minimum Year. For say if you have 6 unique customer for Year 2019 and 11 unique customer for Year 2020, you are looking here for value 6 to return by your measure.
Now, what your summarize table actually returning? if you create a separate custom table for the Summarize code only as below, you can see the table will actually hold all your customer name/id in first column and the second column will hole the MIN year available for that customer.
orders_summarize =
SUMMARIZE(
Orders,
Orders[customer id],
"min_order_date_year",MIN(Orders[Order Date].[Year])
)
So basically you have list of all customer in your summarize table. And now your are counting rows of your summarize table which is actually returning the total number of unique customers.
Finally, if you wants customer count for a specific Year (like MIN year), follow these below steps-
Step-1: Create a custom summarized table as below-
store_summarized_table =
SUMMARIZE(
store,
store[Customer ID],
"mindate",MIN(store[Order Date])
)
Step-2: create a measure as-
count_cust_id = COUNT('store_summarized_table'[Customer ID])
Step-3: Now configure your Matrix visuals as shown in the below image. You can also get the output in the image-
To avoid the Physical Table, you can do this below-
Step-1: Create a Custom Column as below-
is_min_year =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_year = store[Order Date].[Year]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_year_current_custommer_id =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_year = min_year_current_custommer_id, 1,0)
OR create a measure as-
is_min_year_measure =
VAR min_order_year_for_current_customer =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date].[Year]) = min_order_year_for_current_customer, 1,0)
Step-2: Create a Measure as below-
For created Custom Column
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
store[is_min_year] = 1
)
)
For Created Measure
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
[is_min_year_measure] = 1
)
)
Now add "Order Date" and measure "count_cust_for_min_year" to your Matrix. The out put will same as below-

Establish a Habit Score based on number of months with an action per unique user

I'm trying to establish a habit score which indicates +1 for every calendar month with an action and -1 for every calendar month with no action by user. The tricky part to this is I would like to do this per user starting after an indicated date (different for every user). If an action is done twice in the same month, I only want it counted as +1. The below image will help illustrate the logic and desired output.
I need like the result to appear as a calculated column in a separate table with distinct users (this table is already created, I just need the habit score added).
This is probably not the smoothest solution but it seems to work on the testdata you've provided.
N.B: I've assumed that there is a calendar dimension table, in my case called 'Calendar'.
First add a calculated column to the 'Activity' table:
Year-Month =
FORMAT([Activity_Date]; "YYYY-MM")
Then add the desired calculated column to the 'User' table:
Habit =
var minDate =
CALCULATE(
MIN('Activity'[Activity_Date]);
'Activity'[Indicator] <> BLANK()
)
var maxDate =
CALCULATE(
MAX('Activity'[Activity_Date])
)
var calendarActivity = DATEDIFF(minDate; maxDate; MONTH)
var sumAddOne =
CALCULATE(
DISTINCTCOUNT('Activity'[Year-Month]);
'Calendar'[Date] >= minDate;
'Calendar'[Date] <= maxDate
)-1
var sumSubtractOne = calendarActivity-sumAddOne
return
IF(
minDate <> BLANK();
sumAddOne-sumSubtractOne;
BLANK()
)
Then I end up with a 'User' table lilke this:

PowerBI: Find all records valid on specific date

I have a classic datawarehouse with records. Each record has a valid_from and a valid_to date.
Now I want to set a filter in PowerBI. The idea is that the user somehow sets a date and all records for which this date falls between valid_from and a valid_to should be available in PowerBI. Thus granting the user the possibility of timetravelling through the data.
This seems like a very standard task but I can't find how to do it.... Suggestions?
Given the vague question without explicit details, I'll have to make some assumptions. I'll assume that you have a date slicer that populated from a parameter table unrelated to your data table and that you have a set of measures you use in your visual(s) to display the records you're interested in.
Given those assumptions, you can write a measure to filter your an existing measure along these lines:
FilteredMeasure =
VAR SelectedDate = SELECTEDVALUE ( DateSlicer[Date] )
RETURN
CALCULATE (
[ExistingMeasure],
FILTER (
DataTable,
DataTable[valid_from] <= SelectedDate
&& SelectedDate < DataTable[valid_to]
)
)
Here's another similar but not completely equivalent formulation:
FilteredMeasure =
VAR SelectedDate = SELECTEDVALUE ( DateSlicer[Date] )
RETURN
CALCULATE (
[ExistingMeasure],
DataTable[valid_from] <= SelectedDate,
DataTable[valid_to] > SelectedDate
)

Filter across tables

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
)