I'll explain my problem with an example:
I have three tables: one of dates, another of employees and another of departments.
There are two relationships:
dim_date[date_id] -> dim_employee[start_date_id]).
dim_department[department_id -> dim_employee[current_department_id]
dim_employee
name
current_department_id
start_date_id
Employee 1
1
20210701
Employee 2
2
20210701
Employee 3
2
20210901
Employee 4
1
20220201
Employee 5
2
20220201
dim_department
department
department_id
Purchase department
1
Sales department
2
I want to count how many employees each department had, for example, in February 2022 (using a slicer).. I've tried this:
num_employees_purchase_department =
CALCULATE(
DISTINCTCOUNT(dim_employee[employee_id]),
dim_employee[current_department_id]=1
)
However, since the relation is made through start_date_id, I only get the new hires of each department (one for the sales department and one for the purchase department) and not the total number of hired personnel.
The result that I'm looking for:
Employees in Purchase Department: 2
Employees in Sales department: 3
Thank you so much!
Try to make dim_date - dim_employee relationship inactive (in Data model view) and add an extra condition into your measure:
CALCULATE(
DISTINCTCOUNT(dim_employee[name]),
dim_employee[start_date_id] <= MAX(dim_date[date_id])
)
Don`t forget to use USERELATIONSHIP() inside your other measures where active dim_date - dim_employee relationship is necessary.
Related
I have one question about DAX calculation?
Hi Guys,
I have one question about DAX Measure Calculation
Please Help me get the solution of this problem
I have 2 tables
table 1 - Purchase Info
(Column name - Customer ID, Product ID, Quantity, Payment Mode)
table 2 - Product Info
(Column name - Product ID, Product Name, Product Cost, Selling Cost, Tax Amount)
My Question Is, How to Calculate Total Revenue?
The Table 1 (Purchase Info) have repeated customers and more than 10 thousand of records and also records are shuffled. The table 2 (Product Info) have only 10 products and unique records ( Product ID). How to calculate the total revenue.
This will work if you linked your tables via Product ID.
SUMX(
Prod
,CALCULATE(SUMX(Sales,Sales[Quantity]))*Prod[Selling Cost]
)
I have two tables in Power BI as follows:
COUNTRIES
COD COUNTRY
1 BRAZIL
2 ARGENTINA
3 CHILE
4 BRASIL
5 COLOMBIA
6 ARGENTINA
7 URUGUAI
SALES
COD DATE
1 2021-01-02
2 2021-10-01
3 2019-09-04
1 2018-07-05
7 2019-04-10
There's a relationship between the two tables, on the COD column.
I need to count how many countries (column "COUNTRY" from the table "COUNTRIES") have a status CHURN. It's considered CHURN when their latest order from the table "SALES" is more than 180 days, using today as a reference.
I know that I need to group by the MAX date per country, do a DATEDIFF, and then do a COUNT. I've tried using ALL and SUMMARIZE, but I haven't found a way to solve this problem.
Are you able to add a calculated column to store the max sales date for each country in your COUNTRIES table? Either in Power BI or directly in your database. If so, here's one solution with 2 steps.
Create a MaxSalesDate column in your COUNTRIES table. DAX for a calculated column below:
MaxSalesDate =
VAR COD = COUNTRIES[COD]
RETURN MAXX(FILTER(SALES, SALES[COD] = COD), SALES[DATE])
Create a measure that counts the number of MaxSalesDate values that are older than 180 days old:
CountCHURN = COUNTX(COUNTRIES, IF(DATEDIFF(COUNTRIES[MaxSalesDate], TODAY(), Day) > 180, 1))
I have a table named 'DIM_Employee_Details' which contains Employee details along with supervisor id. Each employee has a supervisor. I am applying RLS based on employee id such that when an employee logs in, only the details of the employees who are under the same supervisor are shown. I want to display the rank of employees in descending order under a supervisor based on a measure. Kindly check the below measure used for ranking:
KPI Rank By Supervisor = VAR __ID = MAX(DIM_Employee_details[Supervisor_ID])
RETURN CALCULATE(RANKX(
ALLSELECTED(DIM_Employee_details[emplid])
,([Overall_attainment]),,DESC,Dense),
ALLEXCEPT(DIM_Employee_details,DIM_Employee_details[Supervisor_ID],DIM_Employee_details[emplid]), DIM_Employee_details[Supervisor_ID] = __ID)
While applying this, I am getting the max rank for which overall attainment has values and 1 for which it has blank values.
emplid
Employee_Name
Supervisor_ID
SUPERVISOR_NAME
1
A
IN087263
Yash
2
B
IN087263
Yash
3
C
IN087263
Yash
4
D
IN087263
Yash
I have a table with customers' SSN, account number, purchase date, and max purchase date (the most recent purchase date for SSN, across all the accounts). Customers can have multiple accounts. I know how to create a measure the calculate the distinct count of all the accounts that haven't been active since a certain date (6 months, 18 month, 24 months)..
I would like to create a measure or a calculated column to give me the following information.
when users select the date from the slicer (say 6 months) the chart shows the count of the accounts that have not made a purchase in 6 months, the users also want to have a drop down slicer("Yes", "No") to indicate if the SSN had activities under other accounts. i.e. if the max purchase date is greater than the value from the date slicer.
the table structure looks like this:
SSN AccountNumber LastPurchaseDate MaxPurchaseDate
123-45-5678 9876 8/2/2018 9/4/2019
123-45-5678 6398 9/4/2019 9/4/2019
135-65-4321 2233 6/6/2019 6/6/2019
Best way here would be if you add a custom column with the time difference (in the query designer):
= [MaxPurchaseDate] - [LastPurchaseDate]
Now you have something like this:
SSN AccountNumber LastPurchaseDate MaxPurchaseDate DateDiffDays
123-45-5678 9876 9/2/2018 9/4/2019 2
123-45-5678 6398 9/4/2019 9/4/2019 0
135-65-4321 2233 6/12/2019 6/6/2019 6
You can add another column which acts as filter for your 6 months, 18 month, 24 months (convert the DateDiffDays into months).
The following measure counts the accounts:
=Distinctcount('YourTable'[AccountNumber])
If you filter now by your 6 months, 18 month, 24 months column the measure gets after every filtering calculated again and you get your result.
I want to get a distinct count of Enquiries with Food and Drink in Power Bi. The way the database is structured is:
1 Enquiry could have multiple days, which have food and drink attached to them.
I want to try and calculate how many enquiries have food and drink attached so I need to do a distinct count on the enquiry id in the Enquiries table where a record exists in the food and drink table
The table structure is as follows:
Enquiry Table
EnquiryId
10
Enquiry Day Table
EnquiryDayID EnquiryId
5 10
6 10
Enquiry Day Food Drink Table
EnquiryDayFoodDrinkId EnquiryDayId
20 5
21 6
Basically I want the distinct count to only return 1 as there is only one distinct enquiry with food + Drink attached.
Here is the code in SQL which return the correct number. I want to get the same result in Power BI
select count (distinct(e.pkEnquiries))
from EnquiryDayFoodDrink edfd
inner join EnquiryDay ed
on ed.EnquiryDayId = edfd.EnquiryDayId
inner join Enquiries e
on ed.EnquiryId = e.pkEnquiries