Select rows with multiple max condition - subquery

Table details are
contract (Studentid, Term, Contract ID, Sequence No, Qualification)
For every studentid there are multiple contracts drawn up in different terms in a sequential order
I am looking at a sql that will help me capture all rows for distinct students that are latest term and max sequence

Related

sum values in power pivot using criteria from different table

I have been provided with two tables 1. sales table and 2.Items and it's characters. (The second table has two or more lines for one item if it has more than one characters)
I am trying to get the sum of sales value for the items based on its characters.
I have created a unique characters table and a unique material tables to link to other tables
In power pivot I am able to get the sum of sales by the material in U_Material, but if I include the Characters from U_Item and characters, then I am getting all the characters in the table and the sum, but actually it has only one characters (other characters are repeated with same value). what can i do to solve this.
if I don't include the materials or put them in filter then I get the overall sales value in the output but not as per the category.
Please let me know what I am doing wrong.
The problem is in your model. Your Fact table "Sales" are filtered only by U_Item any of your other tables don't have an impact on it; You should change the model to star/snowflake. Put "Sales" in the center, rest of your tables (Dimension) connect by relationship one to many (many on Sales side);

How to invert a merge query in power query

I have a single column table of customer account numbers and a main table containing 400,000 records pulling from an access database. I want to remove all records from the table where the customer account number can be found in the single column table.
The merge query capability in power query allows me to return only the records where there is a match on the customer list (in addition to a variety of other variations on this theme) but I would like to know whether there is a way to invert this so that I return all records where the customer number does not appear in this list.
I have achieved this already by using the List.Contains function and adding a custom column to identify the rows to exclude and then filtering them out, but I think this is severely impacting the performance of my workbook. Refreshing the table that initially has 400,000 rows prior to this series of transformations takes a very long time, and all queries that depend on this table then also take a long time to refresh.
Thank you
If you do a Left Anti Join of your table with a single column, this will give you your table filtered to only have the rows which do not match to the single column.

Excel Matching Customer Orders by Item and Quantity

Brief:
I have a large dataset, inside of which are Individual customer orders by item and quantity. What I'm trying to do is get excel to tell me which order numbers contain exact matches (in terms of items and quantities) to each other. Ideally, I'd like to have a tolerance of say 80% accuracy which I can flex to purpose but I'll take anything to get me off the ground.
Existing Solution:
At the moment, I've used concatenation to pair item with quantity, pivoted and then put the order references as column and concat as rows with quantity as data (sorted by quantity desc) and I'm visually scrolling across/down to find matches and then manually stripping in my main data where necessary. I have about 2,500 columns to check so was hoping I could find a more suitable solution with excel doing the legwork on identification.
Index/matching works at cross referencing a match for the concatenation but of course, the order numbers (which are unique) are different so its not giving me matches ACROSS orders.
Fingers crossed!
EDIT:
Data set with outcomes
As you can see, the bottom order reference has no correlation to the orders above it so is not listed as a match to the orders above but 3 are identical and 1 has a slightly different item but MOSTLY matches.

Dynamic Grouping in Power BI/PowerPivot Model based on a calculated field

We are trying to create a model in Excel/Power BI (using Power Query or Power Pivot or anything that would work) in order to classify a customer by its best product (based on a ranking system).
The first approach we applied was to count the customers per minimum ranking (or per best product brand). (inspired from the blog https://stackoverflow.com/questions/15742186/powerpivot-dax-dynamic-ranking-per-group-min-per-group)
Below the steps we did exactly:
- In PowerPivot Model, we created Classification and Customers table like in the example further below.
In the same model, we added a calculated column with the following formula to obtain the minimum rank per customer.
=MINX (
FILTER ( ALLSELECTED ( Customers ); [Customer_ID] = EARLIEST ( [Customer_ID] ) );
[Ranking]
)
Within a pivot table in Excel, we’ve put the calculated column in rows.
Then, we’ve used a Count distinct aggregation of the customers in the pivot table values.
 This gave me the first desired result. (below example Pivot_Table.Selection1)
Now, the issue comes when we want to add more analysis axis.
For example, besides the product brand, we want to have the Product type in columns, and we want our measure to be recalculated every time I add/delete an axis.
In other words, we want to have a distinct customer count per best product and per Product Type.
In addition, we want the second attribute (axis) to be variable and the grouping or the distinct count per group to be dynamic.
Example:
Let’s suppose we have the tables Classification and Customers in our Model:
In the first approach we tried, we got the following table: Pivot_Table.Selection1:
Now when we add the analysis axis, we would like to have the following example: Pivot_Table.Selection2:
But we are having this:
As you can see, there should be one customer for the Group “Mercedes” and one for “Renault”, since depending on the product type, the top Truck for customer A is Renault and its top Car is “Mercedes”.
However, in the pivot table, the Mercedes group is shown as Truck (which doesn’t even exist in our dataset).
Edit
I'm open for any suggestion, not only Power Pivot, but also Power Query (M functions) or Power BI or whatever could work.
Finally I think I understood your problem, a customer can have different Product_Brand values, you want to count only those Product_Brand which its ranking is the minimum.
In that case, this is a possible solution:
Create a calculated column called Minimum Rank in the Customer table.
=
CALCULATE (
MIN ( [Ranking] );
FILTER ( Customer; [Customer_ID] = EARLIER ( Customer[Customer_ID] ) )
)
Then create a measure, lets say Customer ID Distinct Count to count those rows where the Rank is equal to the minimum for that customer.
Customer ID Distinct Count :=
CALCULATE (
DISTINCTCOUNT ( Customer[Customer_ID] );
FILTER ( Customer; [Ranking] = [Minimum Rank] )
)
You will get something like this:

DAX Rank by Date

I am Counting on Distinct ID's in a column - this is leading to the sum of the subtotals not equalling the grand total as follows:
What I want to do is rank the Payment Dates in cronological order and select ONLY the highest date to display. In the example above the Grand Total won't change, but the Townville row will not show a Distinct Student Count.
This is a very specific requirement and I'm assuming there's an easy way to do it in DAX - I've tried playing around with both RANKX and MAX but am no closer to solving this.
One last thing - the Rank must be contextual to the Time Filter selected by the user (so if they select 2015 it'd give the second record Rank 1 and the top record wouldn't show. If they select May 2015 it'd give the top record Rank 1 and the second record wouldn't show)
I think this is what you are looking for - I added a calculated column to the PowerPivot model that provides a rank based on the Last Payment Date and the Name of the Student. It will rank the earliest payment for any student as a 1.
The code for the column is as follows:
=RANKX(FILTER(Table1, [Student Name] = EARLIER([Student Name])), [Last Payment Date])
... assuming your table is named "Table1"!
The FILTER is the key that limits the ranking to dates belonging to students with that name only.
Update for Multiple tables
To set up relationships between the tables, go to the "Diagram View" of the model, available in the Home tab of the Power Pivot window.
You can drag fields from one table to the other to create relationships. This will only work if at least one of the fields is unique - it's a good idea to think of the model as a dimensional model, with a tables that acts like a fact and other tables around it that act like dimensions.
From the comment, I would try to get the Payments to act like the fact, and have it link to the Community and Student tables. in this case, you could then have the following code:
=RANKX(FILTER(Table1, Related('Students'[Student Name]) = EARLIER('Students'[Student Name])), [Last Payment Date])
This calculated column would be on your Payments Fact table, and it uses a lookup to a related field.
Note that in this specific case, it would be easier to just run the filter over your Student ID field that is used to lookup the Student name.

Resources