Powerpivot one to many filter column - powerpivot

I have 2 tables, a visit table and then a diagnosis table. Visit is my one and diagnosis is my many (you can have secondary and tertiary diagnosis).
I have over 50 measures created in the visit table. Because of this, I simply want to create a flag on my visit table in the form of a column that will filter down my results based upon selections from the Diagnosis table. I have seen formulas for measures that use =calculate([cases],DXCodes) which will filter down the cases to only those that relate to the DXCodes and this works, but I don't want to have to build this in for every measure. Instead I want a DXFlag that will flag my visit rows with a 1 if what I selected from Diagnosis exists in the Visit table. Suggestions? I can get more specific if this is not making sense

Making some assumptions about your data, you could have tables like this:
And if you create a measure to pick out each of the diagnoses:
[diag1]:=FIRSTNONBLANK(Visits[Primary Diag],1)
[diag2]:=FIRSTNONBLANK(Visits[Secondary Diag],1)
[diag3]:=FIRSTNONBLANK(Visits[Tertiary Diag],1)
Then add this measure to flag your visit rows:
[Found]:=MAXX(ALLSELECTED(Diagnoses[Diagnosis]),IF(FIRSTNONBLANK(Diagnoses[Diagnosis],1)=[Diag1]||FIRSTNONBLANK(Diagnoses[Diagnosis],1)=[Diag2]||FIRSTNONBLANK(Diagnoses[Diagnosis],1)=[Diag3],1,BLANK()))
This gives you this result:
Edit: Response to revised/clarified requirements
I've been trying to see if I can get a calculated column to take account of the diagnosis selection with little success. Doing it with a measure is fairly straightforward:
[Diag Flag]:=IF(CALCULATE(COUNTROWS(DiagnosisTable),FIRSTNONBLANK(DiagnosisTable[Diagnosis],1))>0,1)
But this approach would require you to revise all the measures you wish to appear on your output summary. If it's just the total number of visits and the sum of the length of stay, that's not difficult, but if you need to revise all 50 measures it's a bit less appealing.
Here are the new measures for the number of visits and the length of stay which I have shown in a separate PivotTable on the same sheet:
[Cases]:=SUMX('Visits',[Diag Flag])
[LengthSum]:=SUMX('Visits',Visits[Length of Stay]*[Diag Flag])
And the output:
NOTE: My slicer is connected to both tables.

Related

Create and define functions to apply to different segments/filters that exist (Instead of doing one very long SQL query)

I am trying to come up with some arithmetic calculations for some survey data. I want to do these calculations for a number of segments and want to figure out how to do it without writing numerous SELECT statements.
This is what I have so far:
FACT table. This tables holds survey data at a respondent level - for example, if a survey had 10 questions, this table will have 11 columns: a column to identify the respondent_ID and 10 other columns to identify the responses to those questions.
DIMENSION table. This table segments we want to view the survey data by at a respondent level - for example, if we want to view survey responses by membership_status and age_bracket, this table will have 3 columns: a column to identify the respondent_ID, and two columns to identify membership_status and age_bracket.
OUTPUT.
I want to get aggregate calculations to summarizes the responses to the survey overall and to each question. I also want to be able to get this information for all possible segments that exist in the DIMENSIONS table.
I can do the query below, however I'll need to do this for every segment:
SELECT
COUNT(DISTINCT(CASE WHEN f.QUESTION_1 IN ('8', '9', '10') THEN f.RESPONDENT_ID END))*1.0 / COUNT(DISTINCT(CASE WHEN f.QUESTION_1 IS NOT NULL THEN f.RESPONDENT_ID END))*1.0 AS CSAT_1
FROM FACT f
JOIN DIMENSION d ON f.RESPONDENT_ID = d.RESPONDENT_ID
WHERE d.MEMBERSHIP_STATUS = 'ACTIVE'
The calculation above gives us something called a top 3 box. That is just one calculation, I will need to do many of them. Additionally, ever calculation will need to be done for each segment. In order to get a calculation for nonactive members, I would need to run another query and set d.MEMBERSHIP_STATUS = 'INACTIVE' and I would need to run another query with no filter, to get the overall calculation.
Is there a way I could store all my arithmetic calculations needed in my output as a function (maybe in a temp table or something) - my thought is that it'll be better to set the functions somewhere, and then when I need to calculate the output, I would some how call the function to do all the calculations I need, and give me all the calculations for every segment I have?
I can't fully envision how to get there, or if this is even a good solution, so guidance and detailed SQL code would be extremely helpful.Examples please!

PowerPivot Ranking Groups using DAX's Rankx - Ranking Using Sum of a Field

Am trying to rank groups by summing a field (not a calculated column) for each group so I get a static answer for each row in my table.
For example, I may have a table with state, agent, and sales. Sales is a field, not a measure. There can be many agents within a state, so there are many rows for each individual state. I am trying to rank the states by total sales within each state.
I have tried many things, but the ones that make the most sense to me are:
rankx(CALCULATETABLE(Table,allexcept(Table,Table[AGENT]),sum([Sales]),,DESC)
and
=rankx(SUMMARIZE(State,Table[State],"Sales",sum(Table[Sales])),[Sales])
The first one is creating a table where it sums sales without grouping by Agent. and then tries to rank based on that. I get #ERROR on this one.
The second one creates a table using SUMMARIZE with only sum of Sales grouped by state, then tries to take that table and rank the states based on Sales. For this one I get a rank of 1 for every row.
I think, but am not sure, that my problem is coming from the sales being a static field and not a calculated measure. I can't figure out where to go from here. Any help?
Assuming your data looks something like this...
...have you tried this:
Ranking Measure = RANKX(ALL('Table'[STATE]),CALCULATE(SUM('Table'[Sales])))
The ALL('Table'[STATE]) says to rank all states. The CALCULATE(SUM('Table'[Sales])) says to rank by the sum of their sales. The CALCULATE wrapper is important; a plain SUM('Table'[Sales]) will be filtered to the current row context, resulting in every state being ranked #1. (Alternatively, you can spin off SUM('Table'[Sales]) into a separate Sales measure - which I'd recommend.)
Note: the ranks will change based on slicers/filters (e.g. a filter by agent will re-rank the states by that agent). If you're looking for a static rank of states by their total sales (i.e. not affected by filters on agent and always looking at the entire table), then try this:
Static Ranking Measure = CALCULATE([Ranking Measure], ALLEXCEPT('Table', 'Table'[State]))
This takes the same ranking measure, but removes all filters except the state filter (which you need to leave, as that's the column you're ranking by).
I did figure out a solution that's pretty simple, but it's messier than I'd like. If it's the only thing that works though, that's okay.
I created a new table with each distinct state along with a sum of sales then just do a basic RANKX on that table.

Group Entire Crystal Report by Column in Subreport

I am trying to set up the following report so that it is grouped first by "Transfer-In Unit" (d1_10.xinstitute), which is the title of a column that is included in a sub-report, and second by xpid, which is a patient index number used in all tables (except a catalog) in this report. To simplify this question, assume that xpid is indexed in alphabetical order of the patients' names.
See info/examples below:
This report shows patients that have transferred out of a specific unit since the user-inputted date (Name), a row for each date that the patient transferred out of a particular unit (Unit M Transfer-Out Date), chronologically where they next transferred into (Transfer-In Unit), and when they transferred into that unit (Transfer-In Date).
Currently without attempting this grouping the output looks like this:
Name and Unit M Transfer-Out Date are on the parent report
Transfer-In Unit and Transfer-In Date are in a subreport because they both call d1_10.dstartdate but with different criteria.
The goal is to sort by Transfer-In Unit, which is a column in the subreport.
The output should be:
An underlying issue is that to select the chronological next transfer-in following a transfer-out, I have to select a minimum transfer date grouped by xpid. This causes the requirement of an xpid grouping in the sub-report.
I have tried a grouping by d1_10.xinstitute (the unit index) in both the parent- and sub-reports. In the parent report it creates one all-encompassing group, because every row in this report represents someone transferring out of "Unit M". In the subreport it seems to apply the groupings row by row, so each row has two groups, even when the next row is the same patient transferring into the same unit. However this is my best attempt and it looks like this:
Any help/thoughts/ideas much appreciated. Let me know if I can provide any further info.
Thanks

SQL counting number of rows

I am looking for a way to search for a certain number of rows as a quality check. For example, we have tables that have a certain set of results that are needed.
Here is a quick table for an example:
ID: Name: Result: Reportable:
ONE A 10 X
TWO B 12 X
THREE C 1
FOUR D 18 X
FOUR(redo) D 11 X
So we are looking to double check results as there are people who accidentally report results multiple times (as in the case with ID FOUR). We have used having counts but we need the numbers to be specific and need a query to verify that number is satisfied.
In the table above we only want IDs ONE, TWO, and FOUR, however we have 4 results (one extra). Currently we have our check showing the count needed (ie 3) and the current result count (4) to show the mismatch but want a query to easily only show the result needed. We would need the redo result most of the time so we have set it so we take the latest date, but it doesn't help filter how many rows or results. I apologize if anything is confusing and I am not able to share the SQL query that we have currently. It's my first time posting so if I need to clarify anything please let me know as this seems to be very complicated. Thank you for your time.
EDIT: The details
We have one table (Table A) letting us know which results are reportable. The ones that are reportable go into another table (Table B). We have had issues in which people have made too many results reportable which overpopulates the Table B. Our old query had a count in Table B, but due to mistakes in people placing multiple reportables, samples which had many redos seem to be finished as they were all placed and met the count in Table B.
So now by using the Table A that helps tell us how many are Reportable, we want this to double check that the samples are indeed ready.
As I understand the question, you want ids that have multiple reportables. Assuming you really mean name, then:
select name
from t
where reportable = 'X'
group by name
having count(*) >= 2;

Calculating a Ratio using Column A & Column B - in Powerpivot/MDX/DAX, not in SQL

I have a query to pull clickthrough for a funnel, where if a user hit a page it records as "1", else NULL --
SELECT datestamp
,COUNT(visits) as Visits
,count([QE001]) as firstcount
,count([QE002]) as secondcount
,count([QE004]) as thirdcount
,count([QE006]) as finalcount
,user_type
,user_loc
FROM
dbname.dbo.loggingtable
GROUP BY user_type, user_loc
I want to have a column for each ratio, e.g. firstcount/Visits, secondcount/firstcount, etc. as well as a total (finalcount/Visits).
I know this can be done
in an Excel PivotTable by adding a "calculated field"
in SQL by grouping
in PowerPivot by adding a CalculatedColumn, e.g.
=IFERROR(QueryName[finalcount]/QueryName[Visits],0)
BUT I need give the report consumer the option of slicing by just user_type or just user_loc, etc, and excel will tend to ADD the proportions, which won't work b/c
SUM(A/B) != SUM(A)/SUM(B)
Is there a way in DAX/MDX/PowerPivot to add a calculated column/measure, so that it will be calculated as SUM(finalcount)/SUM(Visits), for any user-defined subset of the data (daterange, user type, location, etc.)?
Yes, via calculated measures. calculated columns are for creating values that you want to see on rows/columns/report header...calculated measures are for creating values that you want to see in the values section of a pivot table and can slice/dice by the columns in the model.
The easiest way would be to create 3 calculated "measures" in the calculation area of the powerpivot sheet.
TotalVisits:=SUM(QueryName[visits])
TotalFinalCount:=SUM(QueryName[finalcount])
TotalFinalCount2VisitsRatio:=[TotalFinalCount]/[TotalVisits]
You can then slice the calculated measure [TotalFinalCount2VisitsRatio] by user_type or just user_loc (or whatever) and the value will be calculated correctly. The difference here is that you are explicitly telling the xVelocity engine to SUM-then-DIVIDE. If you create the calculated column, then the engine thinks you want to DIVIDE-then-SUM.
Also, you don't have to break down the measure into 3 separate measures...it's just good practice. If you're interested in learning more, I'd recommend this book...the author is the PowerPivot/DAX guru and the book is very straightforward.