PowerBI/DAX - summing up group membership - powerbi

I'm very new to powerBI and DAX. I have a challenge which is probably quite simple but I cannot figure it out.
To simplify, my data is basically a table of doctors, dates and points. Each doctor has a distinct and fixed region:
Doctor Region Points Date
John Doe A 3 2020-05-10
John Doe A 2 2020-05-10
John Doe A 4 2020-05-11
Mary Doe A 1 2020-05-10
Mary Doe A 3 2020-05-11
Jane Smith B 1 2020-05-10
Jane Smith B 1 2020-05-10
...etc...
It is simple for me to display this data as a line chart with date on the x-axis (using a date lookup table), and sum of points on the y-axis, where the doctor can be selected individually in an associated slicer.
Here is the challenge. I need to be able to chart both the sum for doctor and the sum of all points in the selected doctor's region (i.e. two lines on the chart, plotted by date) just by selecting the doctor on a single slicer.
I have experimented with creating a new column
report_points_all = CALCULATE( SUM(MyTable[points]), ALL(MyTable) )
But this is giving me nonsense data with enormous sums. I need to filter it down by the region associated with the selected doctor but don't have enough DAX skill to figure it out. Can anyone help point me in the right direction? Thanks in advance

Your current formula is not that far from working
report_points_all = CALCULATE( SUM(MyTable[points]), ALL(MyTable) )
But ALL(table) removes the filters on the whole table (doctors, date, region, etc), that's why you are probably getting one huge constant value.
What you want to do is removing the filter only on the the Doctor column
report_points_all = CALCULATE( SUM(MyTable[points]), ALL('MyTable'[Doctor]) )

Related

Power BI How to remove duplicate rows?

In my report view, I have a table where the rows are repeated twice => once for each position available. I want to show only one row for each employee with his latest position. How can I accomplish this?
Name
Project
Date
Position
John Smith
PowerProject
01-01-2021
Engineer
John Smith
PowerProject
01-01-2021
Senior Engineer
Sort on the date. Group on the name. Choose All Rows as the function and change the code from _ to Table.Last(_) then expand

Power BI DAX - sum all of one column but keyed off different table

This FEELS like something that can be done but I am at a loss for how to do it.
I have a table that has applicants for jobs...
name, requisition id, division, date applied, date hired
Each row is an applicant. Obviously not all applicants are hired. So in every row all fields are filled out with the exception of date hired for applicants that have not been hired.
I have slicers for month/quarter/year and division.
The date slicers all key off a field in every table called data_as_of which is the last day of the month with a one-to-many relationship with a date dimension table.
Here is a sample table...
[1]
[1]: https://i.stack.imgur.com/XQO9d.png
So here is what I'd like to do.
I'd like to slice by year and show a visual of all people hired in that year. Same with Quarter and Month (ie count all people in that quarter or month as appropriate). So far so good. That's easy.
Now on the same report page I'd like to show a visual (assume bar charts) that shows me a count of all the people that applied to the same requisition id prior to the date hired of whomever was hired in that requisition id.
Using the example above...
All of these examples assume 2021.
So if I used the month slicer in December I'd get 2 hirees in HR, Diane and Mel. In the second visual I'd get 7 Applicants.
If I used the month slicer to show November I'd get two hirees - Rhys and Jody. The applicant visual would show me 8 applicants. All 6 from requisition id 4 and 2 from requisition id 2 because one applied after Rhys was hired.
Consequently if I sliced for April of 2021 I'd get 1 hiree - Remi. In the applicant visual I'd get 4 applicants who all applied prior to Remi's hire date (including Morgan who applied in March but wasn't hired until May).
Does that all make sense?
I very much appreciate your help.
Best regards,
~Don

AWS QuickSight, How to add new calculated filed which is Sum of column value till the current row

I am new to QuickSight, I want to know how to add a calculated field that will result in giving the sum up until that particular row.
Say I have a donation data set with given below columns.
Name
Mode
donation
Rahul
Cash
100
David
Online
150
Adam
Cash
200
...
Now I want to add a calculated field (total so far) that should give the total of the donation until that row, as below
Name
Mode
donation
Total so far
Rahul
Cash
100
100
David
Online
150
250
Adam
Cash
200
450
Basically Total so far column is current row donation + the sum of previous donations. I don't want a visual but I want as data set itself, how can I do this.
Use the runningSum function
If you want to have this sorted by a particular field then that goes at the end in the square brackets, I am using "Name" in ascending order in the following example
runningSum
(
{donation},
[{Name} ASC]
)

RANKX in table and slicers

This is table Fact:
Date Person Place Status Sales
01/01/2020 ABC North Active 9852
14/01/2020 DEF North Active 3452
17/01/2020 GHI North Active 9084
02/02/2020 GHI North Active 4902
14/02/2020 GHI North Active 4659
14/02/2020 DEF South Inactive 5000
23/02/2020 GHI North Active 1685
10/03/2020 GHI North Active 6401
21/03/2020 ABC Center Active 4742
09/04/2020 ABC Center Active 6325
14/04/2020 ABC Center Active 8329
27/04/2020 ABC Center Inactive 7740
28/04/2020 ABC Center Inactive 5091
02/05/2020 ABC Center Inactive 3763
04/05/2020 ABC Center Inactive 1434
06/05/2020 DEF Center Active 3718
22/05/2020 DEF South Active 6639
03/06/2020 DEF South Active 5672
12/06/2020 DEF South Active 5268
16/06/2020 DEF South Active 3358
I want to calculate the ranking of sales, depending on slicers for dimensions date, person, status and place.
So:
This measure:
TotalSales = SUM('Fact'[Sales])
Gives me the to total sales.
And:
This measure:
Ranking =
IF([TotalSales],
RANKX (
ALLSELECTED ('Fact'),
CALCULATE ( [TotalSales] ),
,
DESC,
DENSE
))
Is supposed to give me the ranking.
And it does, if all the dimensions are in:
However, if I remove the date:
This is non-sense. Can anyone help calculating the rank? Thanks
Sorry I'm replacing my answer completely because I misunderstood your problem.
I used the following:
Rank1 =
CALCULATE(
RANKX(
ALLSELECTED(Table1[Person], Table1[Place], Table1[Status]),
CALCULATE(
SUM(Table1[Sales])
)
)
)
Using your data, I made the visuals below, and am able to slice by place, person, and status. The problem with slicing by date, however, is that it's too granular, so that everything becomes #1 within that date period. You may want to add new columns like "Year" or "Quarter" which you can then add to the ALLSELECTED() statement in order to rank by, say, the person's best quarter, or the area's best year.
I opened your file & have some good news. I was able to find the issue.
STEP 1
Select all the Months & Places in your filters in the top. Hold CTRL to select multiple items. On the right, get rid of all the things that are in the way. Just check what I show here.
STEP2
Un-check the Date to see that the report goes crazy.
STEP 3
Un-check Total Sales & check Sales instead. You can also re-check Date here.
STEP 4
Hit the drop-down menu for Sales & select Don't Summarize.
STEP 5
Enjoy that you now have all 20 records again, but with no Date showing.

power bi calculate sameperiodlastyear problem

I have a table of data for two years where I want to retrieve the previous years value for a date. my table the same date shown many times delineated by another value e.g.
Employee, Date, Sales, Prev yr sales
Sam, 1/07/2017, 100
Sam, 2/07/2017, 120
John, 1/07/2017, 90
John, 2/07/2017, 23
etc
Sam, 1/07/2018, 200, 100
Sam, 2/07/2018, 21, 120
John, 1/07/2018, 45, 90
John, 2/07/2018, 130, 23
etc
I am using a dates table created in DAX and have made a relationship between the dates in my table and the dates table.
I have created a measure for the sales & a column for the previous year sales. the latter using calculate and the sameperiodlastyear:
Prev Yr Sales = CALCULATE([Sum Sales],SAMEPERIODLASTYEAR('Calendar'[Date]))
my problem is I cant get the prev yrs sales value out in a table if there are multiple rows per date i.e. sam and john. if there is only one employee the function works.
Can anyone help?
When you are writing a calculated column with a CALCULATE in it, the row context from the table becomes the filter context you are working evaluating in. For this calculation, you don't want any of that row context except for the employee name, so you can remove it using the ALLEXCEPT() function:
Prev Yr Sales =
CALCULATE(
[Sum Sales],
ALLEXCEPT(Sales, Sales[Employee]),
SAMEPERIODLASTYEAR('Calendar'[Date])
)
I've done a quick simulation of the case you've described and found out that the reason you're not getting the expected results might be missing .[Date] on the end of your SAMEPERIODLASTYEAR function argument.
Check out the Data view on my simulation and the highlighted adjustment in the Prev Yr Sales measure:
And the matching result on a Matrix visualization on the Report view:
Hope it helps.